gpt4 book ai didi

postgresql - 如何使用 GeoTools 从 hstore 列中过滤数据?

转载 作者:行者123 更新时间:2023-11-29 11:35:34 29 4
gpt4 key购买 nike

我正在尝试使用 GeoTools 从启用了 PostGIS 的数据库中的要素表中过滤要素。

我的配置:

  • PostgreSQL 8.4
  • PostGIS 1.5
  • 渗透 0.40.1
  • OSMembrane build 845
  • 地理工具 2.7.4

设置

我通过按顺序执行这些 sql 脚本来设置支持 postgis 的数据库:

  1. [PATH_TO_POSTGRESQL_8.4]/share/contrib/postgis-1.5/postgis.sql
  2. [PATH_TO_POSTGRESQL_8.4]/share/contrib/postgis-1.5/spatial_ref_sys.sql
  3. [PATH_TO_POSTGRESQL_8.4]/postgresql/8.4/contrib/hstore.sql
  4. [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 中按“natural = coaSTLine”过滤行时,我得到约 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?也许,你可以给我一些提示。

最佳答案

虽然我无法让 geoServer 直接使用 hstore(在基于 SQL View 的层中),但我能够向数据库添加一个辅助函数,这允许显示内容。函数是

CREATE OR REPLACE FUNCTION hstore_to_text(h hstore)
RETURNS text AS
$BODY$
DECLARE
txt text;
BEGIN
txt := cast(h as text);

return txt;
END $BODY$
LANGUAGE 'plpgsql' volatile
COST 1;
ALTER FUNCTION hstore_to_text(hstore)
OWNER TO postgres;

然后你可以使用类似的东西在查询中转换 hstore

从 mytable 中选择 id、hstore_to_text(hst_var)、mygeom

并且 FWIW - 执行转换(或直接在查询中的任何其他变体 - 而不是通过函数)不起作用。

关于postgresql - 如何使用 GeoTools 从 hstore 列中过滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9081946/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com