1

I have a list of of coordinates that have areas mapped out on a map

zones coordinates
1 [[13.055847285909415, 77.47638480859382], [13.04673679588868, 77.50519132714851], [13.03294330911764, 77.53331120019539], [12.984367546003645, 77.51502097802745], [12.986637777984326, 77.47269816308585]]
2 [[12.986439600669756, 77.47192161279304], [12.984519364788886, 77.51411771875007], [12.953531964529226, 77.51547471337904], [12.937431189398561, 77.5343411684571], [12.90406392555974, 77.5290972109376], [12.879429976008769, 77.49609116845704], [12.889201427575893, 77.48010313134762], [12.897968518587028, 77.4673766604003], [12.941287828988496, 77.46896249108886]]
3 [[13.067970727531327, 77.59028213220222], [13.043308554001575, 77.58956289392097], [13.048746934938762, 77.57270748620613], [13.04533078511338, 77.55819381433116], [13.039274615569282, 77.54338387780763], [13.033218297771828, 77.53252215295402], [13.045745477110907, 77.50741463952636], [13.055566230480574, 77.4768047609864], [13.066724330134107, 77.50593304162601], [13.085573422401136, 77.5560040102539], [13.100514882298981, 77.59872061639408]]
4 [[13.099321693483562, 77.59929435449226], [13.101247807558844, 77.63565397363288], [13.053720248805917, 77.63357774072279], [13.03896385704673, 77.6273816347657], [13.039489691716911, 77.61483378320304], [13.040234523347282, 77.6059707460937], [13.044324011031744, 77.58955460839843], [13.069817818287284, 77.59099125390628]]
5 [[13.078588701081543, 77.6346565981446], [13.074869974543358, 77.67549570947267], [13.057773739533706, 77.75307035546882], [13.05204798886958, 77.75889054589857], [13.054349036009384, 77.75990421777351], [13.005777469363625, 77.68599570947276], [13.00356732656461, 77.68131379418946], [13.002695246807559, 77.67628855615226], [12.998084137473775, 77.66580367379754], [13.001167064069731, 77.66355853753657], [13.009984211149678, 77.66303678422537], [13.017128548826081, 77.66148506265256], [13.021381972441715, 77.64876858239745], [13.029386875457236, 77.63277699761966], [13.03806046465725, 77.62708509545894], [13.050678737165466, 77.63246995263671]]

Currently I am trying to convert this into a script, being a beginner not able to figure out how to convert these object variables into a geodata frame

I have used the following code:

my_geo_df = gpd.GeoDataFrame(Poly_Data, geometry=Poly_Data['coordinates'])

It give me the following error:

Input must be valid geometry objects: [[13.055847285909415, 77.47638480859382], [13.04673679588868, 77.50519132714851], [13.03294330911764, 77.53331120019539], [12.984367546003645, 77.51502097802745], [12.986637777984326, 77.47269816308585]]

So, I am trying to figure out how to convert these coordinates to valid geometric objects for usage.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Nov 4, 2020 at 10:41
2
  • How is the list of coordinates originally stored, as a csv? Commented Nov 4, 2020 at 10:59
  • its stored in the same format in a database and have a few 1000 of these to work with Commented Nov 4, 2020 at 11:05

2 Answers 2

2

My solution:

import geopandas as gpd 
from shapely.geometry import Polygon
# read the csv file
gdf = gpd.read_file("coords.csv",sep=";")
gdf
 zones coordinates geometry
0 1 [[13.055847285909415, 77.47638480859382], [13.... None
1 2 [[12.986439600669756, 77.47192161279304], [12.... None
2 3 [[13.067970727531327, 77.59028213220222], [13.... None
3 4 [[13.099321693483562, 77.59929435449226], [13.... None
4 5 [[13.078588701081543, 77.6346565981446], [13.0... None

But problem here: coordinates are strings -> error

type(gdf.coordinates[0]) # first row
str

Therefore

gdf['geometry'] = gdf.apply(lambda row:Polygon(eval(row['coordinates'])), axis=1)
gdf 
 zones coordinates geometry
0 1 [[13.055847285909415, 77.47638480859382], [13.... POLYGON ((13.05585 77.47638, 13.04674 77.50519...
1 2 [[12.986439600669756, 77.47192161279304], [12.... POLYGON ((12.98644 77.47192, 12.98452 77.51412...
2 3 [[13.067970727531327, 77.59028213220222], [13.... POLYGON ((13.06797 77.59028, 13.04331 77.58956...
3 4 [[13.099321693483562, 77.59929435449226], [13.... POLYGON ((13.09932 77.59929, 13.10125 77.63565...
4 5 [[13.078588701081543, 77.6346565981446], [13.0... POLYGON ((13.07859 77.63466, 13.07487 77.67550...
gdf.plot(column='zones', cmap='viridis')

enter image description here

answered Nov 4, 2020 at 13:34
1
  • This solutions works great too I am tring to, I am trying to add this to a folium map as follows mapnew= folium.Map(location=[43.062776, -75.420884],tiles="cartodbpositron", zoom_start=11) folium.GeoJson(data=gdf["geometry"]).add_to(mapnew) which gives me an error message stating RuntimeError: b'no arguments in initialization list' Commented Nov 5, 2020 at 3:33
0

Assuming that Poly_Data is a normal pandas dataframe (as it looks like that in your print-out) with a column name 'coordinates'


coordiantes
zones 
1 [[13.055847285909415, 77.47638480859382], [13....
2 [[12.986439600669756, 77.47192161279304], [12....
3 [[13.067970727531327, 77.59028213220222], [13....
4 [[13.099321693483562, 77.59929435449226], [13....
5 [[13.078588701081543, 77.6346565981446], [13.0...

you can try this code:

import shapely
import geopandas as gpd
import ast
# If your lists are interpreted as strings, do this first
Poly_Data['coordinates'] = Poly_Data['coordinates'].apply(lambda x: ast.literal_eval(x))
Poly_Data['geometry'] = Poly_Data['coordinates'].apply(lambda x: shapely.geometry.Polygon(x))
Poly_Data = gpd.GeoDataFrame(Poly_Data)
answered Nov 4, 2020 at 11:22
7
  • I copied the data on to excel and then pasted the same here. <br/> When I try this code it give me the following error File "shapely/speedups/_speedups.pyx", line 234, in shapely.speedups._speedups.geos_linearring_from_py AttributeError: 'str' object has no attribute 'array_interface' AssertionError Commented Nov 4, 2020 at 11:40
  • Easier Poly_Data['geometry'] = Poly_Data.apply(shapely.geometry.Polygon(row['coordinates'])), axis=1) Commented Nov 4, 2020 at 11:43
  • @Ani, I am a bit lost in how you are trying to apply this? What object type is your Poly_data? Commented Nov 4, 2020 at 12:38
  • @JaschaMuller The Poly_Data is dataframe in Pandas's which has this coordiates at zone level. I need to first have them mapped out then later will use this data as a base to to add more inforation for each of these zone. Commented Nov 4, 2020 at 13:09
  • @gene that gives me an error Commented Nov 4, 2020 at 13:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.