3

I am using shapely unary_union to dissolve multiple line strings to one line before exporting to a shapefile.

The output from unary_union is a multiline string (example below which you can paste into Wicket). When exported to a shapefile it has multiple features, not a single line feature. The lines will also be contiguous and won't crossover.

I wish to export a single line to a shapefile.

Any ideas how I can convert this multilinestring to a single line to export to a shapefile using Python?

MULTILINESTRING ((-0.09949110399971201 51.5155806841531, -0.09939409904597001 51.5155866836785), (-0.09939409904597001 51.5155866836785, -0.099157101257869 51.5156026798101, -0.09894309853312901 51.5156136796089, -0.098606089003514 51.5156396806721), (-0.110974258419515 51.5160386692171, -0.110238251143733 51.5160786768392, -0.110021240981987 51.5160836790967, -0.10920123172948 51.5160436761356), (-0.100489111082423 51.5158396976804, -0.100147103216365 51.515751692648, -0.100005104816976 51.5157156867549, -0.099705101703361 51.5156386848511, -0.09949110399971201 51.5155806841531), (-0.096948042611141 51.5162726881509, -0.096809042170933 51.5163016817966, -0.096766045170016 51.5163096798522, -0.096646035155609 51.516306679561, -0.096536034520283 51.5162796780959, -0.09599703639863801 51.5160726720984), (-0.10920123172948 51.5160436761356, -0.108946235222691 51.5159406814919), (-0.098606089003514 51.5156396806721, -0.098581080324168 51.5159856856925), (-0.098581080324168 51.5159856856925, -0.098579085793847 51.5160186837986), (-0.101631124099211 51.5161287050545, -0.101345125778253 51.5160607023621, -0.100489111082423 51.5158396976804), (-0.108946235222691 51.5159406814919, -0.108427215046776 51.5163006848396, -0.108233217600768 51.5164566927597), (-0.10189812477131 51.5162747066834, -0.101635127398062 51.5161617104127, -0.101631124099211 51.5161287050545), (-0.09756804878370801 51.5168587005634, -0.097378042105961 51.5168537018865), (-0.105628154794541 51.5174297237828, -0.105285153672418 51.5173207230403), (-0.105825165458289 51.5174827247051, -0.105628154794541 51.5174297237828), (-0.106150163502194 51.5175487266679, -0.105825165458289 51.5174827247051), (-0.107501647389745 51.5177349595282, -0.107340171401701 51.517729728669, -0.106975176646835 51.5176907283359), (-0.106626162493138 51.5176407258543, -0.106341172817837 51.5175897234412, -0.106155168677775 51.5175507229232, -0.106150163502194 51.5175487266679), (-0.106975176646835 51.5176907283359, -0.106784171150247 51.5176687253487, -0.106626162493138 51.5176407258543), (-0.107966196562356 51.5169177085558, -0.10799419951621 51.5169487086171, -0.107991194093993 51.5169687050333), (-0.105285153672418 51.5173207230403, -0.105043150976977 51.5172627229313), (-0.107991194093993 51.5169687050333, -0.107955188123035 51.5171947151563, -0.107827192698567 51.5175737187894, -0.107752187450459 51.5176407280537, -0.107620186230955 51.5176727263747, -0.107501647389745 51.5177349595282), (-0.09599703639863801 51.5160726720984, -0.09565201332232399 51.5167506891305), (-0.108233217600768 51.5164566927597, -0.108143207289198 51.5165296924142), (-0.102908143089807 51.5166167176979, -0.102566136130494 51.516508713858, -0.102125132159789 51.5163697100862, -0.102097126506935 51.5163607138707, -0.10189812477131 51.5162747066834), (-0.09684403151146299 51.5168507003487, -0.096917047040718 51.5164926876091, -0.096948042611141 51.5162726881509), (-0.098579085793847 51.5160186837986, -0.098577075996562 51.5160786869506, -0.098523075572903 51.5163296987367, -0.09851506315709301 51.5166507006444), (-0.09565201332232399 51.5167506891305, -0.095124002251253 51.5166336771743, -0.095038011838352 51.5166146812559), (-0.108143207289198 51.5165296924142, -0.108067207437454 51.516627702025, -0.108004200620196 51.5167617033692, -0.107966196562356 51.5169177085558), (-0.097378042105961 51.5168537018865, -0.097303044257364 51.5168506952598, -0.097278040842032 51.5168346960399, -0.0972490383143 51.5168236948407, -0.097226045725422 51.5168216940064, -0.09720304771133 51.516825700246, -0.097183044487221 51.5168366974981, -0.09716203615346999 51.5168597017592, -0.096969034909765 51.516856695177), (-0.097740048796003 51.5167776967244, -0.097643044636673 51.5168047018443, -0.097597040695186 51.5168256999103, -0.09756804878370801 51.5168587005634), (-0.096969034909765 51.516856695177, -0.09684403151146299 51.5168507003487), (-0.105043150976977 51.5172627229313, -0.104872152536407 51.5172147272079, -0.104701153906309 51.5171577204369, -0.104530157078847 51.5171017205277, -0.104389153408835 51.5170447211936, -0.104261154722598 51.517006717753, -0.104032144721189 51.5169677202329, -0.102908143089807 51.5166167176979), (-0.09851506315709301 51.5166507006444, -0.098516066683921 51.516674700639), (-0.098516066683921 51.516674700639, -0.09834305848463699 51.5166906985632), (-0.09834305848463699 51.5166906985632, -0.098296060979841 51.5167397022699, -0.098132056239479 51.5167526984394, -0.097987060249468 51.5167367043092, -0.09787504867437299 51.5167337001089), (-0.09787504867437299 51.5167337001089, -0.097834057950725 51.5167437005148, -0.097740048796003 51.5167776967244))
5

