wuqn yqow
wuqn yqow

Reputation: 85

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])

Upvotes: 1

Views: 119

Answers (1)

Alexander
Alexander

Reputation: 17291

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))

Upvotes: 1

Related Questions