1

Assumed we have some shapefiles imported into QGIS. Each shapefile consists of several features, in this case these are partly connected fragments of polylines (= streets with nodes). Now I want to export the line coordinates of each feature into a single CSV file.


Example:

Layer 1 ("highways") includes feature 1 (= street 1), feature 2 (= street 2), feature 3 (= street 3), feature 4 (= street 4), etc. while some features are connected to each other (e.g. end of street 3 to beginning of street 4).

Question:

So, how can I auto-export all features of the selected layer into single CSV-files without the need to do so for each single feature by itself?

Of course it would be possible to manually select each feature one by one, export it into the target CSV and go ahead to the next. Unfortunately, this would be a big workload in case of higher amounts of features per layer. :-)


In best case I would prefer to select the parental layer, choose "export all features of this layer into single CSV files (named with attributes ID)", and its done.

2
  • 2
    Right-click the layer - Export - Save features as, and choose csv
    – BERA
    Sep 18 '21 at 8:41
  • @MrXsquared: You are right! I mixed it up...
    – Dave
    Sep 18 '21 at 9:17
2

You can skip parts of this answer as you need / do not need them.

First you can add specific coordinates you want to your attribute table. Such as

  • x(start_point($geometry)) for the x coordinate of your startpoint
  • y(end_point($geometry)) for the y coordinate of the endpoint
  • geom_to_wkt(centroid($geometry)) for the wkt representation of the centroid
  • x(line_interpolate_point($geometry,length($geometry)/2)) || ',' || y(line_interpolate_point($geometry,length($geometry)/2)) for comma-separated x and y coordinates of the lines midpoint
  • array_to_string(array_foreach(generate_series(0,num_points($geometry)-1),x_at(@element)),',') for a comma-separated "array" of all x coordinates of all nodes of the line
  • ...

Now right-click your layer and go to Export -> Save features as...

enter image description here

Choose "CSV" as output, remove or add fields as you want and choose all other settings as you need them. Then scroll down a little to "Layer Options" and select "As WKT" as "Geometry". This will add a WKT-Representation of your line to each feature.

enter image description here

You will get a CSV with all features, attributes and a geometry representation, for example:

WKT,id,name,attribute_midpoint_x_y
"LINESTRING (-1.42397137745975 0.141323792486583,-1.16636851520572 0.338103756708408,-0.765652951699463 0.180679785330948,-0.447227191413238 0.216457960644007,-0.178890876565295 0.449016100178891)","1",streat,"-0.8024987253833709,0.19515491070676894"
"LINESTRING (0.075134168157424 0.305903398926655,0.29695885509839 0.277280858676208,0.432915921288014 0.073345259391771,0.450805008944544 -0.094812164579607,0.543828264758497 -0.166368515205725,0.822898032200358 -0.177101967799642,1.10554561717352 -0.0304114490161,1.21645796064401 0.223613595706619)","2",precipitous,"0.6036208068122201,-0.16866822836163697"
"LINESTRING (1.21645796064401 0.223613595706619,1.22361359570662 0.363148479427549,1.1520572450805 0.481216457960644,1.04472271914132 0.584973166368515,0.89087656529517 0.68515205724508,0.783542039355993 0.728085867620751,0.704830053667263 0.756708407871199)","3",undersheets,"1.060582841445665,0.569641714807652"
"LINESTRING (0.704830053667263 0.756708407871199,0.347048300536673 0.788908765652952,0.075134168157424 0.749552772808587,-0.025044722719141 0.68515205724508,-0.189624329159213 0.635062611806798,-0.236135957066189 0.574239713774597,-0.175313059033989 0.524150268336315,-0.178890876565295 0.449016100178891)","4",prelims,"0.13068903580451594,0.7575936089154027"
"LINESTRING (-0.200357781753131 0.223613595706619,-0.293381037567084 0.091234347048301,-0.329159212880143 -0.091234347048301,-0.304114490161002 -0.155635062611807,-0.225402504472272 -0.24865831842576,-0.13953488372093 -0.341681574239714,-0.010733452593918 -0.384615384615385,0.110912343470483 -0.40608228980322,0.289803220035778 -0.431127012522361,0.450805008944544 -0.441860465116279,0.640429338103757 -0.42397137745975)","5",additions,"-0.07954702831089408,-0.361677526043059"
"LINESTRING (-0.096601073345259 -0.148479427549195,-0.046511627906977 -0.04830053667263,-0.007155635062612 0.087656529516995,-0.021466905187836 0.230769230769231,-0.107334525939177 0.330948121645796,-0.143112701252236 0.338103756708408,-0.211091234347048 0.330948121645796,-0.282647584973166 0.277280858676208,-0.350626118067979 0.191413237924866,-0.432915921288014 0.087656529516995,-0.500894454382826 0.019677996422182,-0.762075134168157 -0.066189624329159,-1.01610017889088 -0.066189624329159,-1.12343470483005 -0.04830053667263,-1.17352415026834 -0.033989266547406)","6",Infiltrates,"-0.37713312472882354,0.1579913599611918"
"LINESTRING (-1.50268336314848 -0.087656529516995,-1.40250447227191 -0.237924865831843,-1.32021466905188 -0.280858676207513,-1.01967799642218 -0.255813953488372,-0.840787119856887 -0.191413237924866,-0.644007155635063 -0.24865831842576,-0.540250447227191 -0.48479427549195,-0.379248658318426 -0.570661896243292,-0.143112701252236 -0.570661896243292,0.23613595706619 -0.592128801431127,0.61180679785331 -0.62432915921288,0.81216457960644 -0.588550983899821,0.865831842576029 -0.409660107334526,0.694096601073345 -0.259391771019678,0.479427549194991 -0.202146690518784,0.343470483005367 -0.169946332737031,0.200357781753131 -0.041144901610018,0.186046511627907 0.066189624329159,0.118067978533095 0.162790697674419,0.050089445438283 0.259391771019678,-0.014311270125224 0.352415026833631,0.007155635062612 0.456171735241503,0.007155635062612 0.488372093023256)","7",unroping,"0.2955889864224265,-0.5972247753759473"

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.