我正在尝试使用 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"
难道我做错了什么?