0

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?

asked Oct 18, 2018 at 9:17

1 Answer 1

3

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);
answered Oct 18, 2018 at 16:46
2
  • 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? Commented Oct 18, 2018 at 17:21
  • Yes you can access any of the geometry colum Commented Oct 18, 2018 at 17:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.