- 1 How to read spatial data files with Geopands?
- 2 How to read a Shapefile with Geopandas
- 3 How to read a GeoJSON with Geopands
- 4 How to read a Geopackage with Geopands
- 5 How to read a KML with Geopands
- 6 How to read a CSV with Geopands
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.
- 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.
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.
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:
We can use the following code to obtain the first records of the GeoDataFrame created based on the read GeoJSON:
Also, we can see the column structure of the GeoDataFrame with the following code:
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:
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.
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:
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:
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
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:
# Convert the geometric column of text to Shapely objects
# Convert the DataFrame to a GeoDataFrame
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