我正在尝试使用 GeoTools 从启用 PostGIS 的数据库中的特征表中过滤特征。
我的配置:
- PostgreSQL 8.4
- 地理信息系统 1.5
- 渗透 0.40.1
- OSMembrane 构建 845
- 地理工具 2.7.4
设置
我通过按顺序执行这些 sql 脚本来设置启用 postgis 的数据库:
- [PATH_TO_POSTGRESQL_8.4]/share/contrib/postgis-1.5/postgis.sql
- [PATH_TO_POSTGRESQL_8.4]/share/contrib/postgis-1.5/spatial_ref_sys.sql
- [PATH_TO_POSTGRESQL_8.4]/postgresql/8.4/contrib/hstore.sql
- [PATH_TO_OSMOSIS_0_40_1/script/pgsnapshot_schema_0.6.sql
然后我导入一些我使用 OSMembrane 从 europe.osm 提取的 .osm 数据。
到目前为止,一切都很好。所有表都包含一些数据。然后我尝试阅读一个特征表,例如“方式”,它看起来像这样:
CREATE TABLE ways
(
id bigint NOT NULL,
"version" integer NOT NULL,
user_id integer NOT NULL,
tstamp timestamp without time zone NOT NULL,
changeset_id bigint NOT NULL,
tags hstore,
nodes bigint[],
CONSTRAINT pk_ways PRIMARY KEY (id)
)
特别是“标签”列包含我想用于过滤的键/值对。当尝试在 SQL 中通过“自然 = 海岸线”过滤行时,我得到约 550 个结果行。
SELECT tags FROM ways where tags @> 'natural => coastline'
Example result: '"source"=>"PGS", "natural"=>"coastline", "created_by"=>"almien_coastlines"'
使用 GeoTools 尝试此操作无法按预期工作,因为此示例有望向您展示。
package getfeaturesapplication;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
public class GetFeaturesApplication {
public static void main(String[] args) {
try {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
parameters.put(PostgisNGDataStoreFactory.HOST.key, "localhost");
parameters.put(PostgisNGDataStoreFactory.PORT.key, new Integer(5432));
parameters.put(PostgisNGDataStoreFactory.DATABASE.key, "postgis");
parameters.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");
parameters.put(PostgisNGDataStoreFactory.USER.key, "osm");
parameters.put(PostgisNGDataStoreFactory.PASSWD.key, "osm");
DataStore dataStore = DataStoreFinder.getDataStore(parameters);
String featureName = "ways";
SimpleFeatureSource featureSource = dataStore.getFeatureSource(featureName); //=> WARNINGS
SimpleFeatureCollection features1 = featureSource.getFeatures();
System.out.println("Feature count: " + features1.size()); //406391
FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2(null);
Filter filter = filterFactory.equals(filterFactory.literal("natural"), filterFactory.literal("coastline"));
SimpleFeatureCollection features2 = featureSource.getFeatures(filter);
System.out.println("Features found after filtering: " + !features2.isEmpty()); //SEEMS TO BE ALWAYS EMPTY
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
运行此应用程序时,我得到:
31.01.2012 15:27:49 org.geotools.jdbc.JDBCFeatureSource buildFeatureType
WARNING: Could not find mapping for 'tags', ignoring the column and setting the feature type read only
31.01.2012 15:27:49 org.geotools.jdbc.JDBCFeatureSource buildFeatureType
WARNING: Could not find mapping for 'nodes', ignoring the column and setting the feature type read only
Feature count: 406391
Features found after filtering: false
hstore 和 bigint[] 列是否存在问题,或者我是否在滥用 GeoTools?也许,你可以给我一些提示。