Search in sources :

Example 36 with Polygon

use of org.locationtech.jts.geom.Polygon in project presto by prestodb.

the class JtsGeometrySerde method writePolygon.

private static void writePolygon(Geometry geometry, SliceOutput output, boolean multitype) {
    int numGeometries = geometry.getNumGeometries();
    int numParts = 0;
    int numPoints = geometry.getNumPoints();
    for (int i = 0; i < numGeometries; i++) {
        Polygon polygon = (Polygon) geometry.getGeometryN(i);
        if (polygon.getNumPoints() > 0) {
            numParts += polygon.getNumInteriorRing() + 1;
        }
    }
    if (multitype) {
        output.writeByte(GeometrySerializationType.MULTI_POLYGON.code());
    } else {
        output.writeByte(GeometrySerializationType.POLYGON.code());
    }
    output.writeInt(EsriShapeType.POLYGON.code);
    writeEnvelope(geometry, output);
    output.writeInt(numParts);
    output.writeInt(numPoints);
    if (numParts == 0) {
        return;
    }
    int[] partIndexes = new int[numParts];
    boolean[] shellPart = new boolean[numParts];
    int currentPart = 0;
    int currentPoint = 0;
    for (int i = 0; i < numGeometries; i++) {
        Polygon polygon = (Polygon) geometry.getGeometryN(i);
        partIndexes[currentPart] = currentPoint;
        shellPart[currentPart] = true;
        currentPart++;
        currentPoint += polygon.getExteriorRing().getNumPoints();
        int holesCount = polygon.getNumInteriorRing();
        for (int holeIndex = 0; holeIndex < holesCount; holeIndex++) {
            partIndexes[currentPart] = currentPoint;
            shellPart[currentPart] = false;
            currentPart++;
            currentPoint += polygon.getInteriorRingN(holeIndex).getNumPoints();
        }
    }
    for (int partIndex : partIndexes) {
        output.writeInt(partIndex);
    }
    Coordinate[] coordinates = geometry.getCoordinates();
    canonicalizePolygonCoordinates(coordinates, partIndexes, shellPart);
    writeCoordinates(coordinates, output);
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) Polygon(org.locationtech.jts.geom.Polygon) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 37 with Polygon

use of org.locationtech.jts.geom.Polygon in project arctic-sea by 52North.

the class GmlDecoderv321 method parseCompositeSurfaceType.

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;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) AbstractGeometry(org.n52.shetland.ogc.gml.AbstractGeometry) AbstractSurfaceType(net.opengis.gml.x32.AbstractSurfaceType) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) SurfacePropertyType(net.opengis.gml.x32.SurfacePropertyType) ArrayList(java.util.ArrayList) PolygonType(net.opengis.gml.x32.PolygonType) DecodingException(org.n52.svalbard.decode.exception.DecodingException) Polygon(org.locationtech.jts.geom.Polygon)

Example 38 with Polygon

use of org.locationtech.jts.geom.Polygon in project dhis2-core by dhis2.

the class GeoUtils method checkPointWithMultiPolygon.

/**
 * Check if the point coordinate falls within the polygon/MultiPolygon Shape
 *
 * @param longitude the longitude.
 * @param latitude the latitude.
 * @param geometry the GeoJSON coordinates of the MultiPolygon
 */
public static boolean checkPointWithMultiPolygon(double longitude, double latitude, Geometry geometry) {
    try {
        boolean contains = false;
        Point point = getGeoJsonPoint(longitude, latitude);
        FeatureType featureType = FeatureType.getTypeFromName(geometry.getGeometryType());
        if (point != null && point.isValid()) {
            if (featureType == FeatureType.POLYGON) {
                Polygon polygon = (Polygon) geometry;
                contains = polygon.contains(point);
            } else if (featureType == FeatureType.MULTI_POLYGON) {
                MultiPolygon multiPolygon = (MultiPolygon) geometry;
                contains = multiPolygon.contains(point);
            }
        }
        return contains;
    } catch (Exception ex) {
        return false;
    }
}
Also used : FeatureType(org.hisp.dhis.organisationunit.FeatureType) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) IOException(java.io.IOException)

Example 39 with Polygon

use of org.locationtech.jts.geom.Polygon in project yyl_example by Relucent.

the class JtsGeometryExample2 method withinGeo.

/**
 * 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中
 * @param x 坐标
 * @param y 坐标
 * @param 表达式
 * @return 是否包含
 */
public static boolean withinGeo(double x, double y, String geometry) throws ParseException {
    Coordinate coord = new Coordinate(x, y);
    Point point = GEOMETRY_FACTORY.createPoint(coord);
    WKTReader reader = new WKTReader(GEOMETRY_FACTORY);
    Polygon polygon = (Polygon) reader.read(geometry);
    return point.within(polygon);
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) Point(org.locationtech.jts.geom.Point) WKTReader(org.locationtech.jts.io.WKTReader) Polygon(org.locationtech.jts.geom.Polygon)

Example 40 with Polygon

use of org.locationtech.jts.geom.Polygon in project yyl_example by Relucent.

the class JtsGeometryExample1 method createGeoCollect.

/**
 * create GeometryCollection contain point or multiPoint or line or multiLine or polygon or multiPolygon
 * @return 图形集
 * @throws ParseException
 */
public static GeometryCollection createGeoCollect() throws ParseException {
    LineString line = createLine();
    Polygon poly = createPolygonByWKT();
    Geometry g1 = GEOMETRY_FACTORY.createGeometry(line);
    Geometry g2 = GEOMETRY_FACTORY.createGeometry(poly);
    Geometry[] garray = new Geometry[] { g1, g2 };
    GeometryCollection gc = GEOMETRY_FACTORY.createGeometryCollection(garray);
    return gc;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon)

Aggregations

Polygon (org.locationtech.jts.geom.Polygon)56 LineString (org.locationtech.jts.geom.LineString)23 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)22 Point (org.locationtech.jts.geom.Point)22 Geometry (org.locationtech.jts.geom.Geometry)20 Coordinate (org.locationtech.jts.geom.Coordinate)16 MultiPoint (org.locationtech.jts.geom.MultiPoint)15 Test (org.junit.Test)13 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)11 LinearRing (org.locationtech.jts.geom.LinearRing)10 CustomCoordinateSequence (org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence)8 MultiLineString (org.locationtech.jts.geom.MultiLineString)8 ArrayList (java.util.ArrayList)7 Test (org.junit.jupiter.api.Test)6 DimensionInfo (org.apache.jena.geosparql.implementation.DimensionInfo)5 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)5 WKTReader (org.locationtech.jts.io.WKTReader)5 AreaIndex (com.graphhopper.routing.util.AreaIndex)4 CustomArea (com.graphhopper.routing.util.CustomArea)3 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)3