public final Geometry transform(Geometry inputGeom) { this.inputGeom = inputGeom; this.factory = inputGeom.getFactory(); if (inputGeom instanceof Point) return transformPoint((Point) inputGeom, null); if (inputGeom instanceof MultiPoint) return transformMultiPoint((MultiPoint) inputGeom, null); if (inputGeom instanceof LinearRing) return transformLinearRing((LinearRing) inputGeom, null); if (inputGeom instanceof LineString) return transformLineString((LineString) inputGeom, null); if (inputGeom instanceof MultiLineString) return transformMultiLineString((MultiLineString) inputGeom, null); if (inputGeom instanceof Polygon) return transformPolygon((Polygon) inputGeom, null); if (inputGeom instanceof MultiPolygon) return transformMultiPolygon((MultiPolygon) inputGeom, null); if (inputGeom instanceof GeometryCollection) return transformGeometryCollection((GeometryCollection) inputGeom, null); throw new IllegalArgumentException("Unknown Geometry subtype: " + inputGeom.getClass().getName()); }
/** * Transforms a {@link CoordinateSequence}. * This method should always return a valid coordinate list for * the desired result type. (E.g. a coordinate list for a LineString * must have 0 or at least 2 points). * If this is not possible, return an empty sequence - * this will be pruned out. * * @param coords the coordinates to transform * @param parent the parent geometry * @return the transformed coordinates */ protected CoordinateSequence transformCoordinates(CoordinateSequence coords, Geometry parent) { return copy(coords); }
/** * Simplifies a MultiPolygon, fixing it if required. */ protected Geometry transformMultiPolygon(MultiPolygon geom, Geometry parent) { Geometry rawGeom = super.transformMultiPolygon(geom, parent); return createValidArea(rawGeom); }
/** * Transforms a {@link LineString} geometry. * * @param geom * @param parent * @return */ protected Geometry transformLineString(LineString geom, Geometry parent) { // should check for 1-point sequences and downgrade them to points return factory.createLineString( transformCoordinates(geom.getCoordinateSequence(), geom)); }
/** * Simplifies a LinearRing. If the simplification results * in a degenerate ring, remove the component. * * @return null if the simplification results in a degenerate ring */ protected Geometry transformLinearRing(LinearRing geom, Geometry parent) { boolean removeDegenerateRings = parent instanceof Polygon; Geometry simpResult = super.transformLinearRing(geom, parent); if (removeDegenerateRings && ! (simpResult instanceof LinearRing)) return null;; return simpResult; }
protected Geometry transformMultiPoint(MultiPoint geom, Geometry parent) { List transGeomList = new ArrayList(); for (int i = 0; i < geom.getNumGeometries(); i++) { Geometry transformGeom = transformPoint((Point) geom.getGeometryN(i), geom); if (transformGeom == null) continue; if (transformGeom.isEmpty()) continue; transGeomList.add(transformGeom); } return factory.buildGeometry(transGeomList); }
protected Geometry transformMultiLineString(MultiLineString geom, Geometry parent) { List transGeomList = new ArrayList(); for (int i = 0; i < geom.getNumGeometries(); i++) { Geometry transformGeom = transformLineString((LineString) geom.getGeometryN(i), geom); if (transformGeom == null) continue; if (transformGeom.isEmpty()) continue; transGeomList.add(transformGeom); } return factory.buildGeometry(transGeomList); }
protected Geometry transformGeometryCollection(GeometryCollection geom, Geometry parent) { List transGeomList = new ArrayList(); for (int i = 0; i < geom.getNumGeometries(); i++) { Geometry transformGeom = transform(geom.getGeometryN(i)); if (transformGeom == null) continue; if (pruneEmptyGeometry && transformGeom.isEmpty()) continue; transGeomList.add(transformGeom); } if (preserveGeometryCollectionType) return factory.createGeometryCollection(GeometryFactory.toGeometryArray(transGeomList)); return factory.buildGeometry(transGeomList); }
/** * Simplifies a MultiPolygon, fixing it if required. */ protected Geometry transformMultiPolygon(MultiPolygon geom, Geometry parent) { Geometry rawGeom = super.transformMultiPolygon(geom, parent); return createValidArea(rawGeom); }
protected Geometry transformPoint(Point geom, Geometry parent) { return factory.createPoint( transformCoordinates(geom.getCoordinateSequence(), geom)); }
/** * Simplifies a LinearRing. If the simplification results in a degenerate * ring, remove the component. * * @return null if the simplification results in a degenerate ring */ protected Geometry transformLinearRing(LinearRing geom, Geometry parent) { boolean removeDegenerateRings = parent instanceof Polygon; Geometry simpResult = super.transformLinearRing(geom, parent); if (removeDegenerateRings && !(simpResult instanceof LinearRing)) return null; ; return simpResult; }
protected Geometry transformMultiPolygon(MultiPolygon geom, Geometry parent) { Geometry roughGeom = super.transformMultiPolygon(geom, parent); return createValidArea(roughGeom); }
protected CoordinateSequence transformCoordinates(CoordinateSequence coords, Geometry parent) { if (coords.size() == 0) return null; // for linear components (including rings), simplify the linestring if (parent instanceof LineString) { TaggedLineString taggedLine = (TaggedLineString) linestringMap.get(parent); return createCoordinateSequence(taggedLine.getResultCoordinates()); } // for anything else (e.g. points) just copy the coordinates return super.transformCoordinates(coords, parent); } }
protected Geometry transformPolygon(Polygon geom, Geometry parent) { boolean isAllValidLinearRings = true; Geometry shell = transformLinearRing(geom.getExteriorRing(), geom); if (shell == null || ! (shell instanceof LinearRing) || shell.isEmpty() ) isAllValidLinearRings = false; ArrayList holes = new ArrayList(); for (int i = 0; i < geom.getNumInteriorRing(); i++) { Geometry hole = transformLinearRing(geom.getInteriorRingN(i), geom); if (hole == null || hole.isEmpty()) { continue; } if (! (hole instanceof LinearRing)) isAllValidLinearRings = false; holes.add(hole); } if (isAllValidLinearRings) return factory.createPolygon((LinearRing) shell, (LinearRing[]) holes.toArray(new LinearRing[] { })); else { List components = new ArrayList(); if (shell != null) components.add(shell); components.addAll(holes); return factory.buildGeometry(components); } }
protected Geometry transformMultiPolygon(MultiPolygon geom, Geometry parent) { Geometry roughGeom = super.transformMultiPolygon(geom, parent); return createValidArea(roughGeom); }
/** * Transforms a LinearRing. * The transformation of a LinearRing may result in a coordinate sequence * which does not form a structurally valid ring (i.e. a degenerate ring of 3 or fewer points). * In this case a LineString is returned. * Subclasses may wish to override this method and check for this situation * (e.g. a subclass may choose to eliminate degenerate linear rings) * * @param geom the ring to simplify * @param parent the parent geometry * @return a LinearRing if the transformation resulted in a structurally valid ring * @return a LineString if the transformation caused the LinearRing to collapse to 3 or fewer points */ protected Geometry transformLinearRing(LinearRing geom, Geometry parent) { CoordinateSequence seq = transformCoordinates(geom.getCoordinateSequence(), geom); if (seq == null) return factory.createLinearRing((CoordinateSequence) null); int seqSize = seq.size(); // ensure a valid LinearRing if (seqSize > 0 && seqSize < 4 && ! preserveType) return factory.createLineString(seq); return factory.createLinearRing(seq); }