Sunday, 12 April 2015

sql - Converting MultiLinestring to Linestring with PostGIS?


I have a multiline string and want to convert it to a linestring in order to use some functions that only work with linestrings. I used ST_Dump() and got a collection of my geometries. If I merge them back together, I still get the multilinestring. Should I get the Npoints and combine them together or something, in order to end up with a simple linestring? ST_LineMerge() also does not work on my multilinestring, it returns the same.


my example,


MULTILINESTRING((-3.16420835153456 55.9269166007097,-3.164222 55.926918),(-3.1642070167833 55.9269296196706,-3.16421351659546 55.9268662214904),(-3.16421351659546 55.9268662214904,-3.16421636372824 55.9268384509897),(-3.16421636372824 55.9268384509897,-3.16422182573761 55.9267851753802),(-3.16422182573761 55.9267851753802,-3.16422870102352 55.926718114886),(-3.16422870102352 55.926718114886,-3.16423309121073 55.926675293667),(-3.16423309121073 55.926675293667,-3.16423565148822 55.9266503211093),(-3.16423565148822 55.9266503211093,-3.16424103159897 55.9265978443265),(-3.16424103159897 55.9265978443265,-3.16424680776317 55.9265415044985),(-3.16424680776317 55.9265415044985,-3.16425267254583 55.9264843002995),(-3.16425267254583 55.9264843002995,-3.16425541048045 55.9264575949012),(-3.16425541048045 55.9264575949012,-3.16426111146586 55.9264019883556),(-3.16426111146586 55.9264019883556,-3.1642667032531 55.9263474469124),(-3.1642667032531 55.9263474469124,-3.16426957768543 55.9263194101362),(-3.16426957768543 55.9263194101362,-3.16427488261739 55.9262676666359),(-3.16427488261739 55.9262676666359,-3.16428009893088 55.9262167875066),(-3.16428009893088 55.9262167875066,-3.164282741107 55.9261910161221),(-3.1642875546472 55.9261440655823,-3.164282741107 55.9261910161221),(-3.1642875546472 55.9261440655823,-3.16429466890915 55.9260746741522),(-3.16429466890915 55.9260746741522,-3.16430092974527 55.9260136069079),(-3.16430092974527 55.9260136069079,-3.16430822838418 55.9259424170929),(-3.16430822838418 55.9259424170929,-3.16431547242401 55.925871759829),(-3.16431547242401 55.925871759829,-3.16431448732505 55.9258328901507),(-3.16431770120536 55.9257327846001,-3.16431547242401 55.925871759829),(-3.164339 55.925777,-3.16431770120536 55.9257327846001))



Answer



You could also use ST_SnapToGrid to sort of fix your data, but you'll need to play around with the parameters to get it right.


SELECT ST_AsText(

ST_LineMerge(
ST_SnapToGrid(
ST_GeomFromText('MULTILINESTRING((-3.16420835153456 55.9269166007097,-3.164222 55.926918),(-3.1642070167833 55.9269296196706,-3.16421351659546 55.9268662214904),(-3.16421351659546 55.9268662214904,-3.16421636372824 55.9268384509897),(-3.16421636372824 55.9268384509897,-3.16422182573761 55.9267851753802),(-3.16422182573761 55.9267851753802,-3.16422870102352 55.926718114886),(-3.16422870102352 55.926718114886,-3.16423309121073 55.926675293667),(-3.16423309121073 55.926675293667,-3.16423565148822 55.9266503211093),(-3.16423565148822 55.9266503211093,-3.16424103159897 55.9265978443265),(-3.16424103159897 55.9265978443265,-3.16424680776317 55.9265415044985),(-3.16424680776317 55.9265415044985,-3.16425267254583 55.9264843002995),(-3.16425267254583 55.9264843002995,-3.16425541048045 55.9264575949012),(-3.16425541048045 55.9264575949012,-3.16426111146586 55.9264019883556),(-3.16426111146586 55.9264019883556,-3.1642667032531 55.9263474469124),(-3.1642667032531 55.9263474469124,-3.16426957768543 55.9263194101362),(-3.16426957768543 55.9263194101362,-3.16427488261739 55.9262676666359),(-3.16427488261739 55.9262676666359,-3.16428009893088 55.9262167875066),(-3.16428009893088 55.9262167875066,-3.164282741107 55.9261910161221),(-3.1642875546472 55.9261440655823,-3.164282741107 55.9261910161221),(-3.1642875546472 55.9261440655823,-3.16429466890915 55.9260746741522),(-3.16429466890915 55.9260746741522,-3.16430092974527 55.9260136069079),(-3.16430092974527 55.9260136069079,-3.16430822838418 55.9259424170929),(-3.16430822838418 55.9259424170929,-3.16431547242401 55.925871759829),(-3.16431547242401 55.925871759829,-3.16431448732505 55.9258328901507),(-3.16431770120536 55.9257327846001,-3.16431547242401 55.925871759829),(-3.164339 55.925777,-3.16431770120536 55.9257327846001))'),
0.001)
)
);

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...