Creating GeoDataFrame from DataFrame with coordinates or wkt

Convert DataFrame to GeoDataFrame

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.

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.

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.

We also offer freelancing services. Please email us at contact@spatial-dev.guru for any query.

Leave a ReplyCancel reply