2

I'm a software developer and I need to create a custom tool that imports ESRI shapefiles (shp + shx + dbf), manipulates the content and then exports new shapefiles. To avoid a complex binary reading of the 3 formats, I'd like to convert the input files into a "well known" textual format (JSON, XML, plain text, ...) and I want to be able to revert the conversion.

I tried to use gdal/ogr2ogr to convert the shapefiles into CSV but the output seems to only include the content of the database (dbf file). This means that, if I try to convert the CSV back into shapefiles, I lose all the polygon coordinates.

I then tried to convert to GeoJSON, which was much better but a few data appear to be missing again (like shape boundaries and polygon indexes).

My question is: is there an option in gdal for a "lossless" conversion between shapefiles and any humanly readable format?

Thanks.

  • 1
    What do you mean with missing shape boundaries and polygon indexes? – user30184 Nov 6 '14 at 17:52
  • 1
    Try your ogr2ogr translation again using, as @Mike T suggested, the -lco GEOMETRY=AS_WKT option. If you're interested, I wrote a blog post that expands on some of ogr2ogr's CSV-specific options. – elrobis Nov 6 '14 at 23:27
2

All of the vector formats supported by GDAL/OGR are listed here. With each driver, check out the creation options to control the output. These are passed to ogr2ogr using -dco and -lco flags.

Good text-based output drivers include:

1

What you are looking for is Geographic Markup Language (GML). It is human readable and should maintain everything.

-f gml

in gdal / ogr.

Do not overlook KML either. Both are human readable vector OGC standards supported in gdal / ogr.

You can open both in text editors.

0

Off the top of my head, I don't think that you will get a line string or polgon string of x and y values in csv. I believe that you will only receive point values if you pick csv. You will have a column for x and a column for y.

The gdal libraries that read shapefiles treat the three files as one animal. These libraries already do all the work of reading the three complex file formats, if you include their header files in your project. GeoJSON, GML, and KML would be good intermediate formats for what you are trying to do.

"Import" is the keep word in your question. Depending on your import target, gdal/ogr may have a supporting target database or additional file format. That would save writing code that others have already vetted. Type

ogr2ogr --formats

on the command line. The formats option will list all the file formats that have been compiled into your version of the gdal/ogr application that you are using. Pre-compiled versions may offer more or less of the available file formats. Also check the version that you are using because new formats are made available from release to release. Again available formats depend on what has been compiled for you.

As far as your as your alignment problems, make sure that you use a spatial reference system or SRS. Use -x_srs options where x refers to one of the named srs options. This is a safety measure to make sure that you are explicit in the reference system that you expect gdal to use during the conversion. I have heard of enough problems and hair pulling where the SRS options were not explicitly used, that I always use them to avoid that pain and suffering. That being said, there can be some inherit distortion in any conversion that you perform. That however is part of taking values on a round looking ball and trying to make them look the same on a 2d coordinate plane.

  • By default, the CSV driver doesn't output any geometry data, unless you have something like -lco GEOMETRY=AS_WKT – Mike T Nov 7 '14 at 0:41
  • @MikeT that is a great point! I only use the CSV format when I want just the attribute data and never considered the problem from that angle. – Greg Nov 7 '14 at 1:00

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.