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 / walk_up.py
Last active October 19, 2017 16:50
recursive function to walk up flow table. NOTE: this will not handle braided flowlines!
from StreamCat_functions import dbf2DF
pre = 'D:/NHDPlusV21/NHDPlusGL/NHDPlus04'
fline = dbf2DF('D%s/NHDSnapshot/Hydrography/NHDFlowline.dbf' % pre)
flow = dbf2DF('%s/NHDPlusAttributes/PlusFlow.dbf' pre)[['TOCOMID','FROMCOMID']]
def recurs(val, ups):
print val
ups = ups + flow.ix[flow.TOCOMID == val].FROMCOMID.tolist()
if 0 in ups:
@debboutr
debboutr / dbf2Raster.py
Created October 12, 2017 20:10
Make raster from dbf of shapefile
# create raster from dbf of shapefile
# this works only if dbf is set up reading
# left to right across rows and top down
from osgeo import ogr, gdal
from collections import OrderedDict
from StreamCat_functions import dbf2DF
shp = 'shapes.shp'
att = 'TD_N'
tbl = dbf2DF(shp.split('.')[0] + '.dbf')
@debboutr
debboutr / rasterize.txt
Created October 12, 2017 19:30
Command line statement to perform gdal_rasterize on shapefile
(nusc) L:\...\WetlandConnectivity\NitrogenModeling\LandscapeRasters\scrap_RD>gdal_rasterize -te -2556000.0, -1728000.0, 2952000.0, 1860000 -tr 12000 12000 -a TD_N -where "TD_N >= 0" -a_nodata -999 -l shapes shapes.shp agree2.tif
-te : extent (get from >ogrinfo -so shapes.shp shapes)
-tr : cell resolution/size
-a : attribute in shapefile to put into raster cells
-where : SQL statment on attribute values (not necessarily needed in this case, covered w/ nodata)
-a_nodata : value to be turned to nodata
-l : layer name w/in shapefile, normally same as filename
in .shp filename
out .tif file
@debboutr
debboutr / convert_value.py
Created October 4, 2017 23:36
Fill raster cell values that aren't NoData with a single value with georasters
import numpy as np
import georasters as gr
fll = 'D:/Projects/georaster_scrap/old/lakes_05b.tif'
a = gr.from_file(fll)
a.raster.data[:] = 1
a.raster.unshare_mask()
a.nodata_value = 0
a.datatype = 'Byte'
@debboutr
debboutr / georaster_trim.py
Created October 4, 2017 23:33
trim NoData cells surrounding raster with GeoRaster package
import numpy as np
import georasters as gr
def trim_nodata(ras):
ras.raster.unshare_mask()
bbox = np.where(ras.raster.mask == False)
g = ras.geot
ymin = bbox[0].min()
ymax = bbox[0].max()
xmin = bbox[1].min()
@debboutr
debboutr / do.txt
Created July 27, 2017 23:52
use to allow for python3 in environment when using jupyter notebooks
python -m ipykernel install --user
@debboutr
debboutr / remakeStreamCat.py
Created July 24, 2017 23:01
Make the upstream numpy array w/o filtering out flowline w/ no associated catchment
import os, sys
import itertools
import decimal
import struct
import numpy as np
import pandas as pd
import datetime
from collections import defaultdict
from datetime import datetime as dt
@debboutr
debboutr / loadZipdCSV.py
Created July 18, 2017 18:34
load a zipped csv directly into a pandas DF w/o unzipping
pd.read_csv('./COMID_HydroRegion.zip', compression='infer', sep=',',dtype={'COMID':int,'VPU':str})
@debboutr
debboutr / areaCompare.py
Created July 11, 2017 23:39
THIS SCRIPT COMPARES WATERSHED AREAS ACCUMULATED IN STREAMCAT VS. AGAP
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 06 16:02:50 2017
@author: Rdebbout
"""
import os
import h5py
import struct
@debboutr
debboutr / index.html
Created June 13, 2017 20:16
Collision detection example d3
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var nodes = d3.range(200).map(function() { return {radius: Math.random() * 12 + 4}; }),