# Python 一行语句算出每个省的面积—geopandas

GeoPandas是一个基于pandas，针对地理数据做了特别支持的第三方模块。

## 1.准备

Windows环境下打开Cmd(开始—运行—CMD)，苹果系统环境下请打开Terminal(command+空格输入Terminal)，准备开始输入命令安装依赖。

conda install geopandas

## 2.基本使用

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

>>> print(g.area)
0    0.5
1    1.0
2    1.0
dtype: float64

>>> g.plot()

import matplotlib.pyplot as plt
g.plot()
plt.savefig("test.png")

## 3.绘制并算出每个省的面积

import geopandas
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
# 读取的数据格式类似于
#                                             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")

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