org.locationtech.jts.operation.polygonize.Polygonizer Java Examples

The following examples show how to use org.locationtech.jts.operation.polygonize.Polygonizer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FeatureUtilities.java    From geopaparazzi with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tries to split an invalid polygon in its {@link GeometryCollection}.
 * <p/>
 * <p>Based on JTSBuilder code.
 *
 * @param invalidPolygon the invalid polygon.
 * @return the geometries.
 */
@SuppressWarnings("rawtypes")
public static Geometry invalidPolygonSplit(Geometry invalidPolygon) {
    PrecisionModel pm = new PrecisionModel(10000000);
    GeometryFactory geomFact = invalidPolygon.getFactory();
    List lines = LinearComponentExtracter.getLines(invalidPolygon);
    List nodedLinework = new GeometryNoder(pm).node(lines);
    // union the noded linework to remove duplicates
    Geometry nodedDedupedLinework = geomFact.buildGeometry(nodedLinework).union();
    // polygonize the result
    Polygonizer polygonizer = new Polygonizer();
    polygonizer.add(nodedDedupedLinework);
    Collection polys = polygonizer.getPolygons();
    // convert to collection for return
    Polygon[] polyArray = GeometryFactory.toPolygonArray(polys);
    return geomFact.createGeometryCollection(polyArray);
}
 
Example #2
Source File: Shape.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Split shape
 * @param line Split line
 * @return Splitted shapes
 */
public List<Shape> split(Shape line){
    Geometry g1 = this.toGeometry();
    Geometry g2 = line.toGeometry();
    if (this.getShapeType().isPolygon()){
        Polygonizer polygonizer = new Polygonizer();
        Geometry polygons = g1.getBoundary().union(g2);
        polygonizer.add(polygons);
        List<Geometry> polys = (List)polygonizer.getPolygons();   
        List<Shape> polyShapes = new ArrayList<>();
        for (int i = 0; i < polys.size(); i++){
            org.locationtech.jts.geom.Polygon poly = (org.locationtech.jts.geom.Polygon)polys.get(i);
            if (poly.getInteriorPoint().within(g1))
                polyShapes.add(new PolygonShape(poly));
        }
        return polyShapes;
    } else if (this.getShapeType().isLine()){
        Geometry ugeo = g1.union(g2);            
        List<Shape> lineShapes = new ArrayList<>();
        for (int i = 0; i < ugeo.getNumGeometries(); i++){
            Geometry geo = ugeo.getGeometryN(i);
            if (geo.buffer(0.001).within(g1.buffer(0.0011)))
                lineShapes.add(new PolylineShape(geo));
        }
        return lineShapes;
    }
    
    return null;
}
 
Example #3
Source File: GeometryUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@link Polygon} by {@link LineString} split.
 * 
 * <p>From JTS ml: http://lists.refractions.net/pipermail/jts-devel/2008-September/002666.html</p> 
 * 
 * @param polygon the input polygon.
 * @param line the input line to use to split.
 * @return the list of split polygons.
 */
public static List<Polygon> splitPolygon( Polygon polygon, LineString line ) {
    /*
     * Use MCIndexNoder to node the polygon and linestring together, 
     * Polygonizer to polygonize the noded edges, and then PointLocater 
     * to determine which of the resultant polygons correspond to 
     * the input polygon. 
     */
    IntersectionAdder _intersector = new IntersectionAdder(new RobustLineIntersector());
    MCIndexNoder mci = new MCIndexNoder();
    mci.setSegmentIntersector(_intersector);
    NodedSegmentString pSeg = new NodedSegmentString(polygon.getCoordinates(), null);
    NodedSegmentString lSeg = new NodedSegmentString(line.getCoordinates(), null);
    List<NodedSegmentString> nodesSegmentStringList = new ArrayList<NodedSegmentString>();
    nodesSegmentStringList.add(pSeg);
    nodesSegmentStringList.add(lSeg);
    mci.computeNodes(nodesSegmentStringList);
    Polygonizer polygonizer = new Polygonizer();
    List<LineString> lsList = new ArrayList<LineString>();
    for( Object o : mci.getMonotoneChains() ) {
        MonotoneChain mtc = (MonotoneChain) o;
        LineString l = gf().createLineString(mtc.getCoordinates());
        lsList.add(l);
    }
    Geometry nodedLineStrings = lsList.get(0);
    for( int i = 1; i < lsList.size(); i++ ) {
        nodedLineStrings = nodedLineStrings.union(lsList.get(i));
    }
    polygonizer.add(nodedLineStrings);
    @SuppressWarnings("unchecked")
    Collection<Polygon> polygons = polygonizer.getPolygons();
    List<Polygon> newPolygons = new ArrayList<Polygon>();
    PointLocator pl = new PointLocator();
    for( Polygon p : polygons ) {
        if (pl.locate(p.getInteriorPoint().getCoordinate(), p) == Location.INTERIOR) {
            newPolygons.add(p);
        }
    }
    return newPolygons;
}
 
Example #4
Source File: OmsLinesPolygonizer.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inMap);

    outMap = new DefaultFeatureCollection();

    EGeometryType geometryType = EGeometryType.forGeometryDescriptor(inMap.getSchema().getGeometryDescriptor());
    switch( geometryType ) {
    case LINESTRING:
    case MULTILINESTRING:
        break;
    default:
        throw new ModelsIllegalargumentException("The module only works with line layers.", this, pm);
    }

    List<Geometry> linesList = FeatureUtilities.featureCollectionToGeometriesList(inMap, true, null);

    // Polygonization
    final Polygonizer polygonizer = new Polygonizer();
    polygonizer.add(linesList);
    @SuppressWarnings("unchecked")
    final Collection<Polygon> polygonizedLines = polygonizer.getPolygons();

    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
    b.setName("polygonized");
    b.setCRS(inMap.getSchema().getCoordinateReferenceSystem());
    b.add("the_geom", Polygon.class);
    b.add(fNewId, String.class);
    SimpleFeatureType type = b.buildFeatureType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);

    List<Geometry> pointGeometries = new ArrayList<Geometry>();
    if (inPoints != null) {
        fId = FeatureUtilities.findAttributeName(inPoints.getSchema(), fId);
        pointGeometries = FeatureUtilities.featureCollectionToGeometriesList(inPoints, false, fId);
    }

    pm.beginTask("Generating polygon features...", polygonizedLines.size());
    int index = 0;
    for( Polygon polygon : polygonizedLines ) {
        String attribute = String.valueOf(index++);
        if (inPoints != null) {
            attribute = "-";
            for( Geometry point : pointGeometries ) {
                if (polygon.contains(point)) {
                    attribute = point.getUserData().toString();
                    break;
                }
            }
        }

        Object[] values = new Object[]{polygon, attribute};
        builder.addAll(values);
        SimpleFeature feature = builder.buildFeature(null);
        ((DefaultFeatureCollection) outMap).add(feature);

        pm.worked(1);
    }
    pm.done();
}