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;
}
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);
}
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);
}
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;
}
Aggregations