Search in sources :

Example 21 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project spatial-portal by AtlasOfLivingAustralia.

the class PointGenerationComposer method onFinish.

@Override
public boolean onFinish() {
    SelectedArea sa = getSelectedArea();
    double[][] bbox = null;
    if (sa.getMapLayer() != null && sa.getMapLayer().getMapLayerMetadata() != null) {
        List<Double> bb = sa.getMapLayer().getMapLayerMetadata().getBbox();
        bbox = new double[2][2];
        bbox[0][0] = bb.get(0);
        bbox[0][1] = bb.get(1);
        bbox[1][0] = bb.get(2);
        bbox[1][1] = bb.get(3);
    } else {
        List<Double> bb = Util.getBoundingBox(sa.getWkt());
        bbox = new double[][] { { bb.get(0), bb.get(1) }, { bb.get(2), bb.get(3) } };
    }
    //with bounding box, cut points
    try {
        String wkt = (sa.getMapLayer() != null ? sa.getMapLayer().getWKT() : sa.getWkt());
        StringBuilder sb = new StringBuilder();
        sb.append("longitude,latitude");
        int count = 0;
        int width = (int) Math.ceil((bbox[1][0] - bbox[0][0]) / resolution.getValue());
        int height = (int) Math.ceil((bbox[1][1] - bbox[0][1]) / resolution.getValue());
        double startx = Math.floor(bbox[0][0] / resolution.getValue()) * resolution.getValue();
        double starty = Math.floor(bbox[0][1] / resolution.getValue()) * resolution.getValue();
        if (wkt == null) {
            if (sa.getFacets() != null) {
                double[][] points = new double[(width + 1) * (height + 1)][2];
                int pos = 0;
                for (int i = 0; i <= width; i++) {
                    for (int j = 0; j <= height; j++) {
                        double lng = startx + i * resolution.getValue();
                        double lat = starty + j * resolution.getValue();
                        points[pos][0] = lng;
                        points[pos][1] = lat;
                        pos = pos + 1;
                    }
                }
                List<String> fields = new ArrayList<String>();
                for (Facet f : sa.getFacets()) {
                    fields.add(f.getFields()[0]);
                }
                List<String[]> result = Sampling.sampling(fields, points);
                for (int i = 0; i < result.size(); i++) {
                    Facet f = sa.getFacets().get(i);
                    String[] intersection = result.get(i);
                    for (int j = 0; j < intersection.length; j++) {
                        if (f.isValid("\"" + intersection[j] + "\"")) {
                            sb.append("\n");
                            sb.append(points[j][0]).append(",").append(points[j][1]);
                            count++;
                        }
                    }
                }
            } else {
                LOGGER.error("invalid area selected for point generation");
                getMapComposer().showMessage("Unsupported area selected for point generation. Choose a different area.");
                return false;
            }
        } else {
            //write temporary shapefile
            File tmp = File.createTempFile("tmp", ".shp");
            ShapefileUtils.saveShapefile(tmp, wkt, "tmp");
            FileDataStore store = FileDataStoreFinder.getDataStore(tmp);
            FeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
            FeatureCollection featureCollection = featureSource.getFeatures();
            GeometryFactory gf = new GeometryFactory();
            List<Geometry> geometry = new ArrayList<Geometry>();
            FeatureIterator it = featureCollection.features();
            while (it.hasNext()) {
                SimpleFeature feature = (SimpleFeature) it.next();
                geometry.add((Geometry) feature.getDefaultGeometry());
            }
            try {
                it.close();
            } catch (Exception e) {
            }
            for (int i = 0; i <= width; i++) {
                for (int j = 0; j <= height; j++) {
                    double lng = startx + i * resolution.getValue();
                    double lat = starty + j * resolution.getValue();
                    Coordinate c = new Coordinate(lng, lat);
                    Geometry point = gf.createPoint(c);
                    for (Geometry geom : geometry) {
                        if (geom.contains(point)) {
                            sb.append("\n");
                            sb.append(lng).append(",").append(lat);
                            count++;
                        }
                    }
                }
            }
            //close tmp file
            try {
                store.dispose();
            } catch (Exception e) {
            }
            //delete tmp files
            try {
                FileUtils.deleteQuietly(tmp);
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".shx")));
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".fix")));
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".dbf")));
                FileUtils.deleteQuietly(new File(tmp.getPath().replace(".shp", ".prj")));
            } catch (Exception e) {
            }
        }
        if (count <= 0) {
            getMapComposer().showMessage("No points generated. Try again with a smaller resolution or larger area.");
        } else if (count > Integer.parseInt(CommonData.getSettings().getProperty("max_record_count_upload"))) {
            getMapComposer().showMessage(count + " points generated. Maximum is " + CommonData.getSettings().getProperty("generated_points_max") + ".\n" + "Try again with a larger resolution or smaller area.");
        } else {
            String name = tToolName.getValue();
            try {
                UploadSpeciesController.loadUserPoints(new UserDataDTO(name), new StringReader(sb.toString()), true, name, "points in " + getSelectedAreaDisplayName() + " on " + resolution.getValue() + " degree resolution grid.", getMapComposer(), null);
                detach();
                return true;
            } catch (Exception e) {
                LOGGER.error("failed to upload points");
            }
        }
    } catch (Exception e) {
    }
    return false;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) SelectedArea(au.org.emii.portal.menu.SelectedArea) ArrayList(java.util.ArrayList) FeatureIterator(org.geotools.feature.FeatureIterator) StringReader(java.io.StringReader) UserDataDTO(au.org.ala.spatial.dto.UserDataDTO) FileDataStore(org.geotools.data.FileDataStore) Facet(au.org.ala.legend.Facet) FeatureSource(org.geotools.data.FeatureSource) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(com.vividsolutions.jts.geom.Geometry) FeatureCollection(org.geotools.feature.FeatureCollection) Coordinate(com.vividsolutions.jts.geom.Coordinate) File(java.io.File)

