1

Is there a way to import shapefiles into an existing spatialite-DB (or GeoPackage) and refer the columns of the importing shapefile to the right column of the DB.

E.g. I have a column in the DB named "abc". But in the shapefile the column is namend "def". Do I have to rename the columnname in the shapefile before import or is there a smarter way (especially I have a large number of shapefiles to import)?

Franz

1

So you have one shapefile with attribute "abc" and another one with attribute "def" like this:

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

Layer name: layer_1
Metadata:
  DBF_DATE_LAST_UPDATE=2016-10-10
Geometry: Point
Feature Count: 1
Extent: (130.000000, 373.000000) - (130.000000, 373.000000)
Layer SRS WKT:
(unknown)
abc: String (3.0)

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

Layer name: layer_2
Metadata:
  DBF_DATE_LAST_UPDATE=2016-10-10
Geometry: Point
Feature Count: 1
Extent: (140.000000, 420.000000) - (140.000000, 420.000000)
Layer SRS WKT:
(unknown)
def: String (3.0)

Then you write the first one into GeoPackage

ogr2ogr -f gpkg test.gpkg layer_1.shp

And next you want append the second one into previously created table "layer_1". You must

  • Tell the layer name to append with -nln
  • change the attribute name "def" into "abc" with SQL

ogr2ogr -f gpkg -append -update test.gpkg -sql "select def as abc from layer_2" layer_2.shp -nln layer_1

The select clause must of course contain all the other fields of the schema as well.

Check the result with ogrinfo

ogrinfo test.gpkg layer_1
INFO: Open of test.gpkg
using driver GPKG successful.

Layer name: layer_1
Geometry: Point
Feature Count: 2
Extent: (130.000000, 373.000000) - (140.000000, 420.000000)
Layer SRS WKT:
(unknown)
FID Column = fid
Geometry Column = geom
abc: String (3.0)
OGRFeature(layer_1):1
  abc (String) = foo
  POINT (130 373)

OGRFeature(layer_1):2
  abc (String) = bar
  POINT (140 420)

Seems to work. With SQLite dialect the SQL would be slightly different:

-dialect SQLite -sql "select geometry, def as abc from..."
3
  • Okay, but thers is no GUI to do this? Are the commands above for the Linux-commandline? I work on Windows7, how can i realise this?
    – vezzon
    Oct 11 '16 at 7:29
  • You said you have large number of files to import. GUI would only make the process slower for you. The same commands work on Windows. If you have installed QGIS from OSGeo4W you can run the tools from the OSGeo4W shell window. Otherwise install GDAL from gisinternals.com first.
    – user30184
    Oct 11 '16 at 7:35
  • You are right. But in the future it would be more comfortable to have a GUI. How do I realise this using a script (for a better handling of the itereations)
    – vezzon
    Oct 11 '16 at 8:43

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.