Skip to content

Instantly share code, notes, and snippets.

View debboutr's full-sized avatar
💭
...working on it...

Rick Debbout debboutr

💭
...working on it...
View GitHub Profile
@debboutr
debboutr / xml2DF.py
Last active January 24, 2020 16:05
Read XML file from GRID raster format...
import xml.etree.ElementTree as ET
from lxml import etree
import pandas as pd
xml_data = "%s/rasters/wtshds_%s.aux.xml" % (out,rpu)
def xml2df(xml_data):
tree = ET.parse(xml_data)
root = list(list(tree.getroot())[0])[1]
all_records = []
@debboutr
debboutr / blank.py
Created May 12, 2017 22:21
Remove code from jupyter notebook
from IPython.display import HTML
################################
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
@debboutr
debboutr / dms2DD.py
Last active April 2, 2017 13:36
Convert degrees minutes seconds to decimal degrees
import re
def dms2dec(dms_str):    
"""Convert coordinates in DMS notation to decimal         
Author:  https://github.com/jeteon       """   
dms_str = re.sub(r'\s', '', dms_str)         
sign = -1 if re.search('[swSW]', dms_str) else 1         
numbers = filter(len, re.split('\D+', dms_str, maxsplit=4))    
degree = numbers[0]    
minute = numbers[1] if len(numbers) >= 2 else '0'    
second = numbers[2] if len(numbers) >= 3 else '0'    
@debboutr
debboutr / getWKT.py
Created March 27, 2017 06:43
Get WKT from either epsg.io OR spatialreference.org
def getWKTio(epsg) :
import urllib
from bs4 import BeautifulSoup
html = 'http://epsg.io/{}'.format(epsg)
r = urllib.urlopen(html)
data = r.read()
soup = BeautifulSoup(data, "html")
for tag in soup.find_all('pre'):
if tag.get('id') == 's_esriwkt_text':
return tag.contents
@debboutr
debboutr / snapPoint.py
Last active March 27, 2017 05:55
This function will snap all points in a shapefile to a line file.
import pandas as pd
import geopandas as gpd
def main(ptDF, lnDF):
# handle null geometries in point DF
ptDF = ptDF[~ptDF.geometry.isnull()]
# handle crs issues if geographic
ptDF.to_crs({'init': u'epsg:5070'}, inplace=True)
lnDF.to_crs({'init': u'epsg:5070'}, inplace=True)
shplyLineString = lnDF.ix[0].geometry
@debboutr
debboutr / dimPoints.py
Created March 11, 2017 04:11
reduce MultiPoint to Point geopandas
weird = gpd.read_file('./temp/LakeBoundaryMatch/TOTAL.shp')
money = gpd.GeoDataFrame()
for idx, row in weird.iterrows():
for shp in row.geometry:
mn = gpd.GeoDataFrame({'comid1':[row.comid1],'comid2':[row.comid2], 'VPU':[row.VPU]},geometry=pts)
money = pd.concat([money, mn])
@debboutr
debboutr / polygonToRaster.py
Created March 8, 2017 17:17
Handle conversion for very large rasters to prevent Overflow Error!
import itertools
import rasterio as rs
import geopandas as gpd
from rasterio.crs import CRS
from rasterio import features
def makeAffine(tf, lbl, w, h):
if lbl == 'A':
return tf
if lbl == 'B':
@debboutr
debboutr / compareRasters.py
Created October 25, 2016 18:24
compares if no data cells exist between two rasters
import os
import rasterio
import numpy as np
home = 'L:/Priv/CORFiles/Geospatial_Library/Data/Project/StreamCat/LandscapeRasters/QAComplete'
tifs = []
for x in os.listdir(home):
if ".tif" in x and "mar14" in x and not 'xml' in x and not 'ovr' in x:
print x
tifs.append(x)
@debboutr
debboutr / sjoinPointsNHD.py
Created September 15, 2016 22:46
Spatially joins points with all zones of the NHD appending the COMID of the catchment that each point falls in
import numpy as np
import geopandas as gpd
from geopandas.tools import sjoin
NHD_dir = "D:/NHDPlusV21"
inputs = np.load('%s/StreamCat_npy/zoneInputs.npy' % NHD_dir).item()
pt_file = "C:/Users/Rdebbout/Downloads/Stations_albers.shp"
pts = gpd.GeoDataFrame.from_file(pt_file)
l = "/NHDPlus"
import arcpy, os, sys
from arcpy import env
inputs = {'CA':['18'],'CO':['14','15'],'GB':['16'],'GL':['04'],'MA':['02'],'MS':['05','06','07','08','10L','10U','11'],'NE':['01'],'PN':['17'],'RG':['13'],'SA':['03N','03S','03W'],'SR':['09'],'TX':['12']}
# process one coordinate at a time, locating routes for all hydroregions before looping to next coordinate set
# note that once the flowlines are converted into routes, all attribute information will get dropped, we will process the Locate Routes tool again to determine COMID (to get to stream order) for all sites that fail the checks
sites = 'D:/Projects/WEMAP_WSA/SplitCatsNAD83/xyLayer_join.shp'