Skip to content

Instantly share code, notes, and snippets.

View sgillies's full-sized avatar

Sean Gillies sgillies

View GitHub Profile
@sgillies
sgillies / polygonizing.py
Created January 16, 2014 23:55
polygonizing with rasterio and fiona
import rasterio
with rasterio.open(raster_filename, 'r') as src:
image = src.read_band(1) # read
features = rasterio.features.polygonize(image) # no I/O!
with fiona.open(vector_filename, 'w', **meta) as dst:
dst.writerecords(features) # write
/******************************************************************************
* $Id$
* Project: GDAL
* Purpose: Raster to Polygon Converter
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2008, Frank Warmerdam
*
* Permission is hereby granted, free of charge, to any person obtaining a
@sgillies
sgillies / about.md
Last active April 24, 2021 21:57
Rasterio example

>>> import numpy
>>> from scipy.ndimage.morphology import grey_dilation
>>> a = numpy.zeros((7, 7))
>>> a[3,3] = 1
>>> a
array([[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 1., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
import rasterio
with rasterio.open('input.tif') as src:
source = src.read_band(1)
def window_transform(transform, offset):
result = transform[:]
result[3] = transform[3] + offset[0]*transform[5]
result[0] = transform[0] + offset[1]*transform[1]
return result
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
...
#GPX GPX Yes Yes Yes (read support needs libexpat)
("GPX", "raw"),
...
@sgillies
sgillies / subsample.py
Last active August 29, 2015 13:57
Stats on rasters subsampled by rasterio
import rasterio
import numpy as np
src = rasterio.open('/Users/sean/code/decloud/envs/1_16/output/0_q.tif')
# Fraction of elements with value > 100 at full resolution.
q = src.read_band(1)
ones = np.ones(q.shape)
print "Full, %d pixels" % (q.shape[0]*q.shape[1])
print sum(ones[q>100])/(q.shape[0]*q.shape[1])
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.