I was trying to create a simple Polygon from a list of coordinates and export it to multiple formats (including shapefile and GeoJSON) and wanted to automate it using Python. There were some good answers on creating polygons from coordinates in Python:pyshp and in Python:gdal/ogr, but I prefer using GeoPandas.

I came up with some simple code to create a simple polygon from a list of coordinates, but other users on GIS StackExchange helped to improve the code. The code is working perfectly in Python 3.6 with GeoPandas 0.4.0:

import geopandas as gpd
from shapely.geometry import Polygon

lat_point_list = [50.854457, 52.518172, 50.072651, 48.853033, 50.854457]
lon_point_list = [4.377184, 13.407759, 14.435935, 2.349553, 4.377184]

polygon_geom = Polygon(zip(lon_point_list, lat_point_list))
crs = {'init': 'epsg:4326'}
polygon = gpd.GeoDataFrame(index=[0], crs=crs, geometry=[polygon_geom])       

polygon.to_file(filename='polygon.geojson', driver='GeoJSON')
polygon.to_file(filename='polygon.shp', driver="ESRI Shapefile")

Possibly this code helps others and it would be nice to have suggestions how to create a better Python script that creates a polygon from coordinates in GeoPandas.

I added improvements by ImanolUr, gene and reevesii to the code! I made a stupid mistake to reverse latitude and longitude order. The location of the polygon can be checked by using folium in Jupyter Notebook:

import folium
m = folium.Map([50.854457, 4.377184], zoom_start=5, tiles='cartodbpositron')

enter image description here

  • If this is a working code, it seems more suited for Code Review stack ( codereview.stackexchange.com), but the GIS libraries are quite specific, so I'm not sure whether it could stay here. – ImanolUr Aug 28 '18 at 11:38

I would substitute the for loop for:

polygon_geom = zip(lon_list, lat_list)
  • 1
    With zip() simply use polygon_geom = zip(lon_point_list,lat_point_list) (there is no need for list comprehension here) – gene Aug 28 '18 at 12:10
  • Indeed, I'll edit the answer. – ImanolUr Aug 28 '18 at 13:09
  • 1
    The format of shapely geometries is (x,y), so (longitude, latitude) and not (latitude, longitude) – gene Aug 28 '18 at 14:48
  • Thanks for suggestions. Stupid mistake to reverse longitude and latitude, but the syntax of shapely felt very similar to WKT strings. Next time I should check on the map. – Davma Aug 28 '18 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.