4

I am looking at updating a number of large shapefiles using ogrinfo and UPDATE. I would like to perform a CASE function to update specific fields with specific values. Is it possible to perform CASE functions within ogrinfo UPDATE?

8

Ryan, You should be able to with the SQLite dialect:

$ ogrinfo -ro -geom=no -where "TZID='America/Boise' OR TZID='America/Denver'" tz_world.shp tz_world
INFO: Open of `tz_world.shp'
  using driver `ESRI Shapefile' successful.

Layer name: tz_world
Metadata:
DBF_DATE_LAST_UPDATE=2012-06-25

Geometry: Polygon
Feature Count: 3
Extent: (-179.999900, -89.999900) - (179.999900, 83.627419)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
TZID: String (30.0)
OGRFeature(tz_world):1611
  TZID (String) = America/Boise

OGRFeature(tz_world):3379
  TZID (String) = America/Denver

OGRFeature(tz_world):3380
  TZID (String) = America/Denver

Run the case query with -dialect sqlite

$ ogrinfo -sql "UPDATE tz_world SET TZID=CASE WHEN TZID='America/Boise' THEN 'KYLE' WHEN TZID='America/Denver' THEN 'KYLE2' END" tz_world.shp -dialect sqlite

INFO: Open of `tz_world.shp'
      using driver `ESRI Shapefile' successful.

Changes:

$ ogrinfo -ro -where "TZID LIKE 'KYLE%'" tz_world.shp  tz_world -geom=no
INFO: Open of `tz_world.shp'
      using driver `ESRI Shapefile' successful.

Layer name: tz_world
Metadata:
  DBF_DATE_LAST_UPDATE=2015-10-15
Geometry: Polygon
Feature Count: 3
Extent: (-179.999900, -89.999900) - (179.999900, 83.627419)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
TZID: String (30.0)
OGRFeature(tz_world):1611
  TZID (String) = KYLE

OGRFeature(tz_world):3379
  TZID (String) = KYLE2

OGRFeature(tz_world):3380
  TZID (String) = KYLE2
0

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.