GeofabrikReader.read_shp_zip¶
-
GeofabrikReader.
read_shp_zip
(subregion_name, layer_names=None, feature_names=None, data_dir=None, update=False, download_confirmation_required=True, pickle_it=False, ret_pickle_path=False, rm_extracts=False, rm_shp_zip=False, verbose=False)[source]¶ Read a .shp.zip data file of a geographic region.
- Parameters
subregion_name (str) – name of a region/subregion (case-insensitive) available on Geofabrik’s free download server
layer_names (str or list or None) – name of a .shp layer, e.g. ‘railways’, or names of multiple layers; if
None
(default), all available layersfeature_names (str or list or None) – name of a feature, e.g. ‘rail’, or names of multiple features; if
None
(default), all available featuresdata_dir (str or None) – directory where the .shp.zip data file is located/saved; if
None
, the default directoryupdate (bool) – whether to check to update pickle backup (if available), defaults to
False
download_confirmation_required (bool) – whether to ask for confirmation before starting to download a file, defaults to
True
pickle_it (bool) – whether to save the .shp data as a .pickle file, defaults to
False
ret_pickle_path (bool) – whether to return an absolute path to the saved pickle file (when
pickle_it=True
)rm_extracts (bool) – whether to delete extracted files from the .shp.zip file, defaults to
False
rm_shp_zip (bool) – whether to delete the downloaded .shp.zip file, defaults to
False
verbose (bool or int) – whether to print relevant information in console as the function runs, defaults to
False
- Returns
dictionary of the shapefile data, with keys and values being layer names and tabular data (in the format of geopandas.GeoDataFrame), respectively
- Return type
dict or None
Example:
>>> from pydriosm.reader import GeofabrikReader >>> geofabrik_reader = GeofabrikReader() >>> sr_name = 'Rutland' >>> dat_dir = "tests" >>> rutland_shp = geofabrik_reader.read_shp_zip(sr_name, data_dir=dat_dir) Confirmed to download .shp.zip data of the following geographic region(s): Rutland ? [No]|Yes: yes >>> print(list(rutland_shp.keys())) ['buildings', 'traffic', 'water', 'roads', 'places', 'pofw', 'waterways', 'pois', 'landuse', 'transport', 'natural', 'railways'] >>> rutland_shp_railways = rutland_shp['railways'] >>> print(rutland_shp_railways.head()) osm_id code ... tunnel geometry 0 2162114 6101 ... F LINESTRING (-0.45281 52.69934, -0.45189 52.698... 1 3681043 6101 ... F LINESTRING (-0.65312 52.57308, -0.65318 52.572... 2 3693985 6101 ... F LINESTRING (-0.73234 52.67821, -0.73191 52.678... 3 3693986 6101 ... F LINESTRING (-0.61731 52.61323, -0.62419 52.614... 4 4806329 6101 ... F LINESTRING (-0.45769 52.70352, -0.45654 52.702... [5 rows x 8 columns] >>> sr_layer = 'transport' >>> rutland_shp_transport = geofabrik_reader.read_shp_zip( ... sr_name, sr_layer, data_dir=dat_dir, verbose=True, rm_extracts=True) Deleting the extracts "\tests\rutland-latest-free-shp" ... Done. >>> print(list(rutland_shp_transport.keys())) ['transport'] >>> print(rutland_shp_transport['transport'].head()) osm_id code fclass name geometry 0 472398147 5621 bus_stop None POINT (-0.73213 52.66974) 1 502322073 5621 bus_stop Fife Close POINT (-0.50962 52.66052) 2 502322075 5621 bus_stop Fife Close POINT (-0.50973 52.66058) 3 502322076 5621 bus_stop Aberdeen Close POINT (-0.51039 52.65817) 4 502322077 5621 bus_stop Arran Road (South End) POINT (-0.50973 52.65469) >>> feat_name = 'bus_stop' >>> rutland_shp_transport_bus_stop = geofabrik_reader.read_shp_zip( ... sr_name, sr_layer, feat_name, dat_dir, verbose=True, rm_extracts=True) Extracting from "rutland-latest-free.shp.zip" the following layer(s): 'transport' to "\tests\rutland-latest-free-shp" ... In progress ... Done. Deleting the extracts "\tests\rutland-latest-free-shp" ... Done. >>> print(list(rutland_shp_transport_bus_stop.keys())) ['transport'] >>> print(rutland_shp_transport_bus_stop['transport'].fclass.unique()) ['bus_stop'] >>> sr_layers = ['traffic', 'roads'] >>> feat_names = ['parking', 'trunk'] >>> rutland_shp_tr_pt = geofabrik_reader.read_shp_zip( ... sr_name, sr_layers, feat_name, dat_dir, verbose=True, ... rm_extracts=True, rm_shp_zip=True) Extracting from "rutland-latest-free.shp.zip" the following layer(s): 'traffic' 'roads' to "\tests\rutland-latest-free-shp" ... In progress ... Done. Deleting the extracts "\tests\rutland-latest-free-shp" ... Done. Deleting "tests\rutland-latest-free.shp.zip" ... Done. >>> print(list(rutland_shp_tr_pt.keys())) ['traffic', 'roads'] >>> selected_columns = ['fclass', 'name', 'geometry'] >>> rutland_shp_tr_pt_traffic = rutland_shp_tr_pt['traffic'] >>> print(rutland_shp_tr_pt_traffic[selected_columns].head()) fclass name geometry 0 parking None POLYGON ((-0.66704 52.71108, -0.66670 52.71121... 1 parking None POLYGON ((-0.78712 52.71974, -0.78700 52.71991... 2 parking None POLYGON ((-0.70368 52.65567, -0.70362 52.65587... 3 parking None POLYGON ((-0.63381 52.66442, -0.63367 52.66441... 4 parking None POLYGON ((-0.62814 52.64093, -0.62701 52.64169... >>> rutland_shp_tr_pt_roads = rutland_shp_tr_pt['roads'] >>> print(rutland_shp_tr_pt_roads[selected_columns].head()) fclass name geometry 0 trunk None LINESTRING (-0.72461 52.59642, -0.72452 52.596... 1 trunk Glaston Road LINESTRING (-0.64671 52.59353, -0.64590 52.593... 3 trunk Orange Street LINESTRING (-0.72293 52.58899, -0.72297 52.588... 11 trunk Ayston Road LINESTRING (-0.72483 52.59610, -0.72493 52.596... 12 trunk London Road LINESTRING (-0.72261 52.58759, -0.72264 52.587...