use of org.geotools.data.simple.SimpleFeatureCollection in project spatial-portal by AtlasOfLivingAustralia.
the class AreaUploadShapefileWizardController method executeShapeImageRenderer.
private void executeShapeImageRenderer(Filter filter) {
try {
LOGGER.debug("Generating image");
SimpleFeatureCollection features1;
if (filter == null) {
features1 = source.getFeatures();
} else {
features1 = source.getFeatures(filter);
}
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
org.geotools.styling.Style style = SLD.createSimpleStyle(source.getSchema());
Layer layer = new FeatureLayer(features1, style);
map.addLayer(layer);
GTRenderer renderer = new StreamingRenderer();
renderer.setMapContent(map);
int imageWidth = 800;
int imageHeight = 300;
Rectangle imageBounds;
ReferencedEnvelope mapBounds;
mapBounds = map.getMaxBounds();
double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
if (heightToWidth * imageWidth > imageHeight) {
imageBounds = new Rectangle(0, 0, (int) Math.round(imageHeight / heightToWidth), imageHeight);
} else {
imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
}
BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
Graphics2D gr = image.createGraphics();
gr.setPaint(Color.WHITE);
gr.fill(imageBounds);
renderer.paint(gr, imageBounds, mapBounds);
img.setContent(image);
} catch (Exception e) {
LOGGER.debug("Unable to generate image for selected shapefile", e);
}
}
use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class GeoGigDataStoreTest method testDiffFeatures.
private void testDiffFeatures(ObjectId oldRoot, ObjectId newRoot, int expectedAdded, int expectedRemoved, int expectedChanged) throws IOException {
dataStore.setHead(newRoot.toString());
List<String> fids;
SimpleFeatureCollection features;
ChangeType changeType = ChangeType.ADDED;
features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
fids = toIdList(features);
assertEquals(changeType + fids.toString(), expectedAdded, fids.size());
assertEquals(changeType + fids.toString(), expectedAdded, features.size());
changeType = ChangeType.REMOVED;
features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
fids = toIdList(features);
assertEquals(changeType + fids.toString(), expectedRemoved, fids.size());
assertEquals(changeType + fids.toString(), expectedRemoved, features.size());
changeType = ChangeType.CHANGED_NEW;
features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
fids = toIdList(features);
assertEquals(changeType + fids.toString(), expectedChanged, fids.size());
assertEquals(changeType + fids.toString(), expectedChanged, features.size());
changeType = ChangeType.CHANGED_OLD;
features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
fids = toIdList(features);
assertEquals(changeType + fids.toString(), expectedChanged, fids.size());
assertEquals(changeType + fids.toString(), expectedChanged, features.size());
}
use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class GeoGigFeatureSourceTest method testGetFeaturesFilter.
@Test
public void testGetFeaturesFilter() throws Exception {
SimpleFeatureCollection collection;
Set<List<Object>> actual;
Set<List<Object>> expected;
Filter filter;
filter = ff.id(Collections.singleton(ff.featureId(RepositoryTestCase.idP2)));
collection = pointsSource.getFeatures(new Query(pointsName, filter));
actual = Sets.newHashSet();
for (SimpleFeature f : toList(collection)) {
actual.add(f.getAttributes());
}
expected = Collections.singleton(((SimpleFeature) points2).getAttributes());
assertEquals(expected, actual);
ReferencedEnvelope queryBounds = boundsOf(points1, points2);
Polygon geometry = JTS.toGeometry(queryBounds);
filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
collection = pointsSource.getFeatures(new Query(pointsName, filter));
actual = Sets.newHashSet();
for (SimpleFeature f : toList(collection)) {
actual.add(f.getAttributes());
}
expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
assertEquals(expected, actual);
ReferencedEnvelope transformedQueryBounds;
CoordinateReferenceSystem queryCrs = CRS.decode("EPSG:3857");
transformedQueryBounds = queryBounds.transform(queryCrs, true);
geometry = JTS.toGeometry(transformedQueryBounds);
geometry.setUserData(queryCrs);
filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
collection = pointsSource.getFeatures(new Query(pointsName, filter));
actual = Sets.newHashSet();
for (SimpleFeature f : toList(collection)) {
actual.add(f.getAttributes());
}
expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
assertEquals(expected.size(), actual.size());
assertEquals(expected, actual);
filter = ECQL.toFilter("sp = 'StringProp2_3' OR ip = 2000");
collection = linesSource.getFeatures(new Query(linesName, filter));
actual = Sets.newHashSet();
for (SimpleFeature f : toList(collection)) {
actual.add(f.getAttributes());
}
expected = ImmutableSet.of(((SimpleFeature) lines2).getAttributes(), ((SimpleFeature) lines3).getAttributes());
assertEquals(expected, actual);
}
use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportingUsingFunction.
@Test
public void testExportingUsingFunction() throws Exception {
// Testing export of points feature type into a simplified feature type that
// does not contain the integer attribute.
String simplifiedPointsName = "simplifiedPoints";
String simplifiedPointsTypeSpec = "sp:String,pp:Point:srid=4326";
SimpleFeatureType simplifiedPointsType = DataUtilities.createType(pointsNs, simplifiedPointsName, simplifiedPointsTypeSpec);
Feature simplifiedPoints1 = feature(simplifiedPointsType, ((SimpleFeature) points1).getID(), ((SimpleFeature) points1).getAttribute(0), ((SimpleFeature) points1).getAttribute(2));
Feature simplifiedPoints2 = feature(simplifiedPointsType, ((SimpleFeature) points2).getID(), ((SimpleFeature) points2).getAttribute(0), ((SimpleFeature) points2).getAttribute(2));
Feature simplifiedPoints3 = feature(simplifiedPointsType, ((SimpleFeature) points3).getID(), ((SimpleFeature) points3).getAttribute(0), ((SimpleFeature) points3).getAttribute(2));
Feature[] simplifiedPoints = new Feature[] { simplifiedPoints1, simplifiedPoints2, simplifiedPoints3 };
final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(simplifiedPointsType);
Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {
@Override
@Nullable
public Optional<Feature> apply(@Nullable Feature feature) {
SimpleFeature simpleFeature = (SimpleFeature) feature;
featureBuilder.add(simpleFeature.getAttribute(0));
featureBuilder.add(simpleFeature.getAttribute(2));
return Optional.of((Feature) featureBuilder.buildFeature(null));
}
};
Feature[] points = new Feature[] { points1, points2, points3 };
for (Feature feature : points) {
insert(feature);
}
MemoryDataStore dataStore = new MemoryDataStore(simplifiedPointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFeatureTypeConversionFunction(function).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), points.length);
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, simplifiedPoints));
// check for exceptions when using a function that returns features with a wrong featuretype
try {
String wrongFeaturesName = "wrongFeatures";
String wrongFeaturesTypeSpec = "sp:String";
SimpleFeatureType wrongFeaturesType = DataUtilities.createType(pointsNs, wrongFeaturesName, wrongFeaturesTypeSpec);
final SimpleFeatureBuilder wrongFeatureBuilder = new SimpleFeatureBuilder(wrongFeaturesType);
Function<Feature, Optional<Feature>> wrongFunction = new Function<Feature, Optional<Feature>>() {
@Override
@Nullable
public Optional<Feature> apply(@Nullable Feature feature) {
SimpleFeature simpleFeature = (SimpleFeature) feature;
wrongFeatureBuilder.add(simpleFeature.getAttribute(0));
return Optional.of((Feature) wrongFeatureBuilder.buildFeature(null));
}
};
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFeatureTypeConversionFunction(wrongFunction).call();
fail();
} catch (GeoToolsOpException e) {
assertEquals(e.statusCode, StatusCode.UNABLE_TO_ADD);
}
}
use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportWithAlterUsingFeatureTypeId.
@Test
public void testExportWithAlterUsingFeatureTypeId() throws Exception {
Feature[] points = new Feature[] { points2, points1B, points3 };
for (Feature feature : points) {
insert(feature);
}
MemoryDataStore dataStore = new MemoryDataStore(modifiedPointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setAlter(true).setFilterFeatureTypeId(RevFeatureTypeImpl.build(modifiedPointsType).getId()).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), points.length);
SimpleFeatureIterator features = featureCollection.features();
while (features.hasNext()) {
List<Object> attributes = features.next().getAttributes();
assertEquals(4, attributes.size());
}
}
Aggregations