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

时间:2012-01-31 15:15:14

标签: postgresql postgis geotools

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

我的配置:

  • PostgreSQL 8.4
  • PostGIS 1.5
  • 渗透0.40.1
  • OSMembrane build 845
  • GeoTools 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 /脚本/ pgsnapshot_schema_0.6.sql
  5. 然后我导入一些使用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)
    )
    

    特别是'tags'列包含我想用于过滤的键/值对。 当尝试在SQL中按“natural = coastline”过滤行时,我得到〜550行。

    SELECT tags FROM ways where tags @> 'natural => coastline'
    
    Example result: '"source"=>"PGS", "natural"=>"coastline", "created_by"=>"almien_coastlines"'
    

    使用GeoTools尝试使用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?也许,你可以给我一些提示。

2 个答案:

答案 0 :(得分:2)

虽然我从来没有能够让geoServer直接使用hstore(在基于SQL视图的层中),但我能够向数据库添加辅助函数,这样就可以显示内容。功能是

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

选择id,hstore_to_text(hst_var),来自mytable的mygeom

而FWIW - 直接在查询中执行演员表(或其他任何变体 - 不通过函数)都不起作用。

答案 1 :(得分:1)

我不确定GeoTools PostGIS阅读器是否支持hstore列。 Here are some notes我在PostGIS中导入OSM数据。我的目标是在GeoServer中显示它们,它使用GeoTools数据存储来读取它。我按标签分割数据以使其正常工作。