I have a CSV with many points and attributes that I'm trying to convert into a shapefile but I'm having no luck. I have installed gdal-bin and can use my terminal environment to create a shapefile with ogr2ogr, but I'm not getting the desired outcome. I'm running the following:

ogr2ogr -f "ESRI Shapefile" 5_2834k2-25_FAST.shp 5_2834K2-25_DataCompiled_K2.csv -progress -skipfailures -a_srs EPSG:4326 -nlt POINT

And I get:

"5_2834k2-25_FAST.prj" 1 KB
"5_2834k2-25_FAST.shp" 8 KB
"5_2834k2-25_FAST.shx" 6 KB
"5_2834k2-25_FAST.dbf" 7,625 KB

but despite having all my objects and all the features, my SHP has no geometry. It's extent is 0 and recalculating gives me NaN errors in Arc. Now my source CSV does have a lat and lon column (two separate columns) and I've tried to glean from other answers how to get ogr2ogr to see and interpret these values but I cannot.

For example, perhaps using this bit of sqlite to choose the columns:

-dialect sqlite -sql "SELECT easting_col as long, northing_col as lat, geometry FROM input"

and "SELECT lon as long [...]" from input where input is my CSV... but nope. I tried a two-step cheat, thinking I could provide the dbf or shp from the incomplete output of the first as that input in the second but no...

I've tried as well GEOMETRY=AS_XY and I'm reading the man pages, the --long-usage help file as well as the GDAL ogr2ogr page but I just can't come up with the proper syntax.

How can I do this? I'm very frustrated because my columns don't have elaborate names ('Lat' and 'Lon') so I figured it would be very straightforward.

  • 1
    Could you add the column names and one row of data? You can use some fake data if your own are confidential.
    – user30184
    Nov 26 '18 at 6:43
  • You might need to put up a csvt file to tell GDAL which columns to take as geometry. An extract of your csv data (with header) might help.
    – AndreJ
    Nov 26 '18 at 7:16
  • you definetely need to tell GDAL how to construct your points; there is no auto-detection of arbitrary numbers as geometries. Try with "SELECT *, MakePoint(<e_col>, <n_col>) AS geometry FROM <input>" (from the CSV).
    – geozelot
    Nov 26 '18 at 9:45
  • There is a limited auto-recognition as documented in gdal.org/drv_csv.html Starting with GDAL 2.1, accordingly with the GeoCSV specification, the "CoordX" or "Point(X)" type can be used to specify a column with longitude/easting values, "CoordY" or "Point(Y)" for latitude/northing values and "WKT" for geometries encoded in WKT. Other also documented alternatives are to tell X and Y fields with open options of to write a VRT file that takes care of mapping.
    – user30184
    Nov 26 '18 at 9:56
  • Thanks for all the replies. @user30184 There is a lot of data but here is the first row and a few columns: IDRndX latLonKey Lat Lon nomCat 1966-08-17_DS1036 1966-08-17_DS1037 1972-09-21_Landsat1 2001 -9.44507 -60.7239 Forest F F F latLonKey Lat is sometimes empty. it is the lat multiplied by the lon, nothing fancy. I definitely got the impression there was some auto detection of geometry @ThingumaBob, and I was trying to fulfill those requirements but obviously falling short. I'll look into a cvst file for sure, since my layout should be fairly static, thanks @AndreJ.
    – Pictory
    Nov 26 '18 at 19:10

Success! ogr2ogr -f "ESRI Shapefile" 5_2834k2-25_FAST.shp 5_2834K2-25_DataCompiled_K2.csv -oo X_POSSIBLE_NAMES=lon* -oo Y_POSSIBLE_NAMES=lat* -a_srs EPSG:4326 -nlt POINT -skipfailures

Referring to https://www.gdal.org/drv_csv.html was the key, for me. I found the examples and language on that page more useful than https://www.gdal.org/ogr2ogr.html.

-oo NAME=VALUE: Input dataset open option (format specific)

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.