articles
Gis AnalysisHome

How to read GIS files with Geopandas | Read spatial data files with Geopanda

Learn How to read GIS files,such as shapefiles and GeoJSON, with Geopandas,a Python library for working with geospatial data. This beginner-friendly tutorial covers everything from installation to basic file reading and manipulation, providing an easy-to-follow introduction to Geopandas' powerful capabilities.

article title

Read GIS files with Geopandas

In this short article we are going to show you how they can be accessed and read GIS files with geographic information through Geopands.

  1. Geopands is the main bookstore Python to work with spatial data. It is an essential tool to manipulate, transform, visualize and analyze data sets with a geographic component.
GeoPandas
Python Geopandas Library Logo

The essential and basic point to start using this fantastic Python library is to know how to read files with spatial data. Let’s see how we can perform these tasks with Geopands.

How to read spatial data files with Geopands?

Read space files with Geopands is equivalent to creating a GeoDataFrame object of Geopands containing the data of said file.

A GeoDataFrame object is a tabular data structure that contains at least one column with an object ( Geoseries ) that contains the geometry.

If you are not familiar with these concepts and want to delve into them, we invite you to review this introductory article on Geopands that we published earlier. It defines the concepts of GeoSeries and GeoDataFrame among other points.

That Geopands works using a GeoDataFrame object allows to homogenize the structure of any type of data, so that the Python library is able to work with them regardless of their source format.

So let’s see how we can convert different types of spatial or geographic files with Geopands to a GeoDataFrame with few lines of code.

How to read a Shapefile with Geopandas

For read a file type vector Shapefile, We must import the Geopandas library and then use the read_file ( ) method, specifying the SHP file as a parameter.

We will have to store the object in a variable, which will be the GeoDataFrame object of Geopands that contains the data of the read Shapefile.

1
2
3
import geopandas as gpd
gdf = gpd.read_file('datos.shp')

The variable ‘ gdf ’ contains the GeoDataFrame object built from the source Shapefile.

How to read a GeoJSON with Geopands

The same syntax applies to GeoJSON type files:

1
2
3
import geopandas as gpd
gdf = gpd.read_file('datos.geojson')

We can use the following code to obtain the first records of the GeoDataFrame created based on the read GeoJSON:

1
print(gdf.head())

Also, we can see the column structure of the GeoDataFrame with the following code:

1
print(gdf.columns)
How to read GeoJSON with Python Geopands
Visualization of the first records of a GeoDataFrame created from reading a GeoJSON file with Geopands

How to read a Geopackage with Geopands

To read and load a layer from a Geopackage to a Geopand GeoDataFrame, the syntax is slightly different.

This is because, as you should know, a Geopackage is a format based on a spatial SQLite database, so it can contain more than one layer.

Therefore, it is necessary to specify the name of the layer to be read using the ‘ layer ’ argument:

1
2
3
import geopandas as gpd
gdf = gpd.read_file('datos.gpkg', layer='nombre_capa')
NUTS Layer 3 of the Geopackage that we are going to try to read with Geopands

It is important to note that the layer name must exactly match the layer name in the Geopackage file, including upper and lower case

If the ‘ layer ’ argument is not specified, Geopands will read all the layers of the Geopackage file and return a GeoDataFrame object dictionary.

Python code to read a GeoPackage with Python Geopands
Python code to read a Geopackage and specify the layer ( layer ) to convert to GeoDataFrame with Geopands
Record list read GeoPackage with GeoPandas
Result of the first records of the Geopackage layer read with Geopands and converted to GeoDataFrame

How to read a KML with Geopands

To read KML files with Geopands it will be necessary to specify the argument ‘ driver ’.

Otherwise, the syntax is very similar since the read_file ( ) method is also used as shown below:

1
2
3
import geopandas as gpd
gdf = gpd.read_file('datos.kml', driver='KML')

If you have trouble reading a KML file the reason may be that the Fiona library does not have the default KML driver enabled.

To enable it, you must enter the following commands in the python console:

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import fiona
 
# List the drivers available in Fiona with:
fiona.supported_drivers
 
# Make sure they exist in the list.
# Otherwise they should be enabled.
 
# Enable the drivers (according to version):
fiona.drvsupport.supported_drivers['libkml'] = 'rw'
fiona.drvsupport.supported_drivers['LIBKML'] = 'rw'
 
# O well:
fiona.drvsupport.supported_drivers['libkml'] = 'rw'
fiona.drvsupport.supported_drivers['LIBKML'] = 'rw'
 
# Check again that the list has been updated:
fiona.supported_drivers
Fiona drivers to read KML with GeoPandas
Adding the drivers over Fiona to enable reading and writing ( r, w ) of KML files for Geopands

How to read a CSV with Geopands

In the case of CSV files, the technique is slightly different, since we are not talking about vector GIS files properly.

Although it is not a spatial file format as such, it is interesting to know how you can ingest a CSV with geometries in Geopands since despite this it is quite common to exchange or find geographic information in this format.

To achieve this, we must go a little further and lean on the bookstore Shapely ( on which Geopandas ) is built together with Geopandas properly.

This is because we need to process the geometry column ( generally in WKT format) to indicate to Geopandas the reading of said information through Shapely.

We must indicate the parameters ‘ GEOMETRY_COL ’ and ‘ encoding ’ in the read_file ( ) method of Geopands:

1
2
3
4
5
6
7
8
9
10
11
import geopandas as gpd
from shapely import wkt
# Read the csv file
datos = gpd.read_file('datos.csv', GEOMETRY_COL='geom', encoding='utf-8')
# Convert the geometric column of text to Shapely objects
datos['geom'] = datos['geom'].apply(wkt.loads)
# Convert the DataFrame to a GeoDataFrame
gdf = gpd.GeoDataFrame(datos, geometry='geom')

We will have to alter the geometry column ( originally in text format -WKT- from CSV ) with wkt.loads ( ) from Shapely to convert it to a geometric object that Geopands can process.

Finally we turn the DataFrame into a Geopand GeoDataFrame now that we have the geometry properly converted.

Once we know how to access geoinformation, we can begin to perform more advanced tasks aimed at data transformation, that is, manipulating, visualizing, analyzing, combining, etc.

We hope this short article is useful for learning to read and load GIS vector data with Geopands to work with Python

How can you read a WKT in QGIS and use it as a layer of geographic information

Admin

I'm Syed Inzamam Ul Hassan,Through This website, i hope to inspire other GIS students to pursue their passion for geography and data analysis and to contribute to the field by applying their skills to real-world problems.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

kindly Disable Your Adblocker First