1

I am trying to read a CSV file where the geometries of neighbouring polygons are stored in couples. The file looks like this. enter image description here

I usually read shapefiles in GDAL using the following syntax

GDALAllRegister();

GDALDataset       *poDS;
poDS = (GDALDataset*) GDALOpenEx("shape.shp", GDAL_OF_VECTOR, NULL, NULL, NULL );
if( poDS == NULL )
{
    printf( "Open failed.\n" );
    exit( 1 );
}

OGRLayer  *poLayer;
poLayer = poDS->GetLayerByName("shape" ); // returns the layer that refers to the name (name.shp)
cout <<"There are " <<  poLayer -> GetFeatureCount() << "features"<<endl ;
OGRFeature *poFeature;

But this of course doesn't work. The documentation found here (http://gdal.org/1.11/ogr/drv_csv.html) did not help me at all unfortunately.

I would like to know if there is a way to read this file in GDAL. I am coding in C++, but an answer in Python would be informative as well.

  • What do you want the result to be? – bugmenot123 Feb 26 '18 at 12:26
  • Ideally I would like to scroll the file and populate a polygon class I have created for every single polygon. Which means two per row. – Duccio Piovani Feb 26 '18 at 12:29
1

These look like Well Known Text fields.

You can load these with something like Shapely, for example.

The example from the website linked is this:

from shapely.wkt import loads
g = loads('POINT (0.0 0.0)')

Obviously yours are polygons, but it should work the same.

In C++, you will be able to load it manually, if you need. Or there are likely a few libraries, such as this one, that do it for you.

Edit: using GDAL to parse WKT directly is documented here.

  • 1
    thank you very much for your answer; unfortunately I can't use Shapely. I need to use GDAL, because my code is in C++, and GDAL exists for both python and C++, unlike shapely. – Duccio Piovani Feb 26 '18 at 16:32
  • hi @DuccioPiovani, I've added another line pointing to the documentation on GDAL's geometry creation from WKT. – Alex Leith Feb 26 '18 at 21:07
  • 1
    thank you very much. I can't up vote you because I need at least 15 points of reputation. – Duccio Piovani Feb 27 '18 at 9:50
  • Can you mark the question as resolved, @DuccioPiovani? I get points from that ;-) – Alex Leith Feb 27 '18 at 11:39

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.