The usage of Python in GeoSpatial field has exponentially grown. Almost every GIS desktop software provide python integration. QGIS, famous open source GIS software, has python interface. Python has many open source GIS libraries for example GDAL, Fiona, GeoPandas, RasterIO, Shapely, Xarray Spatial etc. These libraries can process both raster as well as vector data.
GeoPandas is one of the most famous python GIS library that can automate your GIS workflows. GeoPandas provides good functionality and features to create really good geoprocessing tools like geospatial analysis, reading/writing from/to postgis database, transforming GIS data between different formats, buffer analysis etc.
In this tutorial, we will convert DataFrame with latitude longitude column or wkt(Well Known Text) column to GeoDataFrame.
Converting Point Coordinates to GeoDataFrame
The following code will convert the DataFrame with lat long columns to GeoDataFrame.
1. We are reading csv file which have lat long columns using pandas at line number 5.
2. At line number 8 and 9, we are changing data type of lat long columns to numeric/float type.
3. At line number 12, we are using points_from_xy method of geopandas to convert lat long to series of Point Geometry and then we are using GeoDataFrame from geoapandas to construct GeoDataFrame. This line of code will convert DataFrame with lat long columns to GeoDataFrame.
4. And finally, at line number 13, we are setting the Projection or CRS for our created GeoDataFrame.
That’s it. You have successfully converted DataFrame with lat long columns to GeoDataFrame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import geopandas as gpd import pandas as pd # Read csv file uk_accidents = pd.read_csv('uk_accidents_2012_to_2014_xy.tar.xz', low_memory=False) # Convert Long Lat into numeric type uk_accidents['Longitude'] = pd.to_numeric(uk_accidents['Longitude']) uk_accidents['Latitude'] = pd.to_numeric(uk_accidents['Latitude']) # Convert Long Lat into Point Geometry uk_accidents = gpd.GeoDataFrame(uk_accidents, geometry = gpd.points_from_xy(x=uk_accidents['Longitude'], y=uk_accidents['Latitude'])) # Set CRS uk_accidents = uk_accidents.set_crs('EPSG:4326') |
Converting WKT to GeoDataFrame
The following code will convert the DataFrame with WKT column to GeoDataFrame. The WKT is textual representation of a geometry which looks like this ‘Point(1,1)’.
1. We are importing wkt module from shapely library at line number 3 which will convert the WKT columns to shapely geometry.
2. We are reading csv file which have WKT column using pandas at line number 6.
3. At line number 9, we are using wkt.loads method from shapely to parse WKT column to shapely geometry. And then we are storing these geometries in DataFrame’s ‘geometry’ columns.
4. At this time, DataFrame is not fully converted to GeoDataFrame because geometry column type is still object type even though values in this column are shapely geometries. We have to explicitly set the column type to geometry type by using set_geometry(‘geometry’) method at line 12. After setting the the geometry, our DataFrame will be converted to GeoDataFrame.
5. And finally, at line number 15, we are setting the Projection or CRS for our created GeoDataFrame.
That’s it. You have successfully converted DataFrame with WKT column to GeoDataFrame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import geopandas as gpd import pandas as pd from shapely import wkt # Read csv file uk_accidents = pd.read_csv('uk_accidents_2012_to_2014_wkt.tar.xz', low_memory=False) # Convert WKT into Point Geometry uk_accidents['geometry'] = uk_accidents['wkt'].apply(wkt.loads) # Set geometry type uk_accidents = uk_accidents.set_geometry('geometry') # Set CRS uk_accidents = uk_accidents.set_crs('EPSG:4326') |
I hope this tutorial will create a good foundation for you. If you want tutorials on another GIS topic or you have any queries, please send an email at contact@spatial-dev.guru.