Search in sources :

Example 1 with FeatureGeometrySubstitutor

use of org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor in project hortonmachine by TheHortonMachine.

the class OmsLineSmootherMcMaster method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outVector == null, doReset)) {
        return;
    }
    if (pDensify != null) {
        densify = pDensify;
    }
    if (pSimplify != null) {
        simplify = pSimplify;
    }
    outVector = new DefaultFeatureCollection();
    pm.message("Collecting geometries...");
    linesList = FeatureUtilities.featureCollectionToList(inVector);
    int size = inVector.size();
    FeatureGeometrySubstitutor fGS = new FeatureGeometrySubstitutor(inVector.getSchema());
    pm.beginTask("Smoothing features...", size);
    for (SimpleFeature line : linesList) {
        Geometry geometry = (Geometry) line.getDefaultGeometry();
        List<LineString> lsList = smoothGeometries(geometry);
        if (lsList.size() != 0) {
            LineString[] lsArray = (LineString[]) lsList.toArray(new LineString[lsList.size()]);
            MultiLineString multiLineString = gF.createMultiLineString(lsArray);
            SimpleFeature newFeature = fGS.substituteGeometry(line, multiLineString);
            ((DefaultFeatureCollection) outVector).add(newFeature);
        }
        pm.worked(1);
    }
    pm.done();
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) MultiLineString(org.locationtech.jts.geom.MultiLineString) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) FeatureGeometrySubstitutor(org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Execute(oms3.annotations.Execute)

Example 2 with FeatureGeometrySubstitutor

use of org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor in project hortonmachine by TheHortonMachine.

the class OmsPolygonSmootherJaitools method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outVector == null, doReset)) {
        return;
    }
    outVector = new DefaultFeatureCollection();
    pm.message("Collecting geometries...");
    List<SimpleFeature> polygonsList = FeatureUtilities.featureCollectionToList(inVector);
    int size = inVector.size();
    FeatureGeometrySubstitutor fGS = new FeatureGeometrySubstitutor(inVector.getSchema());
    pm.beginTask("Smoothing features...", size);
    PolygonSmoother smoother = new PolygonSmoother(gF);
    for (SimpleFeature polygonsFeature : polygonsList) {
        Geometry geometry = (Geometry) polygonsFeature.getDefaultGeometry();
        int numGeometries = geometry.getNumGeometries();
        List<Polygon> smoothedList = new ArrayList<Polygon>();
        for (int i = 0; i < numGeometries; i++) {
            Geometry geometryN = geometry.getGeometryN(i);
            if (geometryN instanceof Polygon) {
                Polygon polygon = (Polygon) geometryN;
                Polygon smoothed = smoother.smooth(polygon, pAlpha);
                smoothedList.add(smoothed);
            }
        }
        if (smoothedList.size() != 0) {
            Polygon[] lsArray = (Polygon[]) smoothedList.toArray(new Polygon[smoothedList.size()]);
            MultiPolygon multiPolygonString = gF.createMultiPolygon(lsArray);
            SimpleFeature newFeature = fGS.substituteGeometry(polygonsFeature, multiPolygonString);
            ((DefaultFeatureCollection) outVector).add(newFeature);
        }
        pm.worked(1);
    }
    pm.done();
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) PolygonSmoother(org.jaitools.jts.PolygonSmoother) ArrayList(java.util.ArrayList) FeatureGeometrySubstitutor(org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Execute(oms3.annotations.Execute)

Example 3 with FeatureGeometrySubstitutor

use of org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor in project hortonmachine by TheHortonMachine.

the class OmsBuffer method process.

@Execute
public void process() throws Exception {
    checkNull(inMap);
    int joinStyle;
    if (pJoinstyle.equals(JOIN_MITRE)) {
        joinStyle = BufferParameters.JOIN_MITRE;
    } else if (pJoinstyle.equals(JOIN_BEVEL)) {
        joinStyle = BufferParameters.JOIN_BEVEL;
    } else {
        joinStyle = BufferParameters.JOIN_ROUND;
    }
    int endCapStyle;
    if (pCapstyle.equals(CAP_FLAT)) {
        endCapStyle = BufferParameters.CAP_FLAT;
    } else if (pCapstyle.equals(CAP_SQUARE)) {
        endCapStyle = BufferParameters.CAP_SQUARE;
    } else {
        endCapStyle = BufferParameters.CAP_ROUND;
    }
    FeatureGeometrySubstitutor fgs = new FeatureGeometrySubstitutor(inMap.getSchema(), MultiPolygon.class);
    DefaultFeatureCollection outMaptmp = new DefaultFeatureCollection("new", fgs.getNewFeatureType());
    GeometryFactory gf = GeometryUtilities.gf();
    List<SimpleFeature> featuresList = FeatureUtilities.featureCollectionToList(inMap);
    pm.beginTask("Buffering geometries...", featuresList.size());
    for (SimpleFeature feature : featuresList) {
        Geometry geometry = (Geometry) feature.getDefaultGeometry();
        double buf = pBuffer;
        if (pBufferField != null) {
            Object bFieldObj = feature.getAttribute(pBufferField);
            if (bFieldObj instanceof Number) {
                buf = ((Number) bFieldObj).doubleValue();
            }
        }
        BufferParameters bP = new BufferParameters(quadrantSegments, endCapStyle, joinStyle, mitreLimit);
        Geometry bufferedGeom = BufferOp.bufferOp(geometry, buf, bP);
        List<Polygon> polygons = new ArrayList<Polygon>(bufferedGeom.getNumGeometries());
        for (int i = 0; i < bufferedGeom.getNumGeometries(); i++) {
            Geometry geometryN = bufferedGeom.getGeometryN(i);
            if (geometryN instanceof Polygon) {
                polygons.add((Polygon) geometryN);
            } else {
                pm.errorMessage("Ignored non polygonal geometry in: " + geometryN.toText());
            }
        }
        MultiPolygon multiPolygon = gf.createMultiPolygon(polygons.toArray(GeometryUtilities.TYPE_POLYGON));
        SimpleFeature newFeature = fgs.substituteGeometry(feature, multiPolygon);
        outMaptmp.add(newFeature);
        pm.worked(1);
    }
    pm.done();
    outMap = outMaptmp;
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) BufferParameters(org.locationtech.jts.operation.buffer.BufferParameters) ArrayList(java.util.ArrayList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(org.locationtech.jts.geom.Geometry) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) FeatureGeometrySubstitutor(org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) Execute(oms3.annotations.Execute)

Example 4 with FeatureGeometrySubstitutor

use of org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor in project hortonmachine by TheHortonMachine.

the class OmsVectorIntersector method process.

// END VARS DOC
@Execute
public void process() throws Exception {
    checkNull(inMap1, inMap2);
    outMap = new DefaultFeatureCollection();
    if (!doKeepFirstAttributes) {
        SimpleFeatureCollection inMapTmp = inMap1;
        inMap1 = inMap2;
        inMap2 = inMapTmp;
    }
    List<Geometry> geometries = FeatureUtilities.featureCollectionToGeometriesList(inMap2, false, null);
    GeometryCollection geometryCollection = new GeometryCollection(geometries.toArray(new Geometry[geometries.size()]), gf);
    Geometry intersectionGeometry = geometryCollection.buffer(0);
    PreparedGeometry preparedIntersectionGeometry = PreparedGeometryFactory.prepare(intersectionGeometry);
    List<SimpleFeature> mainFeatures = FeatureUtilities.featureCollectionToList(inMap1);
    if (mainFeatures.size() == 0) {
        throw new ModelsIllegalargumentException("No features found in the layer.", this);
    }
    EGeometryType geometryType = EGeometryType.forGeometry((Geometry) mainFeatures.get(0).getDefaultGeometry());
    Class<?> multiClazz = geometryType.getMultiClazz();
    EGeometryType newGeometryType = EGeometryType.forClass(multiClazz);
    FeatureGeometrySubstitutor sub = new FeatureGeometrySubstitutor(inMap1.getSchema(), multiClazz);
    pm.beginTask("Performing intersection...", mainFeatures.size());
    for (SimpleFeature feature : mainFeatures) {
        Geometry geometry = (Geometry) feature.getDefaultGeometry();
        if (preparedIntersectionGeometry.intersects(geometry)) {
            Geometry intersection = geometry.intersection(intersectionGeometry);
            EGeometryType intersectionGeometryType = EGeometryType.forGeometry(intersection);
            if (intersectionGeometryType.isCompatibleWith(newGeometryType)) {
                SimpleFeature newFeature = sub.substituteGeometry(feature, intersection);
                ((DefaultFeatureCollection) outMap).add(newFeature);
            } else {
                pm.errorMessage("Could not add intersection result geometry to layer due to incompatibility: " + intersection);
            }
        }
        pm.worked(1);
    }
    pm.done();
}
Also used : PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) FeatureGeometrySubstitutor(org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) EGeometryType(org.hortonmachine.gears.utils.geometry.EGeometryType) Execute(oms3.annotations.Execute)

Example 5 with FeatureGeometrySubstitutor

use of org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor in project hortonmachine by TheHortonMachine.

the class OmsVectorSimplifier method process.

// PARAM DESC END
@Execute
public void process() throws Exception {
    if (!concatOr(outVector == null, doReset)) {
        return;
    }
    FeatureIterator<SimpleFeature> inFeatureIterator = inVector.features();
    outVector = new DefaultFeatureCollection();
    FeatureGeometrySubstitutor fGS = new FeatureGeometrySubstitutor(inVector.getSchema());
    GeometryPrecisionReducer geometryReducer = null;
    int size = inVector.size();
    pm.beginTask("Simplifing features...", size);
    while (inFeatureIterator.hasNext()) {
        SimpleFeature feature = inFeatureIterator.next();
        Geometry geometry = (Geometry) feature.getDefaultGeometry();
        List<Geometry> geomList = new ArrayList<Geometry>();
        int numGeometries = geometry.getNumGeometries();
        for (int i = 0; i < numGeometries; i++) {
            Geometry geometryN = geometry.getGeometryN(i);
            switch(pType) {
                case TOPOLOGYPRESERVINGSIMPLIFIER:
                    TopologyPreservingSimplifier tpSimplifier = new TopologyPreservingSimplifier(geometryN);
                    tpSimplifier.setDistanceTolerance(pTolerance);
                    Geometry tpsGeometry = tpSimplifier.getResultGeometry();
                    geomList.add(tpsGeometry);
                    break;
                case DOUGLAS_PEUCKER:
                    DouglasPeuckerSimplifier dpSimplifier = new DouglasPeuckerSimplifier(geometryN);
                    dpSimplifier.setDistanceTolerance(pTolerance);
                    Geometry dpsGeometry = dpSimplifier.getResultGeometry();
                    geomList.add(dpsGeometry);
                    break;
                case PRECISION_REDUCER:
                    if (geometryReducer == null) {
                        if (pScale == null) {
                            throw new ModelsIllegalargumentException("To reduce the precision the scale parameter needs to be set.", this);
                        }
                        geometryReducer = new GeometryPrecisionReducer(new PrecisionModel(pScale));
                    }
                    geomList.add(geometryReducer.reduce(geometryN));
                default:
            }
        }
        Geometry newGeometry = null;
        if (geomList.size() == 1) {
            newGeometry = geomList.get(0);
        } else {
            Geometry[] geomArray = (Geometry[]) geomList.toArray(new Geometry[geomList.size()]);
            newGeometry = new GeometryCollection(geomArray, gf);
        }
        SimpleFeature newFeature = fGS.substituteGeometry(feature, newGeometry);
        ((DefaultFeatureCollection) outVector).add(newFeature);
        pm.worked(1);
    }
    pm.done();
    inFeatureIterator.close();
}
Also used : ArrayList(java.util.ArrayList) DouglasPeuckerSimplifier(org.locationtech.jts.simplify.DouglasPeuckerSimplifier) PrecisionModel(org.locationtech.jts.geom.PrecisionModel) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(org.locationtech.jts.geom.Geometry) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) GeometryPrecisionReducer(org.locationtech.jts.precision.GeometryPrecisionReducer) TopologyPreservingSimplifier(org.locationtech.jts.simplify.TopologyPreservingSimplifier) FeatureGeometrySubstitutor(org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) Execute(oms3.annotations.Execute)

Aggregations

Execute (oms3.annotations.Execute)7 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)7 FeatureGeometrySubstitutor (org.hortonmachine.gears.utils.features.FeatureGeometrySubstitutor)7 Geometry (org.locationtech.jts.geom.Geometry)7 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 ArrayList (java.util.ArrayList)4 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)2 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)2 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)2 Polygon (org.locationtech.jts.geom.Polygon)2 AffineTransform (java.awt.geom.AffineTransform)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1 AffineTransform2D (org.geotools.referencing.operation.transform.AffineTransform2D)1 EGeometryType (org.hortonmachine.gears.utils.geometry.EGeometryType)1 LineSmoother (org.jaitools.jts.LineSmoother)1 PolygonSmoother (org.jaitools.jts.PolygonSmoother)1 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)1