GeoPandas is a third-party module based on pandas with special support for geographic data.
It inherits pandas.Series
and pandas.Dataframe
, and implements GeoSeries
and GeoDataFrame
classes, making it very convenient to manipulate and analyze plane geometric objects.
Before starting, you need to make sure that Python and pip have been successfully installed on your computer.
Please choose one of the following methods to enter the command to install dependencies:
Since geopandas involves many third-party dependencies, pip installation is cumbersome. So for this tutorial, I only recommend installing geopandas with conda:
conda install geopandas
The installation can be completed in one line.
Set coordinates to draw a simple graph:
import geopandas
from shapely.geometry import Polygon
p1 = Polygon([(0, 0), (1, 0), (1, 1)])
p2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
p3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
g = geopandas.GeoSeries([p1, p2, p3])
# g:
# result:
# 0 POLYGON ((0 0, 1 0, 1 1, 0 0))
# 1 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
# 2 POLYGON ((2 0, 3 0, 3 1, 2 1, 2 0))
# dtype: geometry
The graph formed by these variables is as follows:
There is an important and powerful usage here, with the area
property, geopandas can directly return the area of these figures:
>>> print(g.area)
0 0.5
1 1.0
2 1.0
dtype: float64
Not only that, through the plot
property function, you can also generate matplotlib plots directly.
>>> g.plot()
Through matplot's pyplot, the picture can be saved:
import matplotlib.pyplot as plt
g.plot()
plt.savefig("test.png")
After learning the above basic usage, we can do simple map drawing and area calculation.
In addition, its biggest highlight is that it can be read such as ESRI shapefile
(a non-topological simple format for storing the geometric position and attribute information of geographic features) through Fiona
(the underlying implementation, the user does not need to manage).
import geopandas
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
maps = geopandas.read_file('1.shx')
# The format of the data read is similar to
# geometry
# 0 POLYGON ((1329152.341 5619034.278, 1323327.591...
# 1 POLYGON ((-2189253.375 4611401.367, -2202922.3...
# 2 POLYGON ((761692.092 4443124.843, 760999.873 4...
# 3 POLYGON ((-34477.046 4516813.963, -41105.128 4...
# …
maps.plot()
plt.savefig("test.png")
As shown in the code, through read_file
you can read shx
, gpkg
, geojson
and other data. The figure read out is as follows:
Similarly, this shapefile
is a provincial administrative region, and each provincial administrative region is divided into a block, so the area occupied by each provincial administrative region can be calculated in one line:
print(maps.area)
# 0 4.156054e+11
# 1 1.528346e+12
# 2 1.487538e+11
# 3 4.781135e+10
# 4 1.189317e+12
# 5 1.468277e+11
# 6 1.597052e+11
# 7 9.770609e+10
# 8 1.385692e+11
# 9 1.846538e+11
# 10 1.015979e+11
# ... ...
This concludes our article.