Java Code Examples for org.locationtech.jts.geom.Geometry#union()

The following examples show how to use org.locationtech.jts.geom.Geometry#union() . 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: Shape.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get union shape
 * @param b Other shape
 * @return Union shape
 */
public Shape union(Shape b){
    Geometry g1 = this.toGeometry();
    Geometry g2 = b.toGeometry();
    Geometry g3 = g1.union(g2);
    return geometry2Shape(g3);
}
 
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: RuleReaderServiceImpl.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
private Geometry union(Geometry g1, Geometry g2) {
    if(g1!=null) {
        if(g2==null)
            return g1;
        else
            return g1.union(g2);
    } else {
        return g2;
    }
}
 
Example 5
Source File: RasterUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
private static Geometry combineIntoOneGeometry(final Collection<Geometry> geometries) {
  final GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);

  // note the following geometry collection may be invalid (say with
  // overlapping polygons)
  final Geometry geometryCollection = factory.buildGeometry(geometries);
  // try {
  return geometryCollection.union();
  // }
  // catch (Exception e) {
  // LOGGER.warn("Error creating a union of this geometry collection", e);
  // return geometryCollection;
  // }
}
 
Example 6
Source File: ExtractGeometryFilterVisitor.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public Object visit(final BBOX filter, final Object data) {
  if (attributeOfInterest.equals(filter.getExpression1().toString())) {
    final Geometry bbox = bbox(data);
    final BoundingBox referencedBBox = filter.getBounds();
    Geometry bounds =
        new GeometryFactory().toGeometry(
            new Envelope(
                referencedBBox.getMinX(),
                referencedBBox.getMaxX(),
                referencedBBox.getMinY(),
                referencedBBox.getMaxY()));

    if ((crs != null)
        && (referencedBBox.getCoordinateReferenceSystem() != null)
        && !crs.equals(referencedBBox.getCoordinateReferenceSystem())) {
      try {
        bounds =
            JTS.transform(
                bounds,
                CRS.findMathTransform(referencedBBox.getCoordinateReferenceSystem(), crs, true));
      } catch (MismatchedDimensionException | TransformException | FactoryException e) {
        LOGGER.error("Unable to transforma bbox", e);
      }
    }
    if (bbox != null) {
      return bbox.union(bounds);
    } else {
      return new ExtractGeometryFilterVisitorResult(bounds, CompareOperation.INTERSECTS);
    }
  } else {
    return new ExtractGeometryFilterVisitorResult(infinity(), null);
  }
}
 
Example 7
Source File: NoDataMetadataFactory.java    From geowave with Apache License 2.0 4 votes vote down vote up
private static NoDataMetadata createMetadata(
    final NoDataSummary noDataSummary,
    final Geometry[] shapes,
    final int width,
    final int height) {
  if (noDataSummary.indices.size() > MAX_LIST_NO_DATA) {
    Geometry finalShape;
    if ((shapes == null) || (shapes.length == 0)) {
      finalShape = null;
    } else {
      finalShape = shapes[0];
      if ((shapes.length > 1) && (finalShape != null)) {
        for (int i = 1; i < shapes.length; i++) {
          if (shapes[i] == null) {
            finalShape = null;
            break;
          } else {
            finalShape = finalShape.union(shapes[i]);
          }
        }
      }
    }
    if ((finalShape != null)
        && finalShape.covers(
            new GeometryFactory().toGeometry(new Envelope(0, width, 0, height)))) {
      // if the coverage of this geometric union ever gets to the
      // point that it fully covers the raster, stop storing it and
      // just set the geometry to null
      finalShape = null;
    }
    return new NoDataByFilter(finalShape, noDataSummary.usedNoDataValues);
  } else if (!noDataSummary.indices.isEmpty()) {
    // just go through every raster sample and determine whether it
    // qualifies as null data
    return new NoDataBySampleIndex(noDataSummary.indices);
  } else {
    // the "no data" samples in the dataset must be 0, so just return
    // null for the metadata
    return null;
  }
}