1

Let's you have a list of coordinates that you can convert to a line string:

coords = [((0.0, 0.0), (0.0, 20.0)), ((20.0, 0.0), (20.0, 20.0)), ((1.0569687968009744, 14.934839039628798), (5.671166148109483, 20.19191074287741)), ((5.159985296083214, 5.788534924043662), (1.818050229337418, -0.37120648178790816)), ((7.0017400619480945, 12.967260092075572), (10.96695601450711, 7.213267528511481)), ((13.884688806883744, 0.35179537762349833), (15.348360220796115, -6.498103101647066))]

now you can do the following:

mlines = MultiLineString(coords)

Which gives you:

MULTILINESTRING ((0 0, 0 20), (20 0, 20 20), (1.200116278748922 14.84428055460748, -5.798466435712259 14.62200169463755), (0.6555662089127656 12.32427661303933, 2.553985876183362 5.598843696996065), (13.35396083234261 12.58544673080557, 15.13771022756384 5.812255232822563), (4.862372312454008 4.839458771128404, 10.04340607477727 0.1213240497574262))

So now my question is how you can reverse the action, to get a list of arrays? I also saw this mapping(mlines) using from shapely.geometry import mapping. But I do not know how I can have access to coordinates not in the LineString format but also in a list format which is array elements of the same data type.

  • Please Edit your question to specify (and tag) the Python package in use. – Vince Apr 18 at 23:32
  • Adding a second question is not likely to result in fewer close votes. – Vince Apr 19 at 3:03
2

Given your coordinates list as a shapely geometry MultiLineString object:

>>> mls = shapely.geometry.MultiLineString(coords)

Iterating over it gives the individual LineString objects:

>>> list(mls)
[<shapely.geometry.linestring.LineString object at 0x7f7c10dcf850>, <shapely.geometry.linestring.LineString object at 0x7f7c10dcf790>, <shapely.geometry.linestring.LineString object at 0x7f7c10dcf710>, <shapely.geometry.linestring.LineString object at 0x7f7c10dcf7d0>, <shapely.geometry.linestring.LineString object at 0x7f7c10dcf6d0>, <shapely.geometry.linestring.LineString object at 0x7f7c10dcf810>]

Then the coords method of a LineString gets the coordinates and you iterate over that to get:

>>> [list(x.coords) for x in list(mls)]
[[(0.0, 0.0), (0.0, 20.0)], [(20.0, 0.0), (20.0, 20.0)], [(1.0569687968009744, 14.934839039628798), (5.671166148109483, 20.19191074287741)], [(5.159985296083214, 5.788534924043662), (1.818050229337418, -0.37120648178790816)], [(7.0017400619480945, 12.967260092075572), (10.96695601450711, 7.213267528511481)], [(13.884688806883744, 0.35179537762349833), (15.348360220796115, -6.498103101647066)]]

which is that you started with except as a list of lists rather than a list of tuples. But close enough. If you really want the output to be the same as the input then:

>>> [tuple(x.coords) for x in list(mls)]
[((0.0, 0.0), (0.0, 20.0)), ((20.0, 0.0), (20.0, 20.0)), ((1.0569687968009744, 14.934839039628798), (5.671166148109483, 20.19191074287741)), ((5.159985296083214, 5.788534924043662), (1.818050229337418, -0.37120648178790816)), ((7.0017400619480945, 12.967260092075572), (10.96695601450711, 7.213267528511481)), ((13.884688806883744, 0.35179537762349833), (15.348360220796115, -6.498103101647066))]

>>> [tuple(x.coords) for x in list(mls)] == coords
True
  • Thanks for your help – koorosh esteki Apr 19 at 13:30
  • Glad to do so. If my answer answers your question there's a little "tick" next to it for you to accept it as the answer. This will help people find this answer if they have the same or similar question. – Spacedman Apr 19 at 14:12
  • 1
    I believe I just did it, let me know if I did not do it correctly – koorosh esteki Apr 19 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.