parse_layer_shp¶
-
pydriosm.reader.
parse_layer_shp
(path_to_layer_shp, feature_names=None, crs=None, save_fclass_shp=False, driver='ESRI Shapefile', ret_path_to_fclass_shp=False, **kwargs)[source]¶ Parse a layer of OSM shapefile data.
- Parameters
path_to_layer_shp (str or list) – absolute path(s) to one (or multiple) shapefile(s)
feature_names (str or list or None) – class name(s) of feature(s), defaults to
None
crs (dict) – specification of coordinate reference system; if
None
(default), checkspecify_shp_crs()
save_fclass_shp (bool) – (when
fclass
is notNone
) whether to save data of thefclass
as shapefile, defaults toFalse
driver (str) – the OGR format driver, defaults to
'ESRI Shapefile'
; see also thedriver
parameter of geopandas.GeoDataFrame.to_file()ret_path_to_fclass_shp (bool) – (when
save_fclass_shp
isTrue
) whether to return the path to the saved data offclass
, defaults toFalse
kwargs – optional parameters of
read_shp_file()
- Returns
parsed shapefile data
- Return type
geopandas.GeoDataFrame
Examples:
>>> import os >>> from pyhelpers.dir import cd, delete_dir >>> from pydriosm.reader import GeofabrikDownloader >>> from pydriosm.reader import parse_layer_shp, unzip_shp_zip >>> geofabrik_downloader = GeofabrikDownloader() >>> sr_name = 'Rutland' >>> path_to_rutland_shp_zip = geofabrik_downloader.download_osm_data( ... sr_name, osm_file_format=".shp", download_dir="tests", ... confirmation_required=False, ret_download_path=True) >>> # Extract the downloaded .shp.zip file >>> rutland_shp_dir = unzip_shp_zip(path_to_rutland_shp_zip, ret_extract_dir=True) >>> path_to_railways_shp = cd(rutland_shp_dir, "gis_osm_railways_free_1.shp") >>> rutland_railways_shp = parse_layer_shp(path_to_railways_shp) >>> print(rutland_railways_shp.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] >>> rutland_railways_rail, path_to_rutland_railways_rail = parse_layer_shp( ... path_to_railways_shp, feature_names='rail', save_fclass_shp=True, ... ret_path_to_fclass_shp=True) >>> print(rutland_railways_rail.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] >>> print(os.path.relpath(path_to_rutland_railways_rail)) tests\rutland-latest-free-shp\railways\gis_osm_railways_free_1_rail.shp >>> # Delete the extracted data files >>> delete_dir(rutland_shp_dir, verbose=True) The directory "\tests\rutland-latest-free-shp" is not empty. Confirmed to delete it? [No]|Yes: yes Deleting "\tests\rutland-latest-free-shp" ... Done. >>> # Delete the downloaded shapefile >>> os.remove(path_to_rutland_shp_zip)