read_shp_file¶
-
pydriosm.reader.
read_shp_file
(path_to_shp, method='geopandas', **kwargs)[source]¶ Parse a shapefile.
- Parameters
path_to_shp – absolute path to a .shp data file
method (str) – the method used to read the .shp file; if
'geopandas'
(default), use the geopandas.read_file() method, for otherwise use shapefile.Reader()kwargs – optional parameters of geopandas.read_file()
- Type
str
- Returns
data frame of the .shp data
- Return type
pandas.DataFrame or geopandas.GeoDataFrame
Examples:
>>> from pyhelpers.dir import cd, delete_dir >>> from pydriosm.reader import GeofabrikDownloader, unzip_shp_zip, read_shp_file >>> geofabrik_downloader = GeofabrikDownloader() >>> sr_name = 'Rutland' >>> file_fmt = ".shp" >>> dwnld_dir = "tests" >>> path_to_rutland_shp_zip = geofabrik_downloader.download_osm_data( ... sr_name, file_fmt, dwnld_dir, ret_download_path=True) Confirmed to download .shp.zip data of the following geographic region(s): Rutland ? [No]|Yes: yes >>> path_to_rutland_shp_dir = unzip_shp_zip(path_to_rutland_shp_zip, ... ret_extract_dir=True) >>> railways_shp_filename = "gis_osm_railways_free_1.shp" >>> path_to_rutland_railways_shp = cd( ... path_to_rutland_shp_dir, railways_shp_filename) >>> rutland_railways_shp = read_shp_file(path_to_rutland_railways_shp, ... method='gpd') >>> 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_shp_ = read_shp_file(path_to_rutland_railways_shp, ... method='pyshp') >>> print(rutland_railways_shp_.head()) osm_id code ... coords shape_type 0 2162114 6101 ... [(-0.4528083, 52.6993402), (-0.4518933, 52.698... 3 1 3681043 6101 ... [(-0.6531215, 52.5730787), (-0.6531793, 52.572... 3 2 3693985 6101 ... [(-0.7323403, 52.6782102), (-0.7319059, 52.678... 3 3 3693986 6101 ... [(-0.6173072, 52.6132317), (-0.6241869, 52.614... 3 4 4806329 6101 ... [(-0.4576926, 52.7035194), (-0.4565358, 52.702... 3 [5 rows x 9 columns] >>> delete_dir(path_to_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)