5

I have a CSV file with the following structure:

id,Geometry
0,MULTIPOLYGON(((343362.10597 7395133.999035, 343362.608646 7395137.592628, 343366.027235 7395138.714539, 343389.482792 7395141.302312, 343390.91157 7395141.3619, 343408.443301 7395142.225044, 343424.796266 7395143.518546, 343427.805771 7395143.750087, 343459.114406 7395151.925896, 343500.691088 7395167.099913, 343533.442931 7395178.768452, 343560.442616 7395188.89142, 343579.337382 7395195.970768, 343607.760736 7395207.592871, 343616.264189 7395206.786678, 343622.432438 7395203.795961, 343628.643546 7395197.749076, 343632.516051 7395190.791227, 343644.108743 7395154.080564, 343650.279907 7395134.566388, 343665.549752 7395091.970019, 343674.153464 7395070.3001, 343684.521723 7395042.103866, 343692.086323 7395023.080773, 343697.664272 7395011.301474, 343704.042755 7395002.000384, 343722.789 7394978.98797, 343737.600949 7394965.856473, 343743.321932 7394960.75674, 343744.156484 7394956.225018, 343740.567359 7394952.90858, 343734.573577 7394954.871254, 343718.491883 7394957.833673, 343704.830423 7394955.971197, 343700.751989 7394955.451426, 343671.508596 7394953.732586, 343656.454855 7394949.352304, 343613.264807 7394936.154904, 343594.698268 7394929.920763, 343581.627559 7394925.329026, 343550.709676 7394913.115351, 343526.554635 7394899.589577, 343478.903537 7394873.940365, 343468.007238 7394863.657216, 343463.622054 7394861.273541, 343459.51688 7394863.256304, 343455.532832 7394867.278113, 343454.991209 7394874.005756, 343454.889219 7394883.540009, 343454.130651 7394913.411519, 343452.278125 7394924.333536, 343448.140323 7394943.681126, 343441.382138 7394972.237009, 343430.693786 7395001.681122, 343425.177072 7395017.259621, 343409.659822 7395056.198477, 343398.087215 7395079.530933, 343391.654566 7395092.9179, 343367.420067 7395126.868407, 343362.10597 7395133.999035)))
1,MULTIPOLYGON(((341753.99139 7396339.857021, 341864.449827 7396365.802449, 341945.003303 7396384.856923, 341974.17656 7396388.616031, 341983.161653 7396389.621122, 342025.201086 7396394.305266, 342072.473769 7396416.499583, 342081.250386 7396416.007268, 342086.252749 7396410.424149, 342135.553505 7396237.879056, 342157.373097 7396136.636069, 342162.163276 7396117.572635, 342167.848504 7396101.675161, 342177.284315 7396088.321013, 342196.099067 7396064.989896, 342199.664712 7396061.008181, 342233.478221 7396032.068704, 342296.775018 7396002.671521, 342179.142305 7396025.834978, 342126.305945 7396050.039647, 342053.755181 7396104.265651, 342037.640031 7396129.098673, 342029.569424 7396140.828401, 342017.999074 7396144.745865, 342010.864372 7396150.173053, 341980.079585 7396256.048079, 341975.091734 7396254.609903, 341963.052807 7396293.418932, 341955.312202 7396308.618598, 341946.724563 7396319.988315, 341932.796812 7396331.33361, 341922.464309 7396339.749661, 341896.858804 7396349.806005, 341877.578455 7396352.82059, 341861.859599 7396352.252155, 341757.173213 7396327.00034, 341753.99139 7396339.857021)))

I'd like to convert CSV files like this to Shapefiles using ogr2ogr. I've found solutions like this one where it solves the problem when the geometry is just lat and lng. For POINTs, it can be solved like the following:

ogr2ogr -oo X_POSSIBLE_NAMES=Lon* -oo Y_POSSIBLE_NAMES=Lat*  -f "ESRI Shapefile" test.shp test.csv

However, my case is different since I have a MULTIPOLYGON geometry asText on my CSV. How can I use ogr2ogr to convert CSV files like the one in my first sample to shapefile?

