Search in sources :

Example 1 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project spatial-portal by AtlasOfLivingAustralia.

the class ShapefileUtils method saveShapefile.

public static void saveShapefile(File shpfile, String wktString, String name) {
    try {
        final SimpleFeatureType type = createFeatureType();
        List<SimpleFeature> features = new ArrayList<SimpleFeature>();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
        WKTReader wkt = new WKTReader();
        Geometry geom = wkt.read(wktString);
        if (geom instanceof GeometryCollection) {
            GeometryCollection gc = (GeometryCollection) geom;
            for (int i = 0; i < gc.getNumGeometries(); i++) {
                Geometry g = gc.getGeometryN(i);
                if (g instanceof Polygon) {
                    g = new GeometryBuilder().multiPolygon((Polygon) g);
                }
                featureBuilder.add(g);
                SimpleFeature feature = featureBuilder.buildFeature(null);
                feature.setAttribute("name", name);
                features.add(feature);
            }
        } else {
            Geometry g = geom;
            if (g instanceof Polygon) {
                g = new GeometryBuilder().multiPolygon((Polygon) g);
            }
            featureBuilder.add(g);
            SimpleFeature feature = featureBuilder.buildFeature(null);
            features.add(feature);
        }
        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("url", shpfile.toURI().toURL());
        params.put("create spatial index", Boolean.TRUE);
        ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
        newDataStore.createSchema(type);
        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
        Transaction transaction = new DefaultTransaction("create");
        String typeName = newDataStore.getTypeNames()[0];
        SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
        if (featureSource instanceof SimpleFeatureStore) {
            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
            DefaultFeatureCollection collection = new DefaultFeatureCollection();
            collection.addAll(features);
            featureStore.setTransaction(transaction);
            try {
                featureStore.addFeatures(collection);
                transaction.commit();
            } catch (Exception problem) {
                LOGGER.error("error pricessing shape file: " + shpfile.getAbsolutePath(), problem);
                transaction.rollback();
            } finally {
                transaction.close();
            }
        }
        LOGGER.debug("Active Area shapefile written to: " + shpfile.getAbsolutePath());
    } catch (Exception e) {
        LOGGER.error("Unable to save shapefile: " + shpfile.getAbsolutePath(), e);
    }
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) WKTReader(com.vividsolutions.jts.io.WKTReader) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) GeometryBuilder(org.geotools.geometry.jts.GeometryBuilder) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 2 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project ddf by codice.

the class TestGeoJsonInputTransformer method testGeometryCollectionStringGeo.

@Test
public void testGeometryCollectionStringGeo() throws IOException, CatalogTransformerException, ParseException {
    InputStream inputStream = new ByteArrayInputStream(sampleGeometryCollectionJsonText().getBytes());
    Metacard metacard = transformer.transform(inputStream);
    verifyBasics(metacard);
    WKTReader reader = new WKTReader();
    Geometry geometry = reader.read(metacard.getLocation());
    assertThat(geometry.getNumGeometries(), is(2));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WKTReader(com.vividsolutions.jts.io.WKTReader) Test(org.junit.Test)

Example 3 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project ddf by codice.

the class TestGeoJsonExtensible method verifyBasics.

private void verifyBasics(Metacard metacard) throws ParseException {
    assertEquals(DEFAULT_TITLE, metacard.getTitle());
    assertEquals(DEFAULT_URI, metacard.getResourceURI().toString());
    assertEquals(DEFAULT_TYPE, metacard.getContentTypeName());
    assertEquals(DEFAULT_VERSION, metacard.getContentTypeVersion());
    assertEquals("<xml></xml>", metacard.getMetadata());
    SimpleDateFormat dateFormat = new SimpleDateFormat(GeoJsonInputTransformer.ISO_8601_DATE_FORMAT);
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    assertEquals(DEFAULT_CREATED_DATE, dateFormat.format(metacard.getCreatedDate()));
    assertEquals(DEFAULT_MODIFIED_DATE, dateFormat.format(metacard.getModifiedDate()));
    assertEquals(DEFAULT_EXPIRATION_DATE, dateFormat.format(metacard.getExpirationDate()));
    assertEquals(DEFAULT_EFFECTIVE_DATE, dateFormat.format(metacard.getEffectiveDate()));
    assertArrayEquals(DEFAULT_BYTES, metacard.getThumbnail());
    assertEquals(DEFAULT_TEMPERATURE, metacard.getAttribute(TEMPERATURE_KEY).getValue());
    assertEquals(BASIC_METACARD.getName(), metacard.getMetacardType().getName());
    WKTReader reader = new WKTReader();
    Geometry geometry = reader.read(metacard.getLocation());
    Coordinate[] coords = geometry.getCoordinates();
    assertThat(coords[0].x, is(30.0));
    assertThat(coords[0].y, is(10.0));
    assertThat(coords[1].x, is(10.0));
    assertThat(coords[1].y, is(30.0));
    assertThat(coords[2].x, is(40.0));
    assertThat(coords[2].y, is(40.0));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Coordinate(com.vividsolutions.jts.geom.Coordinate) WKTReader(com.vividsolutions.jts.io.WKTReader) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project ddf by codice.

the class TestGeoJsonInputTransformer method testPointGeo.

@Test()
public void testPointGeo() throws IOException, CatalogTransformerException, ParseException {
    Metacard metacard = transformer.transform(new ByteArrayInputStream(samplePointJsonText().getBytes()));
    verifyBasics(metacard);
    WKTReader reader = new WKTReader();
    Geometry geometry = reader.read(metacard.getLocation());
    assertThat(geometry.getCoordinate().x, is(30.0));
    assertThat(geometry.getCoordinate().y, is(10.0));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) WKTReader(com.vividsolutions.jts.io.WKTReader) Test(org.junit.Test)

Example 5 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project GeoGig by boundlessgeo.

the class GeometryDiffTest method testNoNewGeometry.

@Test
public void testNoNewGeometry() throws Exception {
    Geometry oldGeom = new WKTReader().read("MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))");
    LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(Optional.of(oldGeom), Optional.fromNullable((Geometry) null));
    LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText());
    assertEquals(diff, deserializedDiff);
    assertEquals("9 point(s) deleted, 0 new point(s) added, 0 point(s) moved", diff.toString());
    Optional<Geometry> resultingGeom = diff.applyOn(Optional.of(oldGeom));
    assertFalse(resultingGeom.isPresent());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) WKTReader(com.vividsolutions.jts.io.WKTReader) Test(org.junit.Test)

Aggregations

WKTReader (com.vividsolutions.jts.io.WKTReader)71 Geometry (com.vividsolutions.jts.geom.Geometry)51 Test (org.junit.Test)28 ParseException (com.vividsolutions.jts.io.ParseException)23 WKTWriter (com.vividsolutions.jts.io.WKTWriter)9 Metacard (ddf.catalog.data.Metacard)9 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 IsValidOp (com.vividsolutions.jts.operation.valid.IsValidOp)6 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)6 GeometryOperator (org.codice.alliance.libs.klv.GeometryOperator)6 Context (org.codice.alliance.video.stream.mpegts.Context)6 MapLayer (au.org.emii.portal.menu.MapLayer)5 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)4 Polygon (com.vividsolutions.jts.geom.Polygon)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Optional (java.util.Optional)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Before (org.junit.Before)3 Facet (au.org.ala.legend.Facet)2