If you use Fiona (what are you using ?) to export the MultiLineString to a shapefile, the results is a shapefile with a single feature (the shapefile format does not know the "MultiLineString" geometry, replaced by a list of polyline geometries).

enter image description here

from shapely.wkt import loads
multi = loads("MULTILINESTRING ((-0.09949110399971201 51.5155806841531, -0.09939409904597001 51.5155866836785), (-0.09939409904597001 51.5155866836785, -0.099157101257869 51.5156026798101, -0.09894309853312901 51.5156136796089, -0.098606089003514 51.5156396806721), (-0.110974258419515 51.5160386692171, -0.110238251143733 51.5160786768392, -0.110021240981987 51.5160836790967, -0.10920123172948 51.5160436761356), (-0.100489111082423 51.5158396976804, -0.100147103216365 51.515751692648, -0.100005104816976 51.5157156867549, -0.099705101703361 51.5156386848511, -0.09949110399971201 51.5155806841531), (-0.096948042611141 51.5162726881509, -0.096809042170933 51.5163016817966, -0.096766045170016 51.5163096798522, -0.096646035155609 51.516306679561, -0.096536034520283 51.5162796780959, -0.09599703639863801 51.5160726720984), (-0.10920123172948 51.5160436761356, -0.108946235222691 51.5159406814919), (-0.098606089003514 51.5156396806721, -0.098581080324168 51.5159856856925), (-0.098581080324168 51.5159856856925, -0.098579085793847 51.5160186837986), (-0.101631124099211 51.5161287050545, -0.101345125778253 51.5160607023621, -0.100489111082423 51.5158396976804), (-0.108946235222691 51.5159406814919, -0.108427215046776 51.5163006848396, -0.108233217600768 51.5164566927597), (-0.10189812477131 51.5162747066834, -0.101635127398062 51.5161617104127, -0.101631124099211 51.5161287050545), (-0.09756804878370801 51.5168587005634, -0.097378042105961 51.5168537018865), (-0.105628154794541 51.5174297237828, -0.105285153672418 51.5173207230403), (-0.105825165458289 51.5174827247051, -0.105628154794541 51.5174297237828), (-0.106150163502194 51.5175487266679, -0.105825165458289 51.5174827247051), (-0.107501647389745 51.5177349595282, -0.107340171401701 51.517729728669, -0.106975176646835 51.5176907283359), (-0.106626162493138 51.5176407258543, -0.106341172817837 51.5175897234412, -0.106155168677775 51.5175507229232, -0.106150163502194 51.5175487266679), (-0.106975176646835 51.5176907283359, -0.106784171150247 51.5176687253487, -0.106626162493138 51.5176407258543), (-0.107966196562356 51.5169177085558, -0.10799419951621 51.5169487086171, -0.107991194093993 51.5169687050333), (-0.105285153672418 51.5173207230403, -0.105043150976977 51.5172627229313), (-0.107991194093993 51.5169687050333, -0.107955188123035 51.5171947151563, -0.107827192698567 51.5175737187894, -0.107752187450459 51.5176407280537, -0.107620186230955 51.5176727263747, -0.107501647389745 51.5177349595282), (-0.09599703639863801 51.5160726720984, -0.09565201332232399 51.5167506891305), (-0.108233217600768 51.5164566927597, -0.108143207289198 51.5165296924142), (-0.102908143089807 51.5166167176979, -0.102566136130494 51.516508713858, -0.102125132159789 51.5163697100862, -0.102097126506935 51.5163607138707, -0.10189812477131 51.5162747066834), (-0.09684403151146299 51.5168507003487, -0.096917047040718 51.5164926876091, -0.096948042611141 51.5162726881509), (-0.098579085793847 51.5160186837986, -0.098577075996562 51.5160786869506, -0.098523075572903 51.5163296987367, -0.09851506315709301 51.5166507006444), (-0.09565201332232399 51.5167506891305, -0.095124002251253 51.5166336771743, -0.095038011838352 51.5166146812559), (-0.108143207289198 51.5165296924142, -0.108067207437454 51.516627702025, -0.108004200620196 51.5167617033692, -0.107966196562356 51.5169177085558), (-0.097378042105961 51.5168537018865, -0.097303044257364 51.5168506952598, -0.097278040842032 51.5168346960399, -0.0972490383143 51.5168236948407, -0.097226045725422 51.5168216940064, -0.09720304771133 51.516825700246, -0.097183044487221 51.5168366974981, -0.09716203615346999 51.5168597017592, -0.096969034909765 51.516856695177), (-0.097740048796003 51.5167776967244, -0.097643044636673 51.5168047018443, -0.097597040695186 51.5168256999103, -0.09756804878370801 51.5168587005634), (-0.096969034909765 51.516856695177, -0.09684403151146299 51.5168507003487), (-0.105043150976977 51.5172627229313, -0.104872152536407 51.5172147272079, -0.104701153906309 51.5171577204369, -0.104530157078847 51.5171017205277, -0.104389153408835 51.5170447211936, -0.104261154722598 51.517006717753, -0.104032144721189 51.5169677202329, -0.102908143089807 51.5166167176979), (-0.09851506315709301 51.5166507006444, -0.098516066683921 51.516674700639), (-0.098516066683921 51.516674700639, -0.09834305848463699 51.5166906985632), (-0.09834305848463699 51.5166906985632, -0.098296060979841 51.5167397022699, -0.098132056239479 51.5167526984394, -0.097987060249468 51.5167367043092, -0.09787504867437299 51.5167337001089), (-0.09787504867437299 51.5167337001089, -0.097834057950725 51.5167437005148, -0.097740048796003 51.5167776967244))")
import loads
from shapely.geometry import mapping
# schema of the resulting shapefile
schema = {'geometry': 'MultiLineString','properties': {'id': 'int'}}
# save 
with fiona.open('multiline.shp', 'w', 'ESRI Shapefile', schema)  as output:
     output.write({'geometry':mapping(multi),'properties': {'id':1}})

Now open the resulting shapefile

 result = fiona.open('multiline.shp')
 # schema
 print result
 {'geometry': 'LineString', 'properties': OrderedDict([(u'id', 'int:10')])}
 # number of features
 print "number= ", len(result)
 number=  1
 # the feature
 feat = result.next()
 print feat['geometry']['type']
 MultiLineString

If you absolutely want a LineString (with Making a flat list out of list of lists in Python)

multicoords = [list(line.coords) for line in multi]
# Making a flat list -> LineString
simple = LineString([item for sublist in multicoords  for item in sublist])
# resulting shapefile
schema = {'geometry': 'LineString','properties': {'id': 'int'}}
with fiona.open('line.shp', 'w', 'ESRI Shapefile', schema)  as output:
   output.write({'geometry':mapping(a),'properties': {'id':1}})

Control

result = fiona.open('line.shp')
print len(result)
1
feat = result.next()
print feat['geometry']['type']
LineString

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.