read_shp_file¶
-
pydriosm.reader.
read_shp_file
(path_to_shp, method='pyshp', emulate_gpd=False, **kwargs)¶ Parse a shapefile.
- Parameters
path_to_shp – path to a .shp data file
method (str) – method used to read shapefiles; options include:
'pyshp'
(default) and'geopandas'
(or'gpd'
) ifmethod='geopandas'
(ormethod='gpd'
), this function relies on geopandas.read_file(); otherwise, it by default uses shapefile.Reader()emulate_gpd (bool) – whether to emulate the data format produced by geopandas.read_file(), when
method='pyshp'
.kwargs – optional parameters of geopandas.read_file() or shapefile.Reader()
- Type
str
- Returns
data frame of the .shp data
- Return type
pandas.DataFrame or geopandas.GeoDataFrame
Note
If
method
is set to be'geopandas'
(or'gpd'
), it requires availability of the package GeoPandas.Examples:
>>> import os >>> from pyhelpers.dir import cd, delete_dir >>> from pydriosm.reader import GeofabrikDownloader, unzip_shp_zip, read_shp_file >>> # Download the .shp.zip file of Rutland as an example >>> 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, verbose=True, ret_download_path=True) To download .shp.zip data of the following geographic region(s): Rutland ? [No]|Yes: yes Downloading "rutland-latest-free.shp.zip" to "tests\" ... Done. >>> path_to_rutland_shp_dir = unzip_shp_zip(path_to_shp_zip=path_to_rutland_shp_zip, ... ret_extract_dir=True) >>> # .shp data of 'railways' >>> railways_shp_filename = "gis_osm_railways_free_1.shp" >>> path_to_rutland_railways_shp = cd(path_to_rutland_shp_dir, railways_shp_filename) >>> # Set `method` to be 'gpd' or 'geopandas' >>> rutland_railways_shp = read_shp_file(path_to_rutland_railways_shp) >>> rutland_railways_shp.head() osm_id code ... coordinates 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] >>> # Set `emulate_gpd` to be True >>> rutland_railways_shp = read_shp_file(path_to_rutland_railways_shp, emulate_gpd=True) >>> rutland_railways_shp.head() osm_id code ... tunnel geometry 0 2162114 6101 ... F LINESTRING (-0.4528083 52.6993402, -0.4518933 ... 1 3681043 6101 ... F LINESTRING (-0.6531215 52.5730787, -0.6531793 ... 2 3693985 6101 ... F LINESTRING (-0.7323403000000001 52.6782102, -0... 3 3693986 6101 ... F LINESTRING (-0.6173071999999999 52.6132317, -0... 4 4806329 6101 ... F LINESTRING (-0.4576926 52.7035194, -0.4565358 ... [5 rows x 8 columns] >>> # Alternatively, set `method` to be 'gpd' to use GeoPandas >>> rutland_railways_shp_ = read_shp_file(path_to_rutland_railways_shp, method='gpd') >>> 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] >>> len(rutland_railways_shp) == len(rutland_railways_shp_) True >>> # Delete the extracted shapefiles >>> 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)