3

Let's say I have the two following CSV files:

file1.csv:

Latitude,Longitude,name
-23.5592201171,-46.5417389180,one
-23.5666778463,-46.5500715205,two

file2.csv:

Latitude,Longitude,name
-23.5703366507,-46.5483625224,three
-23.5840397199,-46.5147095166,four

I know I can convert them individually to shapefiles using ogr2ogr using the following command:

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

However, if I try using this same command a second time with my second CSV file:

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

My old shapefile will be overwritten by the contents of my second CSV. I'd like to continuously add more data inside this shapefile as new CSV files arrive. So the final result inside my CSV would be the concatenation of my CSV files:

Latitude,Longitude,name
-23.5592201171,-46.5417389180,one
-23.5666778463,-46.5500715205,two
-23.5703366507,-46.5483625224,three
-23.5840397199,-46.5147095166,four

I'd like to avoid merging these CSVs before converting them to Shapefile. Is there any way of adding new data to a Shapefile considering the new CSVs will all have the same columns of the first original csv that created the first Shapefile?

1
  • Options that you need are documented in gdal.org/programs/ogr2ogr.html. Combination -update -append -nln output should work for the next CSV files. Notice that "new layer name" -nln is the name of the existing shapefile, without .shp extension.
    – user30184
    Commented Dec 18, 2021 at 20:30

1 Answer 1

4

You must use ogr2ogr with options -update, -append, and -nln https://gdal.org/programs/ogr2ogr.html

First command:

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

Second command:

ogr2ogr -oo X_POSSIBLE_NAMES=Lon* -oo Y_POSSIBLE_NAMES=Lat*  -f "ESRI Shapefile" output.shp file2.csv -update -append -nln output

Check that output.shp contains 4 features now:

ogrinfo output.shp -al -so
INFO: Open of `output.shp'
      using driver `ESRI Shapefile' successful.

Layer name: output
Metadata:
  DBF_DATE_LAST_UPDATE=2021-12-18
Geometry: Point
Feature Count: 4
Extent: (-46.550072, -23.584040) - (-46.514710, -23.559220)
Layer SRS WKT:
(unknown)
Latitude: Real (24.15)
Longitude: Real (24.15)
name: String (80.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.