A Pythonista's BFF: __geo_interface__
With __geo_interface__
in your toolbox, as long as you can read spatial data, you can write GeoJSON
import shapefile
def features(data):
sf = shapefile.Reader(data)
fields = sf.fields[1:]
field_names = [field[0] for field in fields]
for r in sf.shapeRecords():
attr = dict(zip(field_names, r.record))
geom = r.shape.__geo_interface__
yield dict(geometry=geom, properties=attr)
>>> my_features = features(my_awesome_shapefile.shp)
>>> my_features.next()
# start with a shapefile, end with a geoJSON representation of each feature
See also: Shapely, fiona, mapnik... even
arcpy*
$ cat my_awesome_document.txt | wc -l
# returns number of lines in my_awesome_document.txt
(Feel free to just smile & nod here)
$ fio cat my_awesome_shapefile.shp | wc -l
# returns number of spatial features in my_awesome_shapefile.shp
$ fio cat my_red_shapefile.shp my_blue_shapefile.shp \
| fio load my_purple_shapefile.shp
joins two shapefiles together and creates a new
shapefile
$ fio cat boring_shapefile.shp | magical_geoprocessing_script.py \
| fio load shiny_new_shapefile.shp
Pipe arbitrary input through Python scripts & save the output as
new spatial data
Python library fiona's built-in CLI "fio"
cat
works like unix-y cat
, with added
spatial goodness
(For the raster nerds, check out Rasterio, which has a CLI "rio")
Stick spatial data in a repo for the same reason you do it to your code.
*(right?)
Git for shapefiles & databases.
$ geogig init
$ geogig import my_awesome_shapefile.shp
$ geogig add
$ geogig commit -m "initial commit"
$ geogig status
On branch master
# blah blah etc...
# basically the git we all know & love... PLUS SPATIAL.
Keep on eye on them. Start playing around now.