# Geopandas one line of code to calculate the area of each province/state

created at 06-02-2022 views: 9

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.

## 1. preparation¶

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:

• Windows environment Open Cmd (Start-Run-CMD).
• MacOS environment Open Terminal (command+space to enter Terminal).
• If you are using the VSCode editor or Pycharm, you can directly use the Terminal at the bottom of the interface.

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.

## 2. basic use¶

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.

## 3. plot and calculate the area of each province¶

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

# 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.

created at:06-02-2022