I am building a small application which uses postgres and has a couple of tables which have a postgis polygon as one of their columns. I was thinking of using a Geotools DataStore rather than querying it directly through SQL, so that I can take advantage of the library tools (and potentially be able to change to a different DataStore easily if needed.)
I am trying to understand how to get Geotools to know my Schema and which columns it should consider as attributes to my features. All the examples I found in the documentation just set the database URL, username, password, database name etc. but nothing about the tables to extract the features from.
http://docs.geotools.org/stable/userguide/library/jdbc/postgis.html
How do I tell the PostGIS Plugin how to extract the polygons and attributes from my database?
Alternatively, does Geotools expect a table with a certain name and certain structure? If yes where can I find the information about what it expects?
1 Answer 1
Once you have created your DataStore
you can query it for the types available (in the case of a PostGIS Store these map to tables) and then ask for a FeatureSource
for that type using the getFeatureSource
method.
So something like:
// Read
DataStore inputDataStore = DataStoreFinder.getDataStore(inParams);
String inputTypeName = "AstunLocationLookup";
SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);
FeatureSource<SimpleFeatureType, SimpleFeature> source = inputDataStore.getFeatureSource(inputTypeName);
Now inputType
contains all the attributes of the table (AstunLocationLookup) and source
gives access to all the features (rows) of the table, with or without a filter:
Filter filter = ff.bbox(ff.property(geometryPropertyName), bbox);
// write results
SimpleFeatureCollection features = source.getFeatures(filter);
-
Is
geometryPropertyName
the field (column in the table) for the feature's geometry? So if the table had more than one geometry field (column) I could query them separately?jbx– jbx2018年10月18日 17:21:03 +00:00Commented Oct 18, 2018 at 17:21 -
Yes you can access any of the geometry columIan Turton– Ian Turton2018年10月18日 17:38:25 +00:00Commented Oct 18, 2018 at 17:38