BBBikeReader.read_shp_zip¶
-
BBBikeReader.
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 shapefile of a geographic region.
- Parameters
subregion_name (str) – name of a geographic region (case-insensitive) available on BBBike’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; when
pickle_it=True
, return a tuple of the dictionary and an absolute path to the pickle file- Return type
dict or tuple or None
Examples:
>>> import os >>> from pydriosm.reader import BBBikeReader >>> bbbike_reader = BBBikeReader() >>> sr_name = 'Birmingham' >>> dat_dir = "tests" >>> birmingham_shp = bbbike_reader.read_shp_zip(sr_name, data_dir=dat_dir, ... verbose=True) Confirmed to download .shp.zip data of the following geographic region(s): Birmingham ? [No]|Yes: yes Downloading "Birmingham.osm.shp.zip" to "\tests" ... Done. Extracting all of "Birmingham.osm.shp.zip" to "\tests" ... In progress ... Done. Parsing "\tests\Birmingham-shp\shape" ... Done. >>> print(list(birmingham_shp.keys())) ['buildings', 'landuse', 'natural', 'places', 'points', 'pofw', 'pois', 'railways'] >>> birmingham_railways_shp = birmingham_shp['railways'] >>> print(birmingham_railways_shp.head()) osm_id ... geometry 0 740 ... LINESTRING (-1.81789 52.57010, -1.81793 52.569... 1 2148 ... LINESTRING (-1.87319 52.50555, -1.87271 52.505... 2 2950000 ... LINESTRING (-1.87941 52.48138, -1.87960 52.481... 3 3491845 ... LINESTRING (-1.74060 52.51858, -1.73942 52.518... 4 3981454 ... LINESTRING (-1.77475 52.52284, -1.77449 52.522... [5 rows x 4 columns] >>> layer_name = 'roads' >>> feat_name = None >>> birmingham_roads_shp = bbbike_reader.read_shp_zip(sr_name, layer_name, ... feat_name, dat_dir, ... rm_extracts=True, ... verbose=True) Parsing "\tests\Birmingham-shp\shape\roads.shp" ... Done. Deleting the extracts "\tests\Birmingham-shp" ... Done. >>> print(list(birmingham_roads_shp.keys())) ['roads'] >>> print(birmingham_roads_shp['roads'].head()) osm_id ... geometry 0 37 ... LINESTRING (-1.82675 52.55580, -1.82646 52.555... 1 38 ... LINESTRING (-1.81541 52.54785, -1.81475 52.547... 2 41 ... LINESTRING (-1.81931 52.55219, -1.81860 52.552... 3 42 ... LINESTRING (-1.82492 52.55504, -1.82309 52.556... 4 45 ... LINESTRING (-1.82121 52.55389, -1.82056 52.55432) [5 rows x 8 columns] >>> lyr_names = ['railways', 'waterways'] >>> feat_names = ['rail', 'canal'] >>> bham_rw_rc_shp = bbbike_reader.read_shp_zip( ... sr_name, lyr_names, feat_names, dat_dir, rm_extracts=True, ... rm_shp_zip=True, verbose=True) Extracting from "Birmingham.osm.shp.zip" the following layer(s): 'railways' 'waterways' to "\tests" ... In progress ... Done. Parsing "\tests\Birmingham-shp\shape" ... Done. Deleting the extracts "\tests\Birmingham-shp" ... Done. Deleting "tests\Birmingham.osm.shp.zip" ... Done. >>> print(list(bham_rw_rc_shp.keys())) ['railways', 'waterways'] >>> bham_rw_rc_shp_railways = bham_rw_rc_shp['railways'] >>> print(bham_rw_rc_shp_railways[['fclass', 'name']].head()) fclass name 0 rail Cross-City Line 1 rail Cross-City Line 2 rail None 3 rail Birmingham to Peterborough Line 4 rail Freight Line >>> bham_rw_rc_shp_waterways = bham_rw_rc_shp['waterways'] >>> print(bham_rw_rc_shp_waterways[['fclass', 'name']].head()) fclass name 2 canal Birmingham and Fazeley Canal 8 canal Birmingham and Fazeley Canal 9 canal Birmingham Old Line Canal Navigations - Rotton P 10 canal Oozells Street Loop 11 canal Worcester & Birmingham Canal