Example 22 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project series-rest-api by 52North.

the class CRSUtils method createPoint.

/**
     * Creates a 2D point geometry with respect to its height and given reference system.
     *
     * @param x the point's x value.
     * @param y the point's y value.
     * @param z the height or <code>null</code> or <code>NaN</code> if coordinate is 2D.
     * @param srs an authoritive spatial reference system code, e.g. <code>EPSG:4326</code> or
     * <code>CRS:84</code> .
     * @return a point referenced by the given spatial reference system.
     */
public Point createPoint(Double x, Double y, Double z, String srs) {
    Coordinate coordinate = z != null && !z.isNaN() ? new Coordinate(x, y, z) : new Coordinate(x, y);
    GeometryFactory factory = createGeometryFactory(srs);
    return factory.createPoint(coordinate);
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate)

Example 23 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project series-rest-api by 52North.

the class GeotoolsJTSReferenceTester method shouldBlah.

@Test
public void shouldBlah() throws Exception {
    CRSUtils forcedXYOrder = CRSUtils.createEpsgForcedXYAxisOrder();
    GeometryFactory xyFactory = forcedXYOrder.createGeometryFactory("EPSG:4326");
    Point forcedXYPoint = xyFactory.createPoint(new Coordinate(7.4, 52.3));
    LOGGER.info("EPSG:4326 as JTS point (forced XY): {}", forcedXYPoint);
    LOGGER.info("Transformed to EPSG:25832: {}", forcedXYOrder.transform(forcedXYPoint, "EPSG:4326", "EPSG:25832"));
    CRSUtils respectEpsgOrder = CRSUtils.createEpsgStrictAxisOrder();
    GeometryFactory strictFactory = respectEpsgOrder.createGeometryFactory("EPSG:4326");
    Point strictPoint = strictFactory.createPoint(new Coordinate(52.3, 7.4));
    LOGGER.info("EPSG:4326 as JTS point (strict EPSG order): {}", strictPoint);
    LOGGER.info("Transformed to EPSG:25832: {}", respectEpsgOrder.transform(strictPoint, "EPSG:4326", "EPSG:25832"));
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point(com.vividsolutions.jts.geom.Point) Test(org.junit.Test)

Example 24 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project GeoGig by boundlessgeo.

the class ImportOpTest method testAdaptFeatureType.

@Test
public void testAdaptFeatureType() throws Exception {
    ImportOp importOp = geogig.command(ImportOp.class);
    importOp.setDataStore(TestHelper.createTestFactory().createDataStore(null));
    importOp.setTable("shpLikeTable");
    importOp.setDestinationPath("table");
    importOp.call();
    Optional<RevFeature> feature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:table/feature1").call(RevFeature.class);
    assertTrue(feature.isPresent());
    RevFeatureType originalFeatureType = geogig.command(ResolveFeatureType.class).setRefSpec("WORK_HEAD:table/feature1").call().get();
    importOp.setTable("shpLikeTable2");
    importOp.call();
    feature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:table/feature1").call(RevFeature.class);
    assertTrue(feature.isPresent());
    RevFeatureType featureType = geogig.command(ResolveFeatureType.class).setRefSpec("WORK_HEAD:table/feature1").call().get();
    assertEquals(originalFeatureType.getId(), featureType.getId());
    GeometryFactory gf = new GeometryFactory();
    ImmutableList<Optional<Object>> values = feature.get().getValues();
    assertEquals(values.get(0).get(), gf.createPoint(new Coordinate(0, 7)));
    assertEquals(values.get(1).get(), 3.2);
    assertEquals(values.get(2).get(), 1100.0);
    importOp.setTable("GeoJsonLikeTable");
    importOp.call();
    feature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:table/feature1").call(RevFeature.class);
    assertTrue(feature.isPresent());
    featureType = geogig.command(ResolveFeatureType.class).setRefSpec("WORK_HEAD:table/feature1").call().get();
    assertEquals(originalFeatureType.getId(), featureType.getId());
    values = feature.get().getValues();
    assertEquals(values.get(0).get(), gf.createPoint(new Coordinate(0, 8)));
    assertEquals(values.get(1).get(), 4.2);
    assertEquals(values.get(2).get(), 1200.0);
}
Also used : ResolveFeatureType(org.locationtech.geogig.api.plumbing.ResolveFeatureType) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Optional(com.google.common.base.Optional) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Example 25 with GeometryFactory

use of com.vividsolutions.jts.geom.GeometryFactory in project GeoGig by boundlessgeo.

the class TestHelper method createTestFactory.

public static AbstractDataStoreFactory createTestFactory() throws Exception {
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setCRS(CRS.decode("EPSG:4326"));
    builder.add("geom", Point.class);
    builder.add("label", String.class);
    builder.setName("table1");
    SimpleFeatureType type = builder.buildFeatureType();
    SimpleFeatureTypeBuilder builder2 = new SimpleFeatureTypeBuilder();
    builder2.setCRS(CRS.decode("EPSG:4326"));
    builder2.add("geom", Point.class);
    builder2.add("name", String.class);
    builder2.setName("table2");
    SimpleFeatureType type2 = builder2.buildFeatureType();
    SimpleFeatureTypeBuilder builder3 = new SimpleFeatureTypeBuilder();
    builder3.setCRS(CRS.decode("EPSG:4326"));
    builder3.add("geom", Point.class);
    builder3.add("name", String.class);
    builder3.add("number", Long.class);
    builder3.setName("table3");
    SimpleFeatureTypeBuilder builder4 = new SimpleFeatureTypeBuilder();
    builder4.setCRS(CRS.decode("EPSG:4326"));
    builder4.add("geom", Point.class);
    builder4.add("number", Double.class);
    builder4.setName("table4");
    // A table with a shp-like structure
    SimpleFeatureTypeBuilder builderShp = new SimpleFeatureTypeBuilder();
    builderShp.setCRS(CRS.decode("EPSG:4326"));
    builderShp.add("the_geom", Point.class);
    builderShp.add("number", Double.class);
    builderShp.add("number2", Double.class);
    builderShp.setName("shpLikeTable");
    SimpleFeatureTypeBuilder builderShp2 = new SimpleFeatureTypeBuilder();
    builderShp2.setCRS(CRS.decode("EPSG:4326"));
    builderShp2.add("the_geom", Point.class);
    builderShp2.add("number", Double.class);
    builderShp2.add("number2", Integer.class);
    builderShp2.setName("shpLikeTable2");
    // A table with a geojson-like structure
    SimpleFeatureTypeBuilder builderGeoJson = new SimpleFeatureTypeBuilder();
    builderGeoJson.setCRS(CRS.decode("EPSG:4326"));
    builderGeoJson.add("number", Double.class);
    builderGeoJson.add("number2", Double.class);
    builderGeoJson.add("geom", Point.class);
    builderGeoJson.setName("GeoJsonLikeTable");
    SimpleFeatureTypeBuilder builderGeoJson2 = new SimpleFeatureTypeBuilder();
    builderGeoJson2.setCRS(CRS.decode("EPSG:23030"));
    builderGeoJson2.add("number", Double.class);
    builderGeoJson2.add("number2", Double.class);
    builderGeoJson2.add("geom", Point.class);
    builderGeoJson2.setName("GeoJsonLikeTable2");
    SimpleFeatureType type3 = builder3.buildFeatureType();
    SimpleFeatureType typeShp = builderShp.buildFeatureType();
    SimpleFeatureType typeShp2 = builderShp2.buildFeatureType();
    SimpleFeatureType typeGeoJson = builderGeoJson.buildFeatureType();
    SimpleFeatureType typeGeoJson2 = builderGeoJson2.buildFeatureType();
    GeometryFactory gf = new GeometryFactory();
    SimpleFeature f1 = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(5, 8)), "feature1" }, "table1.feature1");
    SimpleFeature f2 = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(5, 4)), "feature2" }, "table1.feature2");
    SimpleFeature f3 = SimpleFeatureBuilder.build(type2, new Object[] { gf.createPoint(new Coordinate(3, 2)), "feature3" }, "table2.feature3");
    SimpleFeature f4 = SimpleFeatureBuilder.build(type3, new Object[] { gf.createPoint(new Coordinate(0, 5)), "feature4", 1000 }, "table2.feature4");
    SimpleFeature f5 = SimpleFeatureBuilder.build(typeShp, new Object[] { gf.createPoint(new Coordinate(0, 6)), 2.2, 1000 }, "feature1");
    SimpleFeature f6 = SimpleFeatureBuilder.build(typeShp2, new Object[] { gf.createPoint(new Coordinate(0, 7)), 3.2, 1100.0 }, "feature1");
    SimpleFeature f7 = SimpleFeatureBuilder.build(typeGeoJson, new Object[] { 4.2, 1200, gf.createPoint(new Coordinate(0, 8)) }, "feature1");
    SimpleFeature f8 = SimpleFeatureBuilder.build(typeGeoJson2, new Object[] { 4.2, 1200, gf.createPoint(new Coordinate(0, 9)) }, "feature1");
    MemoryDataStore testDataStore = new MemoryDataStore();
    testDataStore.addFeature(f1);
    testDataStore.addFeature(f2);
    testDataStore.addFeature(f3);
    testDataStore.addFeature(f4);
    testDataStore.addFeature(f5);
    testDataStore.addFeature(f6);
    testDataStore.addFeature(f7);
    testDataStore.addFeature(f8);
    testDataStore.createSchema(builder4.buildFeatureType());
    final AbstractDataStoreFactory factory = mock(AbstractDataStoreFactory.class);
    when(factory.createDataStore(anyMapOf(String.class, Serializable.class))).thenReturn(testDataStore);
    when(factory.canProcess(anyMapOf(String.class, Serializable.class))).thenReturn(true);
    return factory;
}
Also used : AbstractDataStoreFactory(org.geotools.data.AbstractDataStoreFactory) Serializable(java.io.Serializable) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(com.vividsolutions.jts.geom.Coordinate) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Matchers.anyString(org.mockito.Matchers.anyString) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Aggregations

GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)33 Coordinate (com.vividsolutions.jts.geom.Coordinate)27 Test (org.junit.Test)21 Geometry (com.vividsolutions.jts.geom.Geometry)13 SimpleFeature (org.opengis.feature.simple.SimpleFeature)12 ArrayList (java.util.ArrayList)10 Point (com.vividsolutions.jts.geom.Point)9 File (java.io.File)9 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)8 Optional (com.google.common.base.Optional)7 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)7 RevFeature (org.locationtech.geogig.api.RevFeature)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 AddOp (org.locationtech.geogig.api.porcelain.AddOp)6 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)5 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)5 Envelope (com.vividsolutions.jts.geom.Envelope)4 IOException (java.io.IOException)4 Serializable (java.io.Serializable)4