Search in sources :

Example 1 with MultiGeometry

use of org.geotoolkit.data.kml.model.MultiGeometry in project geotoolkit by Geomatys.

the class KmlFeatureUtilities method extractFeature.

/**
 * create a {@link SimpleFeature} from an {@link Map.Entry}
 * @param idgeom current id iterator
 * @param geometry : {@link Map.Entry} which contains a geometry
 * @return a {@link SimpleFeature}
 */
private static Feature extractFeature(int idgeom, Map.Entry<Object, Map<String, String>> geometry) {
    final Object geom = geometry.getKey();
    Geometry finalGeom = null;
    // if it's a simple geometry
    if (geom instanceof Geometry) {
        finalGeom = (Geometry) geometry.getKey();
        try {
            // if it's lineString it can cut meridian. So test it
            if (finalGeom instanceof LineString) {
                LineString lineString = (LineString) finalGeom;
                MultiLineString multiLine = cutAtMeridian(lineString);
                if (multiLine != null) {
                    finalGeom = multiLine;
                }
            }
        } catch (Exception ex) {
            LOGGER.log(Level.WARNING, ex.getMessage(), ex);
        }
    } else // it's a geometry collection
    if (geom instanceof DefaultMultiGeometry) {
        final DefaultMultiGeometry kmlabstractGeometry = (DefaultMultiGeometry) geom;
        final List<Geometry> multiGeometry = new ArrayList<>(0);
        // loop on geometry to add id on a GeometryList
        for (AbstractGeometry abstractGeometry : kmlabstractGeometry.getGeometries()) {
            if (abstractGeometry instanceof Geometry) {
                final Geometry currentGeom = (Geometry) abstractGeometry;
                multiGeometry.add(currentGeom);
            }
        }
        final GeometryFactory gf = JTS.getFactory();
        Geometry[] geometryArray = new Geometry[multiGeometry.size()];
        for (int i = 0; i < multiGeometry.size(); i++) {
            geometryArray[i] = multiGeometry.get(i);
        }
        finalGeom = new GeometryCollection(geometryArray, gf);
    }
    if (finalGeom != null) {
        return BuildSimpleFeature(idgeom, geometry.getValue(), finalGeom);
    }
    return null;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) DefaultMultiGeometry(org.geotoolkit.data.kml.model.DefaultMultiGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) MultiLineString(org.locationtech.jts.geom.MultiLineString) AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) DefaultMultiGeometry(org.geotoolkit.data.kml.model.DefaultMultiGeometry) List(java.util.List) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException)

Example 2 with MultiGeometry

use of org.geotoolkit.data.kml.model.MultiGeometry in project geotoolkit by Geomatys.

the class MultiGeometryTest method multiGeometryReadTest.

@Test
public void multiGeometryReadTest() throws IOException, XMLStreamException, KmlException, URISyntaxException {
    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();
    final Feature placemark = kmlObjects.getAbstractFeature();
    assertEquals("SF Marina Harbor Master", placemark.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals(Boolean.FALSE, placemark.getPropertyValue(KmlConstants.TAG_VISIBILITY));
    final MultiGeometry multiGeometry = (MultiGeometry) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
    assertEquals(2, multiGeometry.getGeometries().size());
    LineString lineString0 = (LineString) multiGeometry.getGeometries().get(0);
    LineString lineString1 = (LineString) multiGeometry.getGeometries().get(1);
    final CoordinateSequence coordinates0 = lineString0.getCoordinateSequence();
    final CoordinateSequence coordinates1 = lineString1.getCoordinateSequence();
    assertEquals(2, coordinates0.size());
    assertEquals(2, coordinates1.size());
    final Coordinate coordinate00 = coordinates0.getCoordinate(0);
    final Coordinate coordinate01 = coordinates0.getCoordinate(1);
    final Coordinate coordinate10 = coordinates1.getCoordinate(0);
    final Coordinate coordinate11 = coordinates1.getCoordinate(1);
    assertEquals(-122.4425587930444, coordinate00.x, DELTA);
    assertEquals(37.80666418607323, coordinate00.y, DELTA);
    assertEquals(0, coordinate00.z, DELTA);
    assertEquals(-122.4428379594768, coordinate01.x, DELTA);
    assertEquals(37.80663578323093, coordinate01.y, DELTA);
    assertEquals(0, coordinate01.z, DELTA);
    assertEquals(-122.4425509770566, coordinate10.x, DELTA);
    assertEquals(37.80662588061205, coordinate10.y, DELTA);
    assertEquals(0, coordinate10.z, DELTA);
    assertEquals(-122.4428340530617, coordinate11.x, DELTA);
    assertEquals(37.8065999493009, coordinate11.y, DELTA);
    assertEquals(0, coordinate11.z, DELTA);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) LineString(org.geotoolkit.data.kml.model.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) Kml(org.geotoolkit.data.kml.model.Kml) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) File(java.io.File) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 3 with MultiGeometry

use of org.geotoolkit.data.kml.model.MultiGeometry in project geotoolkit by Geomatys.

the class MultiGeometryTest method multiGeometryWriteTest.

