Loading and Querying Datasets
Mundipy supports loading and querying any vector dataset supported by GDAL and tables from PostGIS. This includes Shapefiles, GeoJSON, FlatGeobufs, and KMLs, with a full list of drivers available here.
Loading vector datasets
Files from disk
Passing a filename as the first argument loads a vector file from disk:
from mundipy.dataset import Dataset
ds = Dataset('starbucks_in_seattle.shp')
print('Loaded %d coffeeshops' % len(ds))
# -> Loaded 308 coffeeshops
Files over http/https
Mundipy will automatically download relevant parts of a remote file when you pass a URL:
from mundipy.dataset import Dataset
ds = Dataset('https://pub-f49c63ed10ee494eb0deeb568b66f33f.r2.dev/trails.fgb')
print('Loaded %d trails' % len(ds))
# -> Loaded 5885 trails
PostGIS table
To download from a PostGIS table, pass the connection URL along with the table name:
from mundipy.dataset import Dataset
ds = Dataset({
'url': "postgresql://postgres@localhost:5432/postgres",
'table': 'nyc_boroughs'
})
print('Loaded %d boroughs' % len([ borough for borough in ds ]))
# -> Loaded 5 boroughs
If a loaded dataset is from PostGIS or is a Shapefile or FlatGeobuf, we can use the spatial index to query a subset of the data.
Querying datasets
Intersection
We can get a list of geometries that intersect with a given geometry by
using ds.intersects(geom)
.
intersects = ds.intersects(geom)
print(intersects)
# [<mundipy.geometry.Point object at 0x10e4f8160>, <mundipy.geometry.Point object at 0x10e435fc0>, <mundipy.geometry.Point object at 0x1115c1720>]
Nearest
This returns the geometry that is closest to the given geometry. Useful for nearest neighbor queries.
Returns None
if the dataset is empty.
pt = ds.nearest(geom)
print(pt)
# <mundipy.geometry.Point object at 0x10e4f8160>
Within
Returns a list of geometries that fall within a certain radius of the given geometry.
Useful for finding all of the points of interest near an area. Geometry does not have to be a Point
.
pois_in_walking_distance = ds.within(1200, geom)
print(pois_in_walking_distance)
# [<mundipy.geometry.Point object at 0x10e4f8160>, <mundipy.geometry.Point object at 0x10e435fc0>]
This is equivalent to calling ds.intersects(geom.buffer(1200))
.