I want to develop a simple GIS application using GeoTools. I have looked at the tutorials but i could get to much meaningful from them.
I have a simple JMapFrame to which add can add a featureLayer from a shapefile
public void action(ActionEvent e) throws Throwable{
sourceFile = JFileDataStoreChooser.showOpenFile("shp", null);
if (sourceFile == null) {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile);
featureSource = store.getFeatureSource();
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
}
Now, lets say I want to create a buffer around my features. From what I have understood I have to get the featureSource, then create a featureCollection, iterate through the features and for each of those feature I have to cast them to a Geometry, then create a buffer.
When I have created a buffer should I go all the way back to featureLayer and add it to my map?
Answer
In general GeoTools always looks to display Layers
in the JMapFrame
, so any time you want to display something new you will need to construct a new layer and add it to the MapContext
.
For any operation involving features (such as buffering) then you will need to loop through all of the features you need and apply the operation to them. This will usually use a SimpleFeatureIterator
and generate a new SimpleFeatureCollection
which you can generate a new layer from.
I'd use something like this to do the buffering:
public SimpleFeature bufferFeature(SimpleFeature feature, Quantity distance) {
// extract the geometry
GeometryAttribute gProp = feature.getDefaultGeometryProperty();
CoordinateReferenceSystem origCRS = gProp.getDescriptor().getCoordinateReferenceSystem();
Geometry geom = (Geometry) feature.getDefaultGeometry();
Geometry retGeom = bufferPoint(distance, origCRS, geom);
// return a new feature containing the geom
SimpleFeatureType schema = feature.getFeatureType();
SimpleFeatureTypeBuilder ftBuilder = new SimpleFeatureTypeBuilder();
ftBuilder.setCRS(origCRS);
for (AttributeDescriptor attrib : schema.getAttributeDescriptors()) {
AttributeType type = attrib.getType();
if (type instanceof GeometryType) {
String oldGeomAttrib = attrib.getLocalName();
ftBuilder.add(oldGeomAttrib, Polygon.class);
} else {
ftBuilder.add(attrib);
}
}
ftBuilder.setName(schema.getName());
SimpleFeatureType nSchema = ftBuilder.buildFeatureType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(nSchema);
List
No comments:
Post a Comment