Search in sources :

Example 11 with ReferencedEnvelope

use of org.geotools.geometry.jts.ReferencedEnvelope 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);
    }
}
Also used : MapContent(org.geotools.map.MapContent) FeatureLayer(org.geotools.map.FeatureLayer) Layer(org.geotools.map.Layer) MapLayer(au.org.emii.portal.menu.MapLayer) BufferedImage(java.awt.image.BufferedImage) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) StreamingRenderer(org.geotools.renderer.lite.StreamingRenderer) FeatureLayer(org.geotools.map.FeatureLayer) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) GTRenderer(org.geotools.renderer.GTRenderer)

Example 12 with ReferencedEnvelope

use of org.geotools.geometry.jts.ReferencedEnvelope 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);
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(com.vividsolutions.jts.geom.Polygon) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 13 with ReferencedEnvelope

use of org.geotools.geometry.jts.ReferencedEnvelope in project GeoGig by boundlessgeo.

the class GeoGigFeatureSourceTest method testGetBounds.

@Test
public void testGetBounds() throws IOException {
    ReferencedEnvelope expected;
    ReferencedEnvelope bounds;
    bounds = pointsSource.getBounds();
    assertNotNull(bounds);
    expected = boundsOf(points1, points2, points3);
    assertEquals(expected, bounds);
    bounds = linesSource.getBounds();
    assertNotNull(bounds);
    expected = boundsOf(lines1, lines2, lines3);
    assertEquals(expected, bounds);
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Test(org.junit.Test)

Example 14 with ReferencedEnvelope

use of org.geotools.geometry.jts.ReferencedEnvelope in project GeoGig by boundlessgeo.

the class RepositoryTestCase method boundsOf.

/**
     * Computes the aggregated bounds of {@code features} in the {@code targetCrs}
     */
public ReferencedEnvelope boundsOf(CoordinateReferenceSystem targetCrs, Feature... features) throws Exception {
    ReferencedEnvelope bounds = new ReferencedEnvelope(targetCrs);
    for (int i = 0; i < features.length; i++) {
        Feature f = features[i];
        BoundingBox fbounds = f.getBounds();
        if (!CRS.equalsIgnoreMetadata(targetCrs, fbounds)) {
            fbounds = fbounds.toBounds(targetCrs);
        }
        bounds.include(fbounds);
    }
    return bounds;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) BoundingBox(org.opengis.geometry.BoundingBox) Feature(org.opengis.feature.Feature)

Example 15 with ReferencedEnvelope

use of org.geotools.geometry.jts.ReferencedEnvelope in project GeoGig by boundlessgeo.

the class BoundsFilteringDiffConsumer method getProjectedFilter.

private ReferencedEnvelope getProjectedFilter(final ObjectId metadataId) {
    ReferencedEnvelope projectedFilter = filtersByMetadataId.get(metadataId);
    if (projectedFilter == null) {
        projectedFilter = createProjectedFilter(metadataId);
        filtersByMetadataId.put(metadataId, projectedFilter);
    }
    return projectedFilter;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope)

Aggregations

ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)20 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)12 Test (org.junit.Test)9 Filter (org.opengis.filter.Filter)4 BoundingBox (org.opengis.geometry.BoundingBox)4 Envelope (com.vividsolutions.jts.geom.Envelope)3 Polygon (com.vividsolutions.jts.geom.Polygon)3 Query (org.geotools.data.Query)3 RevTree (org.locationtech.geogig.api.RevTree)3 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)3 BufferedImage (java.awt.image.BufferedImage)2 List (java.util.List)2 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)2 MapContent (org.geotools.map.MapContent)2 GTRenderer (org.geotools.renderer.GTRenderer)2 StreamingRenderer (org.geotools.renderer.lite.StreamingRenderer)2 ObjectId (org.locationtech.geogig.api.ObjectId)2 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)2 Feature (org.opengis.feature.Feature)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2