Java Code Examples for org.locationtech.jts.geom.GeometryFactory#createMultiPolygon()

The following examples show how to use org.locationtech.jts.geom.GeometryFactory#createMultiPolygon() . 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: PolygonShape.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * To geometry method
 *
 * @param factory GeometryFactory
 * @return Geometry
 */
@Override
public Geometry toGeometry(GeometryFactory factory) {
    if (this._polygons.size() == 1) {
        return this._polygons.get(0).toGeometry(factory);
    } else {
        org.locationtech.jts.geom.Polygon[] polygons = new org.locationtech.jts.geom.Polygon[this._polygons.size()];
        for (int j = 0; j < polygons.length; j++) {
            polygons[j] = (org.locationtech.jts.geom.Polygon) this._polygons.get(j).toGeometry(factory);
        }
        MultiPolygon mls = factory.createMultiPolygon(polygons);
        return mls;
    }
}
 
Example 2
Source File: GeoJSONDecoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected MultiPolygon decodeMultiPolygon(JsonNode node, GeometryFactory fac)
        throws GeoJSONDecodingException {
    JsonNode coordinates = requireCoordinates(node);
    Polygon[] polygons = new Polygon[coordinates.size()];
    for (int i = 0; i < coordinates.size(); ++i) {
        polygons[i] = decodePolygonCoordinates(coordinates.get(i), fac);
    }
    return fac.createMultiPolygon(polygons);
}
 
Example 3
Source File: GmlDecoderv321.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private Geometry parseCompositeSurfaceType(CompositeSurfaceType xbCompositeSurface) throws DecodingException {
    SurfacePropertyType[] xbCurfaceProperties = xbCompositeSurface.getSurfaceMemberArray();
    int srid = -1;
    ArrayList<Polygon> polygons = new ArrayList<>(xbCurfaceProperties.length);
    if (xbCompositeSurface.getSrsName() != null) {
        srid = CRSHelper.parseSrsName(xbCompositeSurface.getSrsName());
    }
    for (SurfacePropertyType xbSurfaceProperty : xbCurfaceProperties) {
        AbstractSurfaceType xbAbstractSurface = xbSurfaceProperty.getAbstractSurface();
        if (srid == -1 && xbAbstractSurface.getSrsName() != null) {
            srid = CRSHelper.parseSrsName(xbAbstractSurface.getSrsName());
        }
        if (xbAbstractSurface instanceof PolygonType) {
            polygons.add((Polygon) parsePolygonType((PolygonType) xbAbstractSurface));
        } else {
            throw new DecodingException("The FeatureType %s is not supportted! Only PolygonType",
                                        xbAbstractSurface);
        }
    }
    if (polygons.isEmpty()) {
        throw new DecodingException("The FeatureType: %s does not contain any member!", xbCompositeSurface);
    }
    srid = setDefaultForUnsetSrid(srid);
    GeometryFactory factory = new GeometryFactory();
    Geometry geom = factory.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()]));
    geom.setSRID(srid);
    return geom;
}
 
Example 4
Source File: MultiPolygonUtils.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Simplifies a MultiPolygon.
 * <BR/><BR/>
 * Simplification is performed by first removing collinear points, then
 * by applying DouglasPeucker simplification.
 * <BR/>Order <B>is</B> important, since it's more likely to have collinear
 * points before applying any other simplification.
 */
public static MultiPolygon simplifyMultiPolygon(final MultiPolygon mp)
{

    final Polygon[] simpPolys = new Polygon[mp.getNumGeometries()];

    for (int i = 0; i < mp.getNumGeometries(); i++)
    {
        Polygon p = (Polygon) mp.getGeometryN(i);
        Polygon s1 = p; //Utils.removeCollinearVertices(p);
        TopologyPreservingSimplifier tps = new TopologyPreservingSimplifier(s1);
        Polygon s2 = (Polygon) tps.getResultGeometry();
        simpPolys[i] = s2;

        if (LOGGER.isInfoEnabled())
        {
            LOGGER.info("RCV: simplified poly " + getPoints(p) +
                " --> " + getPoints(s1) +
                " --> " + getPoints(s2));
        }
    }

    // reuse existing factory
    final GeometryFactory gf = mp.getFactory();

    return gf.createMultiPolygon(simpPolys);
}
 
