Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Geopandas to calculate the area of each province by Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how Python uses Geopandas to calculate the area of each province". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "how Python uses Geopandas to calculate the area of each province".

GeoPandas is a third-party module based on pandas and specially supported for geographic data.

It inherits pandas.Series and pandas.Dataframe and implements GeoSeries and GeoDataFrame classes, which makes it very convenient to manipulate and analyze plane geometric objects.

1. Prepare for

Before you begin, make sure that Python and pip are successfully installed on your computer.

Please choose any of the following ways to enter the command to install dependencies:

The 1.Windows environment opens Cmd (start-run-CMD).

The 2.MacOS environment opens Terminal (enter Terminal for command+ spaces).

3. If you are using a VSCode editor or Pycharm, you can use Terminal directly at the bottom of the interface.

Because geopandas involves many third-party dependencies, pip is cumbersome to install. So in this tutorial, I only recommend using conda to install geopandas:

Conda install geopandas

The installation can be completed with one line of statements.

two。 Basic use

Set the coordinates to draw simple graphics:

Import geopandasfrom shapely.geometry import Polygonp1 = 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]) # result:# 0 POLYGON ((0,10,11)) ) # 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 figures formed by these variables are as follows:

There is an important and powerful use here. Through the area property, geopandas can directly return the area of these drawings:

> print (g.area) 0 0.51 1.02 1.0dtype: float64

Not only that, you can also generate matplotlib diagrams directly through the plot property function.

> > g.plot ()

Through matplot's pyplot, you can save the picture:

Import matplotlib.pyplot as pltg.plot () plt.savefig ("test.png")

By learning the basic usage above, we can do simple mapping and area calculation.

3. Draw and calculate the area of each province

In addition, its biggest highlight is that it can be read through Fiona (low-level implementation, users do not need a tube), such as ESRI shapefile (a non-topological simple format for storing geometric location and attribute information of geographic elements).

Import geopandasimport matplotlib.pyplot as pltfrom shapely.geometry import Polygonmaps = geopandas.read_file ('1.shx') # reads data in a format similar to # geometry# 0 POLYGON ((1329152.341 5619034.278, 1323327.591. # 1 POLYGON (- 2189253.375 4611401.367,-220 2922.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, you can read shx, gpkg, geojson and other data through read_file. The figure read out is as follows:

Similarly, this shapefile is a provincial administrative region, and each provincial administrative district is divided into a block, so you can calculate the area of each provincial administrative district with 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#. The above is all the contents of the article "how Python uses Geopandas to calculate the area of each province". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report