use of com.vividsolutions.jts.geom.LinearRing in project ddf by codice.
the class GmdTransformer method setLocationFromPolygon.
private void setLocationFromPolygon(String boundingPolygon, MetacardImpl metacard) {
try {
String[] stringArray = boundingPolygon.split(" ");
List<Coordinate> coordinates = new ArrayList<>();
for (int i = 0; i < stringArray.length - 1; i += 2) {
coordinates.add(new Coordinate(Double.parseDouble(stringArray[i]), Double.parseDouble(stringArray[i + 1])));
}
LinearRing linearRing = factory.createLinearRing(coordinates.toArray(new Coordinate[coordinates.size()]));
String wkt = WKT_WRITER_THREAD_LOCAL.get().write(factory.createPolygon(linearRing, null));
if (wkt != null) {
metacard.setAttribute(Core.LOCATION, wkt);
}
} catch (NumberFormatException nfe) {
LOGGER.debug("Unable to parse double in {}. Metacard location will not be set.", boundingPolygon);
}
}
use of com.vividsolutions.jts.geom.LinearRing in project ddf by codice.
the class Wfs20JTStoGML321Converter method convertToPolygonType.
public static PolygonType convertToPolygonType(Polygon polygon, String srsName) {
PolygonType polygonType = GML320_OBJECT_FACTORY.createPolygonType();
//exterior
LineString lineString = polygon.getExteriorRing();
LinearRing linearRing = lineString.getFactory().createLinearRing(lineString.getCoordinateSequence());
RingType ringType = convertToRingType(linearRing, srsName);
JAXBElement<RingType> ringTypeJAXBElement = GML320_OBJECT_FACTORY.createRing(ringType);
AbstractRingPropertyType abstractRingPropertyType = GML320_OBJECT_FACTORY.createAbstractRingPropertyType();
abstractRingPropertyType.setAbstractRing(ringTypeJAXBElement);
polygonType.setExterior(abstractRingPropertyType);
//interiors
for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
LineString interiorRingN = polygon.getInteriorRingN(i);
LinearRing linearRing1 = interiorRingN.getFactory().createLinearRing(interiorRingN.getCoordinateSequence());
RingType ringType1 = convertToRingType(linearRing1, srsName);
JAXBElement<RingType> ringTypeJAXBElement1 = GML320_OBJECT_FACTORY.createRing(ringType1);
AbstractRingPropertyType abstractRingPropertyType1 = GML320_OBJECT_FACTORY.createAbstractRingPropertyType();
abstractRingPropertyType1.setAbstractRing(ringTypeJAXBElement1);
polygonType.getInterior().add(abstractRingPropertyType1);
}
polygonType.setSrsName(srsName);
return polygonType;
}
use of com.vividsolutions.jts.geom.LinearRing in project StreetComplete by westnordost.
the class JTSConst method toShellsWithHoles.
private static Map<LinearRing, ArrayList<LinearRing>> toShellsWithHoles(List<List<LatLon>> outerPolygons, List<List<LatLon>> innerPolygons) {
// outer -> List of inner
Map<LinearRing, ArrayList<LinearRing>> shellsWithHoles = new HashMap<>();
for (List<LatLon> outer : outerPolygons) {
LinearRing shell = factory.createLinearRing(toCoordinates(outer));
Geometry outerGeom = factory.createPolygon(shell);
shellsWithHoles.put(shell, null);
Iterator<List<LatLon>> it = innerPolygons.iterator();
while (it.hasNext()) {
List<LatLon> inner = it.next();
LinearRing hole = factory.createLinearRing(toCoordinates(inner));
Geometry holeGeom = factory.createPolygon(hole);
if (outerGeom.contains(holeGeom)) {
if (shellsWithHoles.get(shell) == null) {
shellsWithHoles.put(shell, new ArrayList<>());
}
shellsWithHoles.get(shell).add(hole);
it.remove();
}
}
}
return shellsWithHoles;
}
use of com.vividsolutions.jts.geom.LinearRing in project StreetComplete by westnordost.
the class GeoJsonReader method createPolygon.
private Polygon createPolygon(JSONArray coords) throws JSONException {
LinearRing[] linearRings = new LinearRing[coords.length()];
for (int i = 0; i < coords.length(); i++) {
linearRings[i] = createLinearRing(coords.getJSONArray(i));
}
LinearRing shell = linearRings[0];
ArrayList<LinearRing> inner = new ArrayList<>();
if (linearRings.length > 1) {
LinearRing[] innerArray = new LinearRing[linearRings.length - 1];
System.arraycopy(linearRings, 1, innerArray, 0, linearRings.length - 1);
inner.addAll(Arrays.asList(innerArray));
/* JTS imposes a restriction on a polygon that GeoJSON does not: that the linear rings
that define the holes may not touch each other in more than one point. So, we need to
merge inner linear rings that touch each other in a line together */
mergeHoles(inner);
}
Polygon polygon = factory.createPolygon(shell, inner.toArray(new LinearRing[] {}));
/* in JTS, outer shells are clockwise but in GeoJSON it is specified to be the other way
round. This reader is forgiving: It does not care about the direction, it will just
reorder if necessary (part of normalize) */
polygon.normalize();
return polygon;
}
use of com.vividsolutions.jts.geom.LinearRing in project StreetComplete by westnordost.
the class GeoJsonReader method mergeHoles.
private void mergeHoles(ArrayList<LinearRing> rings) {
if (rings.size() == 1)
return;
// need to be converted to polygons and back because linearring is a lineal data structure,
// we want to merge by area
ArrayList<Polygon> polygons = new ArrayList<>(rings.size());
for (LinearRing ring : rings) {
polygons.add(factory.createPolygon(ring));
}
mergePolygons(polygons);
// something was merged. Convert polygons back to rings
if (polygons.size() != rings.size()) {
rings.clear();
for (Polygon polygon : polygons) {
rings.add((LinearRing) polygon.getExteriorRing());
}
}
}
Aggregations