Example 5
Source File: GeometryUtilsTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstraintGeneration() {

  final GeometryFactory gf = new GeometryFactory();
  final Geometry multiPolygon =
      gf.createMultiPolygon(
          new Polygon[] {
              gf.createPolygon(
                  new Coordinate[] {
                      new Coordinate(20.0, 30),
                      new Coordinate(20, 40),
                      new Coordinate(10, 40),
                      new Coordinate(10, 30),
                      new Coordinate(20, 30)}),
              gf.createPolygon(
                  new Coordinate[] {
                      new Coordinate(-9, -2),
                      new Coordinate(-9, -1),
                      new Coordinate(-8, -1),
                      new Coordinate(-8, -2),
                      new Coordinate(-9, -2)})});
  final Constraints constraints = GeometryUtils.basicConstraintsFromGeometry(multiPolygon);
  final List<MultiDimensionalNumericData> results =
      constraints.getIndexConstraints(new IndexImpl(new ExampleNumericIndexStrategy(), null));
  assertEquals(2, results.size());
  assertTrue(Arrays.equals(new double[] {10, 30}, results.get(0).getMinValuesPerDimension()));
  assertTrue(Arrays.equals(new double[] {20, 40}, results.get(0).getMaxValuesPerDimension()));
  assertTrue(Arrays.equals(new double[] {-9, -2}, results.get(1).getMinValuesPerDimension()));
  assertTrue(Arrays.equals(new double[] {-8, -1}, results.get(1).getMaxValuesPerDimension()));
}
 
Example 6
Source File: WithinQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
private Query getQuery(Function inner, LuceneQueryBuilder.Context context) {
    RefAndLiteral innerPair = RefAndLiteral.of(inner);
    if (innerPair == null) {
        return null;
    }
    if (innerPair.reference().valueType().equals(DataTypes.GEO_SHAPE)) {
        // we have within('POINT(0 0)', shape_column)
        return LuceneQueryBuilder.genericFunctionFilter(inner, context);
    }
    GeoPointFieldMapper.GeoPointFieldType geoPointFieldType = getGeoPointFieldType(
        innerPair.reference().column().fqn(),
        context.mapperService);

    Map<String, Object> geoJSON = DataTypes.GEO_SHAPE.value(innerPair.literal().value());
    Geometry geometry;
    Shape shape = GeoJSONUtils.map2Shape(geoJSON);
    if (shape instanceof ShapeCollection) {
        int i = 0;
        ShapeCollection<Shape> collection = (ShapeCollection) shape;
        org.locationtech.jts.geom.Polygon[] polygons = new org.locationtech.jts.geom.Polygon[collection.size()];
        for (Shape s : collection.getShapes()) {
            Geometry subGeometry = JtsSpatialContext.GEO.getShapeFactory().getGeometryFrom(s);
            if (subGeometry instanceof org.locationtech.jts.geom.Polygon) {
                polygons[i++] = (org.locationtech.jts.geom.Polygon) subGeometry;
            } else {
                throw new InvalidShapeException("Shape collection must contain only Polygon shapes.");
            }
        }
        GeometryFactory geometryFactory = JtsSpatialContext.GEO.getShapeFactory().getGeometryFactory();
        geometry = geometryFactory.createMultiPolygon(polygons);
    } else {
        geometry = JtsSpatialContext.GEO.getShapeFactory().getGeometryFrom(shape);
    }

    return getPolygonQuery(geometry, geoPointFieldType);
}
 
Example 7
Source File: GeoFunctionsDescriptor.java    From datawave with Apache License 2.0 4 votes vote down vote up
private MultiPolygon createMultiPolygon(Polygon poly1, Polygon poly2) {
    GeometryFactory geomFactory = new GeometryFactory();
    return geomFactory.createMultiPolygon(new Polygon[] {poly1, poly2});
}
 
Example 8
Source File: OmsBuffer.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@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;

}
 
Example 9
Source File: PolygonBuilder.java    From crate with Apache License 2.0 3 votes vote down vote up
/**
 * Create a Multipolygon from a set of coordinates. Each primary array contains a polygon which
 * in turn contains an array of linestrings. These line Strings are represented as an array of
 * coordinates. The first linestring will be the shell of the polygon the others define holes
 * within the polygon.
 *
 * @param factory {@link GeometryFactory} to use
 * @param polygons definition of polygons
 * @return a new Multipolygon
 */
protected static MultiPolygon multipolygon(GeometryFactory factory, Coordinate[][][] polygons) {
    Polygon[] polygonSet = new Polygon[polygons.length];
    for (int i = 0; i < polygonSet.length; i++) {
        polygonSet[i] = polygon(factory, polygons[i]);
    }
    return factory.createMultiPolygon(polygonSet);
}