# python: convert parenthesis to bracket

## Question:

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=[[20.94320297241211, 41.99777372492389], [20.923118591308594, 41.98578066472604] , [20.917625427246094, 41.970467091533], [20.936164855957028, 41.94825586972943]]
``````

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

and this doesn’t work

``````polygon = Polygon(coordinates)
``````

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=[[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]
polygon = Polygon(data)
print(lng_lat.within(polygon))
``````

and if they need to be Point Instances:

``````data = [Point(i) for i in coorinate]
polygon = Polygon(data)
print(lng_lat.within(polygon))
``````
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.