我正在尝试使用GeoTools及其OGR Plugin在Java中读取MapInfo MAP文件。我想以特定的排序顺序迭代这些功能。我正在尝试使用GeoTool的Query和SortBy,但生成的SQL查询有语法错误。
代码示例:
OGRDataStoreFactory factory = new JniOGRDataStoreFactory();
Map<String, String> connectionParams = new HashMap<String, String>();
connectionParams.put("DriverName", "MapInfo File");
connectionParams.put("DatasourceName", new File("MyTable.TAB").getAbsolutePath());
DataStore store = factory.createDataStore(connectionParams);
SimpleFeatureSource featureSource = store.getFeatureSource("MyTable");
Query query = new Query();
query.setSortBy(new SortBy[]{ new SortByImpl(new AttributeExpressionImpl("PED"), SortOrder.ASCENDING)});
SimpleFeatureIterator features = featureSource.getFeatures(query).features();
int count = 0;
while (features.hasNext() && count++ < 10) {
Feature feature = features.next();
System.out.println(feature);
}
这会导致将此错误消息打印到stderr:
ERROR 1: SQL Expression Parsing Error: syntax error, unexpected ORDER, expecting '.'. Occurred around :
SELECT FID, * FROM 'MyTable' ORDER BY PED
^
问题是表名周围的单引号。
如果我删除setSortBy()
行,则返回功能。所以数据访问一般都有效。
如果我在命令行上使用双引号使用ogr2ogr
尝试相同的查询,它会按预期工作:
ogr2ogr -f "CSV" /vsistdout/ MyTable.TAB -lco GEOMETRY=AS_WKT -sql "SELECT FID, * FROM \"MyTable\" ORDER BY \"PED\" ASC"
我做错了吗?