SHP.read_shp¶
- classmethod SHP.read_shp(shp_pathname, engine='geopandas', emulate_gpd=False, **kwargs)[source]¶
Read a shapefile.
- Parameters:
shp_pathname (str) – pathname of a shape format file (.shp)
engine (str) – method used to read shapefiles; options include:
'pyshp'and'geopandas'(default) (or'gpd') this function by default relies on shapefile.reader(); whenengine='geopandas'(orengine='gpd'), it relies on geopandas.read_file();emulate_gpd (bool) – whether to emulate the data format produced by geopandas.read_file() when
engine='pyshp'.kwargs – [optional] parameters of the function geopandas.read_file() or shapefile.reader()
- Returns:
data frame of the shapefile data
- Return type:
pandas.DataFrame | geopandas.GeoDataFrame
Note
- If
engineis set to be'geopandas'(or'gpd'), it requires that GeoPandas is installed.
- If
Examples:
>>> from pydriosm.reader._shp import SHP >>> from pydriosm.downloader import BBBikeDownloader >>> from pyhelpers.dirs import cd, delete_dir >>> import os >>> import glob >>> # Download the shapefile data of London as an example >>> subregion_name = 'birmingham' >>> osm_file_format = ".shp" >>> download_dir = "tests/osm_data" >>> bbd = BBBikeDownloader() >>> bbd.download_data(subregion_name, osm_file_format, download_dir, verbose=True) Proceed to download data in the format '.shp.zip' for the following geographic (sub... "Birmingham" to "./tests/osm_data/birmingham/" ? [No]|Yes: yes Downloading "Birmingham.osm.shp.zip" 100%|██████████| 79.1M/79.1M | 17.4MB/s ... Saving "Birmingham.osm.shp.zip" to "./tests/osm_data/birmingham/" ... Done. >>> bham_shp_zip = bbd.data_paths[0] >>> os.path.relpath(bham_shp_zip) 'tests\osm_data\birmingham\Birmingham.osm.shp.zip' >>> # Extract all >>> bham_shp_dir = SHP.unzip_shp_zip(bham_shp_zip, ret_extract_dir=True) >>> # Get the pathname of the .shp data of 'railways' >>> path_to_railways_shp = glob.glob( ... cd(bham_shp_dir, "Birmingham-shp", "shape", "*railways*.shp"))[0] >>> os.path.relpath(path_to_railways_shp) # Check the pathname of the .shp file 'tests\osm_data\birmingham\Birmingham-osm-shp\Birmingham-shp\shape\railways.shp' >>> # Read the data of 'railways' >>> bham_railways = SHP.read_shp(path_to_railways_shp) >>> bham_railways.head() osm_id ... geometry 0 740 ... LINESTRING (-1.81789 52.5701, -1.81793 52.5698... 1 2148 ... LINESTRING (-1.87303 52.50542, -1.8727 52.5051... 2 2950000 ... LINESTRING (-1.87933 52.48138, -1.87962 52.481... 3 3491845 ... LINESTRING (-1.7406 52.51858, -1.73942 52.5186... 4 3981454 ... LINESTRING (-1.77412 52.52249, -1.77376 52.522... [5 rows x 4 columns] >>> # Set `emulate_gpd=True` to return data of similar format to what GeoPandas does >>> bham_railways_ = SHP.read_shp( ... path_to_railways_shp, engine='pyshp', emulate_gpd=True) >>> bham_railways_.head() osm_id ... geometry 0 740 ... LINESTRING (-1.8178905 52.5700974, -1.8179287 ... 1 2148 ... LINESTRING (-1.873028 52.5054182, -1.8726964 5... 2 2950000 ... LINESTRING (-1.8793303 52.4813778, -1.8796237 ... 3 3491845 ... LINESTRING (-1.7406017 52.5185831, -1.7394216 ... 4 3981454 ... LINESTRING (-1.7741212 52.5224935, -1.7737563 ... [5 rows x 4 columns] >>> # Check the data types of `london_railways` and `london_railways_` >>> railways_data = [bham_railways, bham_railways_] >>> list(map(type, railways_data)) [geopandas.geodataframe.GeoDataFrame, pandas.DataFrame] >>> # Check the geometry data of `london_railways` and `london_railways_` >>> geom1, geom2 = map(lambda x: x['geometry'].map(lambda y: y.wkb), railways_data) >>> geom1.equals(geom2) True >>> # Delete the download/data directory >>> delete_dir(bbd.download_dir, verbose=True) To delete the directory "./tests/osm_data/" (Not empty) ? [No]|Yes: yes Deleting "./tests/osm_data/" ... Done.