3
  • gis.stackexchange.com/questions/24947/…
    – BERA
    Dec 18, 2021 at 16:33
  • @BERA Thanks, now I understood that I need to create a .vrt file in order to make this convertion. But since I need it for a MULTIPOLYGON it'll differ a little from that question. I'm trying to figure out how to create it here... I didn't find any direct sample of what the .vrt file should be for my case.
    – raylight
    Dec 18, 2021 at 16:45
  • 1
    The VRT route may have been the only possibility nearly ten years ago (answered May 7 '12 at 21:02) but nowadays the GDAL CSV driver can read WKT directly.
    – user30184
    Dec 18, 2021 at 18:32

1 Answer 1

3

Conversion is possible with a slightly edited csv file. Read first https://gdal.org/drivers/vector/csv.html#vector-csv

When reading a field named “WKT” is assumed to contain WKT geometry, but also is treated as a regular field. The OGR CSV driver returns all attribute columns as string data types if no field type information file (with .csvt extension) is available.

CSV edited to have a wkt field:

id,wkt
0,"MULTIPOLYGON(((343362.10597 7395133.999035, 343362.608646 7395137.592628, 343366.027235 7395138.714539, 343389.482792 7395141.302312, 343390.91157 7395141.3619, 343408.443301 7395142.225044, 343424.796266 7395143.518546, 343427.805771 7395143.750087, 343459.114406 7395151.925896, 343500.691088 7395167.099913, 343533.442931 7395178.768452, 343560.442616 7395188.89142, 343579.337382 7395195.970768, 343607.760736 7395207.592871, 343616.264189 7395206.786678, 343622.432438 7395203.795961, 343628.643546 7395197.749076, 343632.516051 7395190.791227, 343644.108743 7395154.080564, 343650.279907 7395134.566388, 343665.549752 7395091.970019, 343674.153464 7395070.3001, 343684.521723 7395042.103866, 343692.086323 7395023.080773, 343697.664272 7395011.301474, 343704.042755 7395002.000384, 343722.789 7394978.98797, 343737.600949 7394965.856473, 343743.321932 7394960.75674, 343744.156484 7394956.225018, 343740.567359 7394952.90858, 343734.573577 7394954.871254, 343718.491883 7394957.833673, 343704.830423 7394955.971197, 343700.751989 7394955.451426, 343671.508596 7394953.732586, 343656.454855 7394949.352304, 343613.264807 7394936.154904, 343594.698268 7394929.920763, 343581.627559 7394925.329026, 343550.709676 7394913.115351, 343526.554635 7394899.589577, 343478.903537 7394873.940365, 343468.007238 7394863.657216, 343463.622054 7394861.273541, 343459.51688 7394863.256304, 343455.532832 7394867.278113, 343454.991209 7394874.005756, 343454.889219 7394883.540009, 343454.130651 7394913.411519, 343452.278125 7394924.333536, 343448.140323 7394943.681126, 343441.382138 7394972.237009, 343430.693786 7395001.681122, 343425.177072 7395017.259621, 343409.659822 7395056.198477, 343398.087215 7395079.530933, 343391.654566 7395092.9179, 343367.420067 7395126.868407, 343362.10597 7395133.999035)))"
1,"MULTIPOLYGON(((341753.99139 7396339.857021, 341864.449827 7396365.802449, 341945.003303 7396384.856923, 341974.17656 7396388.616031, 341983.161653 7396389.621122, 342025.201086 7396394.305266, 342072.473769 7396416.499583, 342081.250386 7396416.007268, 342086.252749 7396410.424149, 342135.553505 7396237.879056, 342157.373097 7396136.636069, 342162.163276 7396117.572635, 342167.848504 7396101.675161, 342177.284315 7396088.321013, 342196.099067 7396064.989896, 342199.664712 7396061.008181, 342233.478221 7396032.068704, 342296.775018 7396002.671521, 342179.142305 7396025.834978, 342126.305945 7396050.039647, 342053.755181 7396104.265651, 342037.640031 7396129.098673, 342029.569424 7396140.828401, 342017.999074 7396144.745865, 342010.864372 7396150.173053, 341980.079585 7396256.048079, 341975.091734 7396254.609903, 341963.052807 7396293.418932, 341955.312202 7396308.618598, 341946.724563 7396319.988315, 341932.796812 7396331.33361, 341922.464309 7396339.749661, 341896.858804 7396349.806005, 341877.578455 7396352.82059, 341861.859599 7396352.252155, 341757.173213 7396327.00034, 341753.99139 7396339.857021)))"

Test with ogrinfo:

ogrinfo gis_se_test.csv -al
INFO: Open of `gis_se_test.csv'
      using driver `CSV' successful.

Layer name: gis_se_test
Geometry: Unknown (any)
Feature Count: 2
Extent: (341753.991390, 7394861.273541) - (343744.156484, 7396416.499583)
Layer SRS WKT:
(unknown)
id: String (0.0)
wkt: String (0.0)
OGRFeature(gis_se_test):1
....

Command that converts WKT into geometry but does not try to save the WKT also as a string into an attribute:

ogr2ogr -f "ESRI Shapefile" gis_se_test.shp gis_se_test.csv -sql "select id from gis_se_test"
1
  • As the documentation says but [wkt] also is treated as a regular field. You do not need WKT as a string (and it is useless in shapefile in your case anyway because strings can only be max 254 characters long) so you can either delete the wkt field from the shapefile afterwards, or select just the id (geometry will be selected automatically by the OGR SQL dialect) with SQL ogrinfo gis_se_test.csv -sql "select id from gis_se_test".
    – user30184
    Dec 18, 2021 at 20:18

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.