First Love 1994

Poetry By Jenn Powers. “First Love 1994” is published by Goat in Scene & Heard (SNH).

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Python Geopandas package for shapefile management

While working with geospatial data, I came across an important Python package called Geopandas, and I’d like to discuss the capabilities and functionalities of the Geopandas package in manipulating geographical data with some examples. Geopandas combines the ability of Pandas & Shapely for manipulating geographic data mainly vector data such as points, polylines, & polygons. This package is simple to use and has a wide range of features. It can also read and write vector data in a variety of formats, including the ESRI shapefile format (.shp), and GeoJSON.

Therefore, this blog attempts to introduce the following key topics and Geopands package functionalities in manipulating geospatial data (shapefile):

Add & set conda-forge channel:

Install Geopandas package with python-3:

Import the Geopandas library:

Check the Geopandas version:

You have successfully installed Geopandas if you receive the version number. Finally, for reading & writing shapefiles, you can use any Python Notebook or IDE, such as Jupyter-Notebook & Spyder.

Import the matplotlib library for visualization purposes:

Inspect the attribute table of the data by typing:

Similarly, to display the last 5 rows of the data frame, type:

Alternatively, you can view a specific section of the data frame by defining the row numbers, but keep in mind that Python counts starting from zero. For example, to display only the 3rd & 4th rows, you can type:

To find out the size of the data frame, type:

Output: (12, 8)
As can be seen, the data frame has 12 rows and 6 columns

To obtain the row numbers or length of the data frame, type:

Output: 12

To print the column names, type:

To list the column’s contents:

array(['ABBAY', 'AWASH', 'AYSHA', 'BARO AKOBO', 'DENAKIL', 'GENALE DAWA', 'MEREB GASH', 'OGADEN', 'OMO GIBE', 'RIFT VALLY', 'TEKEZE', 'WABI SHEBELE'], dtype=object)

To know how the data is represented or stored in the data frame, type:

geopandas.geodataframe.GeoDataFrame In this example, each vector data is represented as GeoDataFrame

The geometry the column is the most essential of the columns & it contains the x-y coordinate values of the vector data. For instance, you can look at the values using .geometry function:

You can use the following function to extract x-y coordinate values of a given polygon, In this case, the first polygon:

Let’s look at the first ten x-y coordinate values of the first polygon:

You can use the geom type function to get information about the geometry type (polygon, line, or point) of each data recorded in the data frame:

array([33.00541327, 3.40510906, 47.99567693, 14.88539753])

The shapefile ranges from 33.00541327 to 47.99567693 longitudes & 3.40510906 to 14.88539753 latitudes

You may get a quick overview of the shapefile by using the geometry values. For instance, to view the first data, you can use the code below:

Let’s now make a simple plot of the shapefile using plot() function:

Let’s colorize the map with the cmap function:

You can colorize the map based on a specific column, in this case, let’s use the BASINNAME column:

You can also use the figsize option to change the plot size:

You can apply an edge color to each polygon in the shapefile using edgecolor function:

You may also put a legend in the plot:

However, using the matplotlib functions is a more convenient way to make plots:

You can use the script underneath to add grids to the plot:

Here, instead of dashdot you can use dotted, dashed, solid

You can remove the grids & boundary lines by using the code below:

You can also change the legend’s position:

Plots can be saved as image files using the plt. savefig() function:

Making subplots:

It is critical to remember that the coordinate systems of the two shapefiles must be identical. The .crs function can be used to verify the coordinate system of the two shapefiles:

You can see that they both use the WGS-84 geographic coordinate system (EPSG:4326)

Now, you can plot the overlaid map using the following code:

Some examples of widely used spatial reference systems:

Re-projecting from EPSG:4326 to EPSG:32637:

Or simply type:

Let’s review the coordinate system of the modified shapefile:

As you can see, it has been converted to the UTM projection. You can also confirm by inspecting the shapefile boundaries:

array([-161882.56221284, 376388.12499996, 1495282.8685069 , 1645935.62500012])

Let’s have a look at the new map under the projected UTM reference system:

Then, use the .dissolve() function, which works based on a unique attribute value

Let’s view the resulting geodataframe:

Now we can plot & see the resulting boundary map:

To do that type the following:

Now, let’s plot the clipped Abbay basin:

You can also use the intersection tool to clip data to a specified region, for instance, let’s load the major world river shapefile, & clip it to the Ethiopian region.

First, load the major world rivers:

Let’s have a look at the loaded map:

Let’s plot the clipped major rivers over Ethiopia:

You can export the geopandas data frame mainly to ESRI shapefile format (.shp) & GeoJSON (.json) file formats.

To export your geopandas data frame as a shapefile:

To export your geopandas data frame as a GeoJSON file:

Well done! 🥇🥇🥇

I hope this blog has given you the basics of the geopandas package; if you find it useful in your work, please share your thoughts.

Add a comment

Related posts:

How Thing 1 and Thing 2 led to an epiphany

One day in the break room at work I noticed the tattoo on the leg of one of the interns in our call center. She had Dr. Seuss’ Thing 1 and Thing 2 running up the back of her calf. It was fun and…

Wisdom stolen from a reverend

On Saturday morning I went to church. It’s unusual to see me outside of the house in the mornings, never mind at church. I was baptised Catholic (Irish mother), but I stopped going to church just…

DDEX Launches Closed Beta API

Over the past several months, our team has worked tirelessly to develop new features, upgrade, and refine the DDEX exchange platform. Countless coffee cups and many standup meetings later, we are…