GDAL SQL语法以添加字段put值


9

我尝试在属性表中创建一个新字段,并在从字符转换为整数后将另一个字段的值放入其中,但无法获得正确的语法。我在MSYS中将其作为shell脚本运行。

cd D:/GIS_DataBase/CorineLC/shps_app_and_extr/
myfile=extr_and_app.shp

name=${myfile%.shp}

ogrinfo $myfile -sql "ALTER TABLE $name ADD COLUMN code_num int(3)"
ogrinfo $myfile -sql "UPDATE TABLE $name SET code_num = CONVERT(code_06 As int(3))"

错误消息(MSYS):

Kay@KAY-PC /c/users/kay/desktop/bash
$ sh calc_field_shp.sh
Warning 6: Unsupported column type 'int'. Defaulting to VARCHAR
INFO: Open of `extr_and_app.shp'
      using driver `ESRI Shapefile' successful.
ERROR 1: SQL Expression Parsing Error: syntax error
INFO: Open of `extr_and_app.shp'
      using driver `ESRI Shapefile' successful.

编辑-SQLite的另一种选择:

cd D:/GIS_DataBase/CorineLC/shps_app_and_extr/
myfile=extr_and_app.dbf

name=${myfile%.dbf}

ogrinfo $myfile -sql "ALTER TABLE $name DROP COLUMN code_num"

ogrinfo $myfile -sql "ALTER TABLE $name ADD COLUMN code_num integer(3)"
ogrinfo $myfile -dialect SQLite -sql "UPDATE $name SET code_num = CAST(code_06 As integer(3))"

错误信息:

Kay@KAY-PC /c/users/kay/desktop/bash
$ sh calc_field_shp.sh
INFO: Open of `extr_and_app.dbf'
      using driver `ESRI Shapefile' successful.
INFO: Open of `extr_and_app.dbf'
      using driver `ESRI Shapefile' successful.
ERROR 1: SQL Expression Parsing Error: syntax error
INFO: Open of `extr_and_app.dbf'
      using driver `ESRI Shapefile' successful.

你遇到了什么错误?
RK

@RK,我将错误消息添加到操作中。

您是否尝试过CAST而不是CONVERT?
dmci 2013年

我没有成功

Answers:


13

如注释中所述,由于OGR SQL不支持UPDATE ,因此应使用GDAL> = 1.10中可用的SQLite SQL方言,并使用SQLite和SpatiaLite支持来更新表:

ogrinfo $myfile -sql "ALTER TABLE $name ADD COLUMN code_num integer(3)"
ogrinfo $myfile -dialect SQLite -sql "UPDATE $name SET code_num = CAST(code_06 AS integer(3))"

3

您可以尝试使用CAST运算符,如dmci所提到的那样

ogrinfo $myfile -sql "UPDATE TABLE $name SET code_num = CAST(code_06 as int(3))"

如果正确调用,OGR支持的SQL方言将没有CONVERT。您可以检查文档以获取更多信息。祝好运!


1
似乎UPDATE不受支持,请参见此处:osgeo-org.1560.x6.nabble.com/…但是,它应该使用SQLite方言,但我也没有运气。–
Kay

您正在使用哪个版本的GDAL?
RK

我正在使用gdal-17-

1
SQLite语言需要GDAL / OGR 1.10
13年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.