Reputation: 12508
I have a coordinates list looking like so
myCoordinates
> [(2, -6), (21, 19)]
I want to convert them into shapely geometry
objects so that I can do some calculations with them:
from shapely.geometry import Point
for i in myCoordinates:
c = [Point(i[0], i[1])]
print c
> [<shapely.geometry.point.Point object at 0x1044033d0>]
However, that only gives me one (!) geometry object.
But when I do this
circles = [Point(random.random(), random.random()).buffer(random.random() * 0.1) for i in range(3)]
I get three geometry objects.
print circles
> [<shapely.geometry.polygon.Polygon object at 0x1043f6890>, <shapely.geometry.polygon.Polygon object at 0x10442f8d0>, <shapely.geometry.polygon.Polygon object at 0x10442f910>]
What am I doing wrong? Why is it only converting one Point into a geometry object and not the two in my list?
Upvotes: 3
Views: 14315
Reputation: 391
You are overwriting your variable in each iteration of the loop. You need to make c
a list and then append to it:
from shapely.geometry import Point
c = []
for i in myCoordinates:
c.append([Point(i[0], i[1])])
print c
Or you can do it all in one line with a list comprehension:
c = [Point(coord[0], coord[1]) for coord in myCoordinates]
Upvotes: 6
Reputation: 19871
Following would give you three points:
c = [Point(i[0], i[1]) for i in myCoords]
It follows the same list comprehension as the circles
:
circles = [Point(random.random(), random.random()).buffer(random.random() * 0.1) for i in range(3)]
What you were doing earlier is assigning the point to a variable c
so at the end of the for loop, there is just one point in a list:
for i in myCoordinates:
c = [Point(i[0], i[1])] # here c is overwritten in each iteration
Upvotes: 2