@Test
public void multiGeometryWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Coordinate coordinate00 = kmlFactory.createCoordinate("-122.4425587930444,37.80666418607323,0.0");
    final Coordinate coordinate01 = kmlFactory.createCoordinate("-122.4428379594768,37.80663578323093,0.0");
    final Coordinate coordinate10 = kmlFactory.createCoordinate("-122.4425509770566,37.80662588061205,0.0");
    final Coordinate coordinate11 = kmlFactory.createCoordinate("-122.4428340530617,37.8065999493009,0.0");
    final CoordinateSequence coordinates0 = kmlFactory.createCoordinates(Arrays.asList(coordinate00, coordinate01));
    final CoordinateSequence coordinates1 = kmlFactory.createCoordinates(Arrays.asList(coordinate10, coordinate11));
    final LineString lineString0 = kmlFactory.createLineString(coordinates0);
    final LineString lineString1 = kmlFactory.createLineString(coordinates1);
    final MultiGeometry multiGeometry = kmlFactory.createMultiGeometry();
    multiGeometry.setGeometries(Arrays.asList((AbstractGeometry) lineString0, (AbstractGeometry) lineString1));
    final Feature placemark = kmlFactory.createPlacemark();
    placemark.setPropertyValue(KmlConstants.TAG_NAME, "SF Marina Harbor Master");
    placemark.setPropertyValue(KmlConstants.TAG_VISIBILITY, Boolean.FALSE);
    placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, multiGeometry);
    final Kml kml = kmlFactory.createKml(null, placemark, null, null);
    final File temp = File.createTempFile("testMultiGeometry", ".kml");
    temp.deleteOnExit();
    final KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.write(kml);
    writer.dispose();
    DomCompare.compare(new File(pathToTestFile), temp);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.geotoolkit.data.kml.model.LineString) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) Kml(org.geotoolkit.data.kml.model.Kml) Feature(org.opengis.feature.Feature) File(java.io.File) Test(org.junit.Test)

Example 4 with MultiGeometry

use of org.geotoolkit.data.kml.model.MultiGeometry in project geotoolkit by Geomatys.

the class KmlFeatureUtilities method buildKMLGeometry.

private static AbstractGeometry buildKMLGeometry(Geometry geometry) {
    Class<?> geometryClass = geometry.getClass();
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    if (geometryClass.equals(LineString.class)) {
        LineString ls = (LineString) geometry;
        org.geotoolkit.data.kml.model.LineString lineString = kmlFactory.createLineString(ls.getCoordinateSequence());
        return lineString;
    } else if (geometryClass.equals(Point.class)) {
        Point point = (Point) geometry;
        org.geotoolkit.data.kml.model.Point kmlPoint = kmlFactory.createPoint(point.getCoordinateSequence());
        return kmlPoint;
    } else if (geometryClass.equals(Polygon.class)) {
        final Polygon poly = (Polygon) geometry;
        // interiorRing
        final List<Boundary> innerBoundaries = new ArrayList<>(0);
        int innerRing = poly.getNumInteriorRing();
        if (innerRing > 0) {
            for (int i = 0; i < innerRing; i++) {
                final LineString inner = poly.getInteriorRingN(i);
                final org.geotoolkit.data.kml.model.LinearRing innerLineRing = kmlFactory.createLinearRing(inner.getCoordinateSequence());
                final Boundary innerBoundary = kmlFactory.createBoundary(innerLineRing, null, null);
                innerBoundaries.add(innerBoundary);
            }
        }
        // exterior ring
        final org.geotoolkit.data.kml.model.LinearRing lr = kmlFactory.createLinearRing(poly.getExteriorRing().getCoordinateSequence());
        final Boundary boundary = kmlFactory.createBoundary(lr, null, null);
        final org.geotoolkit.data.kml.model.Polygon kmlPolygon = kmlFactory.createPolygon(boundary, innerBoundaries);
        return kmlPolygon;
    } else if (GeometryCollection.class.isAssignableFrom(geometryClass)) {
        final GeometryCollection geoCollec = (GeometryCollection) geometry;
        final MultiGeometry mg = kmlFactory.createMultiGeometry();
        final List<AbstractGeometry> geometries = new ArrayList<>(0);
        for (int i = 0; i < geoCollec.getNumGeometries(); i++) {
            Geometry currentGeometry = geoCollec.getGeometryN(i);
            AbstractGeometry ag = buildKMLGeometry(currentGeometry);
            geometries.add(ag);
        }
        mg.setGeometries(geometries);
        return mg;
    }
    return null;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) ArrayList(java.util.ArrayList) Point(org.locationtech.jts.geom.Point) Point(org.locationtech.jts.geom.Point) Boundary(org.geotoolkit.data.kml.model.Boundary) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) DefaultMultiGeometry(org.geotoolkit.data.kml.model.DefaultMultiGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) List(java.util.List) ArrayList(java.util.ArrayList) DefaultMultiGeometry(org.geotoolkit.data.kml.model.DefaultMultiGeometry) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) Polygon(org.locationtech.jts.geom.Polygon)

Aggregations

MultiGeometry (org.geotoolkit.data.kml.model.MultiGeometry)4 AbstractGeometry (org.geotoolkit.data.kml.model.AbstractGeometry)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DefaultMultiGeometry (org.geotoolkit.data.kml.model.DefaultMultiGeometry)2 Kml (org.geotoolkit.data.kml.model.Kml)2 LineString (org.geotoolkit.data.kml.model.LineString)2 Test (org.junit.Test)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)2 Geometry (org.locationtech.jts.geom.Geometry)2 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)2 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 Feature (org.opengis.feature.Feature)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Boundary (org.geotoolkit.data.kml.model.Boundary)1 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)1