Skip to content

Instantly share code, notes, and snippets.

@jasonbartz
Created April 1, 2013 20:34
Show Gist options
  • Save jasonbartz/5287530 to your computer and use it in GitHub Desktop.
Save jasonbartz/5287530 to your computer and use it in GitHub Desktop.
Geo without a database
from django.contrib.gis.gdal import DataSource
from django.contrib.gis.gdal.geometries import Point
# Get the DC voting districts shape (from the Census Bureau)
ds = DataSource('/path/to/my/shapefiles/tl_2012_11_vtd10/tl_2012_11_vtd10.shp')
# Grab the layer that contains all of the shape data
layer = ds[0]
def get_shape_contains(layer, geometry):
# Iterate over every shape
for row in layer:
# If positive match (geometry is in shape), return
# NOTE: This logic returns on first match
if row.geom.contains(geometry):
return(row)
# GDAL geometries require a WKT
wapo_wkt = 'POINT (-77.0349768 38.9040006)'
# Create a GDAL point from the WKT string
wapo = Point(wapo_wkt)
# Get the shape
shape_that_wapo_is_in = get_shape_contains(layer, wapo)
#Print all of the fields
print({name: shape_that_wapo_is_in.get(name) for name in shape_that_wapo_is_in.fields})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment