Skip to content

Instantly share code, notes, and snippets.

View mhweber's full-sized avatar

Marc Weber mhweber

View GitHub Profile
@mhweber
mhweber / explode.py
Created July 25, 2016 17:45 — forked from debboutr/explode.py
Explode MultiPolygon geometry into individual Polygon geometries in a shapefile using GeoPandas and Shapely
import geopands as gpd
from shapely.geometry.polygon import Polygon
from shapely.geometry.multipolygon import MultiPolygon
def explode(indata):
indf = gpd.GeoDataFrame.from_file(indata)
outdf = gpd.GeoDataFrame(columns=indf.columns)
for idx, row in indf.iterrows():
if type(row.geometry) == Polygon:
outdf = outdf.append(row,ignore_index=True)
@mhweber
mhweber / Download_StreamCat.py
Last active September 20, 2016 16:00
Example of ftp download for StreamCat data
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 08:20:02 2016
@author: mweber
"""
import os
from ftplib import FTP
@mhweber
mhweber / R PostGIS
Created December 2, 2016 19:27
Figured out reading my PostGIS data directly in R using Rpostgis package
# example of reading regular data table from Postgres using RPostgresSQL library
library(RPostgreSQL)
con <- dbConnect(PostgreSQL(), host="localhost", port="5432", user="username", password="password", dbname="db_name")
result <- dbGetQuery(con, statement = "SELECT * FROM table1, table2 WHERE table1.key = table2.key")
head(result)
# example of reading spatial data from PostGIS using rpostgis library
library(rpostgis)
conn <- dbConnect(PostgreSQL(), host="localhost", port="5432", user="postgres", password="postgres", dbname="sp_db")
dbSchema(conn, name = 'sp_db')
@mhweber
mhweber / Georasters_NoData.py
Created December 6, 2016 16:28
Set a specific value as nodata in a geotif using python georasters package
import georasters as gr
data = gr.from_file('myraster.tif')
data.nodata_value
# assuming we want to set 0s to nodata-
data2= gr.GeoRaster(
data.raster,
data.geot,
nodata_value=0,
projection=data.projection,
datatype=data.datatype,
@mhweber
mhweber / Shapefile_Rasterize
Last active August 21, 2023 19:18
Rasterize a shapefile using geopandas and fiona
import geopandas as gpd
import rasterio
import fiona
from rasterio import features
import os
from datetime import datetime as dt
def Rasterize(shapefile, inras, outras, meta, field):
with rasterio.open(inras) as src:
kwargs = src.meta.copy()
@mhweber
mhweber / reduce_raster_depth.py
Created January 12, 2017 19:34
Reduce the bit depth of a given raster using georasters package in python
import georasters as gr
def reduce_depth(inras, outras, out_depth, nodata):
infile = gr.from_file(inras)
out= gr.GeoRaster(
infile.raster,
infile.geot,
nodata_value=nodata,
projection=data.projection,
datatype='Byte')
@mhweber
mhweber / MissingFiles.py
Created January 25, 2017 18:02
Short code snippet to get a list of files in one directory that don't have corresponding file in another directory using list comprehension and sets. Also gets unique names where multiple files with same 'base' name.
import os
dir1_vars = [f.split('_')[0] for f in os.listdir(dir1) if f.count('Region01')] # files in this directory have repeating base names with '_Region##' at end, this grabs unique file names
state_vars = [f.split('_')[0] for f in os.listdir('L:/Priv/CORFiles/Geospatial_Library/Data/Project/StreamCat/FTP_Staging/StreamCat/States') if f.count('AL')] # files in this directory have repeating base names with state name at end, this grabs unique file names
missing = list(set(hydroregion_vars) - set(state_vars))
@mhweber
mhweber / Nearby.R
Last active February 15, 2017 16:48
Function to find nearest polygons to a set of spatial points and return identifying column from polygons as a column in points attribute table
nearby = function(points, polys, column){
m = gDistance(points, polys, byid=TRUE)
row = apply(m, 2, function(x) which(x==min(x)))
row = sapply(row, "[",1)
labels = unlist(polys@data[row,][[column]])
points$missing <- labels
# head(missing)
return(points@data)
}
@mhweber
mhweber / Arcpy GISPro
Created June 22, 2017 18:15
syntax for running arcpy scripts using ArcGIS Pro
"c:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat" my_script.py
@mhweber
mhweber / SF_Point_Poly.R
Last active January 30, 2018 17:37
Do a point in polygon join using sf package to get polygon attributes where points land - in this case the NHDPlus catchment ID where points land
library(sf)
gages <- st_read('C:/Users/mweber/Temp/gages_test.shp')
class(gages)
# Let's pretend it's a csv file we read in rather than a shapefile - I'll strip out the 'spatial'
# part (the geometry column) and make it just a data.frame with Lon and Lat columns, then show
# how to promote it back
st_geometry(gages) <- NULL
class(gages)