Skip to content
Advertisement

python: convert parenthesis to bracket

I need to check if longitude, latitude is in polygon in Python 3. The code below works, but I have a problem putting data from variable.

lng_lat = Point(42.01410106690003, 20.97770690917969)

#this is how I need to add data
polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
print(lng_lat.within(poly))

but my data are like this (I get data from json file with json.load)

coordinate[0]=[[20.94320297241211, 41.99777372492389], [20.923118591308594, 41.98578066472604] , [20.917625427246094, 41.970467091533], [20.936164855957028, 41.94825586972943]]

How can I pass data from coordinates[0] to polygon which requires ( ) instead of [ ]

and this doesn’t work

polygon = Polygon(coordinates[0])

Advertisement

Answer

It isn’t clear whether they need to be tuples or Point instances…

Also your print statement is trying to print poly which doesn’t exist in the code you included.

lng_lat = Point(42.01410106690003, 20.97770690917969)

coordinate[0]=[[20.94320297241211, 41.99777372492389], [20.923118591308594, 41.98578066472604] , [20.917625427246094, 41.970467091533], [20.936164855957028, 41.94825586972943]]

If tuples then:

data = [tuple(i) for i in coorinate[0]]
polygon = Polygon(data)
print(lng_lat.within(polygon))

and if they need to be Point Instances:

data = [Point(i) for i in coorinate[0]]
polygon = Polygon(data)
print(lng_lat.within(polygon))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement