Tuesday 23 February 2016

python - How to create a Shapely LineString from two Points


If have two points, from which I want to create a straight LineString object:


from shapely.geometry import Point, LineString
A = Point(0,0)
B = Point(1,1)


The Shapely manual for LineString states:



A sequence of Point instances is not a valid constructor parameter. A LineString is described by points, but is not composed of Point instances.



So if I have two points A and B, is there a shorter/better/easier way of creating a line AB than my current "best" guess...


AB = LineString(tuple(A.coords) + tuple(B.coords))

... which looks rather complicated. Is there an easier way?


Update


With today's released Shapely 1.3.2, the above statement from the manual is no longer correct. So from now on,



AB = LineString([A, B])

works!



Answer



Since Shapely 1.3, you can create a LineString from Points:


>>> from shapely.geometry import Point, LineString
>>> LineString([Point(0, 0), Point(1, 1)]).wkt
'LINESTRING (0 0, 1 1)'

Apologies for the contradiction in the manual.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...