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 planar geometry objects.

1. Prepare

Before you start, make sure Python and PIP are successfully installed on your computer.

Please select one of the following ways to enter the command to install dependencies:

  1. In Windows, open Cmd (start – Run – Cmd).
  2. MacOS environment open Terminal (command+ space enter Terminal).
  3. If you are using VSCode editor or Pycharm, you can use Terminal at the bottom of the screen.

Because geopandas involves many third-party dependencies, PIP is cumbersome to install. Therefore, in this tutorial, I only recommend installing geopandas using Conda:

conda install geopandas
Copy the code

A single statement completes the installation.

2. Basic usage

Set the 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
Copy the code

These variables form the following graph:

An important and powerful use of geopandas is to return the area of these graphs directly through the area attribute:

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

Not only that, you can also generate matplotlib plots directly with the plot property function.

>>> g.plot()
Copy the code

With Matplot’s Pyplot, images can be saved:

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

Learn the basic usage above, we can carry out simple map drawing and area calculation.

3. Plot and calculate the area of each province

In addition, its biggest advantage is the ability to read things like ESRI ShapeFile (a simple non-topological format for storing information about the geometric locations and attributes of geographical elements) through Fiona(a low-level implementation that users don’t need to manage).

import geopandas import matplotlib.pyplot as plt from shapely.geometry import Polygon maps = Geopandas.read_file ('1.shx') # Access the data in a format 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")Copy the code

As shown in the code, read_file lets you read SHX, GPKG, GeoJSON, etc. The graph reads as follows:

Again, the shapefile is for a provincial district. Each provincial district is divided into a block, so you can calculate the area of each provincial district 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.597052 9.770609 e+10 e+11 # 7 # 8 1.385692 1.015979 1.846538 e+11 e+11 # 9 # 10 e+11 #... .Copy the code

That’s the end of our article. If you enjoyed today’s Python tutorial, remember the triple Link