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

标签 postgresql postgis geotools

我正在尝试使用 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/

相关文章:

java - GeoTools:从 LAT/LNG 转换为 Albers

postgresql 9.3 记录慢速查询

postgresql - Symfony2 - 如何限制用户可以存储的记录数?

java - 使用java(jdbc)在mysql中插入地理空间数据类型(multipolygon)

postgresql - 类型 "geometry"不存在

postgis - 管理单个 PostGIS 表上的多个 SRID

java - Geotools 找不到 HSQL EPSG DB,引发错误 : NoSuchAuthorityCodeException

python - 使用新值更新 Enum 后,无法使用 psycopg2 插入该值

ruby-on-rails - Rails 和 jsonb 类型 "jsonb"不存在

sql - 从纬度和经度对计算边界框