문제

I'm trying to filter features from a feature table in a PostGIS enabled database with GeoTools.

My configuration:

  • PostgreSQL 8.4
  • PostGIS 1.5
  • Osmosis 0.40.1
  • OSMembrane build 845
  • GeoTools 2.7.4

Setup

I set up my postgis enabled database by executing these sql scripts in order:

  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

Then I import some .osm data I extracted from europe.osm using OSMembrane.

Everything is fine, so far. All the tables contain some data. Then I try to read a feature table e.g. 'ways' which looks like this:

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)
)

Especially the 'tags' column contains key/values pairs I'd like to use for filtering. When trying to filter rows by "natural = coastline" in SQL I get ~550 resulting rows.

SELECT tags FROM ways where tags @> 'natural => coastline'

Example result: '"source"=>"PGS", "natural"=>"coastline", "created_by"=>"almien_coastlines"'

Trying this with GeoTools does not work as expected as this example will hopefully show you.

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();
        }
    }
}

When running this application I get:

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

Is there an issue with hstore and bigint[] columns or am I misusing GeoTools? Maybe, you can give me some hints.

도움이 되었습니까?

해결책

I'm not sure that the GeoTools PostGIS reader supports hstore columns. Here are some notes I made on importing OSM data in PostGIS. My aim was to display them in GeoServer which uses the GeoTools datastore to read it. I split the data up by tag to make it work.

다른 팁

While I was never able to get geoServer to use an hstore directly (in a SQL view based layer), I was able to add a helper function to the DB, and this allowed the content to be displayed. The function is

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;

Then you can convert the hstore in a query using something like

select id, hstore_to_text(hst_var), mygeom from mytable

And FWIW - doing the cast (or any of several other variations directly in the query - not via the function) does NOT work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top