Inspect Shape files (.shp)¶
A Shapefile is a widely used vector data format for storing points, lines, or polygons together with attribute information. A shapefile only consists of a single vector layer. Although often referred to as a single file, a Shapefile actually consists of multiple files that must always be kept together.
All files share the same base name and together form one vector dataset. Removing or renaming individual files may make the dataset unusable.
Required components
.shp– stores the geometry data (coordinates).dbf– stores the attribute table (non-spatial information such as names or values).shx– index file linking geometries to attributes and enabling fast access
Optional but common components
.prj– stores coordinate reference system (CRS) information
1. Import required packages¶
GeoKit is imported to provide the required functionality for working with vector data.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import geokit.core.vector
import geokit.core.geom
import pathlib
from geokit.core.get_test_data import get_test_data
# Path to shapefile folder
gadm36_DEU_1_path = get_test_data(
file_name="gadm36_DEU_1.shp",
)
# Load vector dataset from a Shapefile and reproject to EPSG:4326
federal_states_DE = geokit.core.vector.extractFeatures(gadm36_DEU_1_path)
# show dataset
federal_states_DE
| geom | GID_0 | NAME_0 | GID_1 | NAME_1 | VARNAME_1 | NL_NAME_1 | TYPE_1 | ENGTYPE_1 | CC_1 | HASC_1 | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | MULTIPOLYGON (((8.70802116 47.6895752,8.707655... | DEU | Germany | DEU.1_1 | Baden-Württemberg | None | None | Land | State | 08 | DE.BW |
| 1 | POLYGON ((9.74066353 47.55353546,9.74057579 47... | DEU | Germany | DEU.2_1 | Bayern | Bavaria | None | Freistaat | None | 09 | DE.BY |
| 2 | POLYGON ((13.17135715 52.3977623,13.17135334 5... | DEU | Germany | DEU.3_1 | Berlin | None | None | Land | State | 11 | DE.BE |
| 3 | MULTIPOLYGON (((12.26715565 52.2313118,12.2704... | DEU | Germany | DEU.4_1 | Brandenburg | None | None | Land | State | 12 | DE.BR |
| 4 | MULTIPOLYGON (((8.71142387 53.04463196,8.71033... | DEU | Germany | DEU.5_1 | Bremen | None | None | Freie Hansestadt | State | 04 | DE.HB |
| 5 | MULTIPOLYGON (((10.21060085 53.52004623,10.211... | DEU | Germany | DEU.6_1 | Hamburg | None | None | Freie und Hansestadt | State | 02 | DE.HH |
| 6 | MULTIPOLYGON (((8.68058777 49.62247086,8.68134... | DEU | Germany | DEU.7_1 | Hessen | Hesse | None | Land | State | 06 | DE.HE |
| 7 | MULTIPOLYGON (((11.4958334 54.03180695,11.4958... | DEU | Germany | DEU.8_1 | Mecklenburg-Vorpommern | Mecklenburg-West Pomerania | None | Land | State | 13 | DE.MV |
| 8 | MULTIPOLYGON (((6.76194477 53.61875153,6.76194... | DEU | Germany | DEU.9_1 | Niedersachsen | Lower Saxony | None | Land | State | 03 | DE.NI |
| 9 | POLYGON ((8.05965614 50.69504166,8.0413866 50.... | DEU | Germany | DEU.10_1 | Nordrhein-Westfalen | North Rhine-Westphalia | None | Land | State | 05 | DE.NW |
| 10 | POLYGON ((6.6504283 49.54951096,6.65105438 49.... | DEU | Germany | DEU.11_1 | Rheinland-Pfalz | Rhineland-Palatinate | None | Land | State | 07 | DE.RP |
| 11 | POLYGON ((6.84392643 49.15551376,6.84085464 49... | DEU | Germany | DEU.12_1 | Saarland | None | None | Land | State | 10 | DE.SL |
| 12 | POLYGON ((14.79049587 50.82408524,14.79012299 ... | DEU | Germany | DEU.14_1 | Sachsen | Saxony | None | Freistaat | State | 14 | DE.SN |
| 13 | MULTIPOLYGON (((11.44027328 51.19976044,11.441... | DEU | Germany | DEU.13_1 | Sachsen-Anhalt | Saxony-Anhalt | None | Land | State | 15 | DE.ST |
| 14 | MULTIPOLYGON (((9.53010368 53.67519379,9.53016... | DEU | Germany | DEU.15_1 | Schleswig-Holstein | None | None | Land | State | 01 | DE.SH |
| 15 | POLYGON ((11.70222759 50.40244293,11.70159817 ... | DEU | Germany | DEU.16_1 | Thüringen | Thuringia | None | Freistaat | State | 16 | DE.TH |
Visualize the Geometries¶
Use the drawGeoms function of GeoKit to visualize all geometries stored in the geom column.
ax_handles = geokit.core.geom.drawGeoms(federal_states_DE, figsize=(6, 6))
Inspect the shape file¶
To inspect some of the metadata about the shapefile, use the vectorInfo() function. For example, you can inspect the spatial reference system, the number of geometries, and the names of the attributes.
vinfo = geokit.core.vector.vectorInfo(gadm36_DEU_1_path)
# show information about the vector dataset
print("COUNT", vinfo.count)
print("SRS", vinfo.srs)
print("ATTRIBUTES", vinfo.attributes)
COUNT 16
SRS GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AXIS["Latitude",NORTH],
AXIS["Longitude",EAST],
AUTHORITY["EPSG","4326"]]
ATTRIBUTES ['GID_0', 'NAME_0', 'GID_1', 'NAME_1', 'VARNAME_1', 'NL_NAME_1', 'TYPE_1', 'ENGTYPE_1', 'CC_1', 'HASC_1']