Search in sources :

Example 11 with Polygon

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

the class JtsGeometryExample1 method main.

public static void main(String[] args) throws ParseException {
    Polygon p = createCircle(0, 1, 2);
    // 圆上所有的坐标(32个)
    Coordinate[] coords = p.getCoordinates();
    for (Coordinate coord : coords) {
        System.out.println(coord.x + "," + coord.y);
    }
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon)

Example 12 with Polygon

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

the class JtsGeometryExample1 method createCircle.

/**
 * create a Circle 创建一个圆,圆心(x,y) 半径RADIUS
 * @param x 圆心X坐标
 * @param y 圆心X坐标
 * @param RADIUS 半径
 * @return 圆
 */
public static Polygon createCircle(double x, double y, final double RADIUS) {
    // 圆上面的点个数
    final int SIDES = 32;
    Coordinate[] coords = new Coordinate[SIDES + 1];
    for (int i = 0; i < SIDES; i++) {
        double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
        double dx = Math.cos(angle) * RADIUS;
        double dy = Math.sin(angle) * RADIUS;
        coords[i] = new Coordinate((double) x + dx, (double) y + dy);
    }
    coords[SIDES] = coords[0];
    LinearRing ring = GEOMETRY_FACTORY.createLinearRing(coords);
    Polygon polygon = GEOMETRY_FACTORY.createPolygon(ring, null);
    return polygon;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 13 with Polygon

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

the class JtsGeometryExample1 method createPolygonByWKT.

/**
 * create a polygon(多边形) by WKT
 * @return 多边形
 * @throws ParseException
 */
public static Polygon createPolygonByWKT() throws ParseException {
    WKTReader reader = new WKTReader(GEOMETRY_FACTORY);
    Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
    return polygon;
}
Also used : WKTReader(org.locationtech.jts.io.WKTReader) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon)

Example 14 with Polygon

use of org.locationtech.jts.geom.Polygon in project jena by apache.

the class GMLReaderTest method testExtractMultiSurface2.

/**
 * Test of extract method, of class GMLReader.
 *
 * @throws org.jdom2.JDOMException
 * @throws java.io.IOException
 */
@Test
public void testExtractMultiSurface2() throws JDOMException, IOException {
    Polygon[] polygons = new Polygon[2];
    polygons[0] = GEOMETRY_FACTORY.createPolygon(new CustomCoordinateSequence(CoordinateSequenceDimensions.XY, "40 40, 20 45, 45 30, 40 40"));
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XY, "20 35, 10 30, 10 10, 30 5, 45 20, 20 35"));
    LinearRing[] holes = new LinearRing[] { GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XY, "30 20, 20 15, 20 25, 30 20")) };
    polygons[1] = GEOMETRY_FACTORY.createPolygon(shell, holes);
    Geometry geometry = GEOMETRY_FACTORY.createMultiPolygon(polygons);
    GMLReader expResult = new GMLReader(geometry, 2, SRS_URI.OSGB36_CRS);
    String gmlText = "<gml:MultiSurface xmlns:gml=\"http://www.opengis.net/ont/gml\" srsName=\"http://www.opengis.net/def/crs/EPSG/0/27700\"><gml:surfaceMember><gml:Polygon srsName=\"http://www.opengis.net/def/crs/EPSG/0/27700\"><gml:exterior><gml:LinearRing><gml:posList>40 40 20 45 45 30 40 40</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:surfaceMember><gml:surfaceMember><gml:Polygon srsName=\"http://www.opengis.net/def/crs/EPSG/0/27700\"><gml:exterior><gml:LinearRing><gml:posList>20 35 10 30 10 10 30 5 45 20 20 35</gml:posList></gml:LinearRing></gml:exterior><gml:interior><gml:LinearRing><gml:posList>30 20 20 15 20 25 30 20</gml:posList></gml:LinearRing></gml:interior></gml:Polygon></gml:surfaceMember></gml:MultiSurface>";
    GMLReader result = GMLReader.extract(gmlText);
    // 
    // 
    assertEquals(expResult, result);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) CustomCoordinateSequence(org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence) LinearRing(org.locationtech.jts.geom.LinearRing) Test(org.junit.Test)

Example 15 with Polygon

use of org.locationtech.jts.geom.Polygon in project jena by apache.

the class WKTWriterTest method testWriteMultiPolygon2.

/**
 * Test of write method, of class WKTWriter.
 */
@Test
public void testWriteMultiPolygon2() {
    Polygon[] polygons = new Polygon[2];
    polygons[0] = GEOMETRY_FACTORY.createPolygon(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "40 40 0 1, 20 45 0 1, 45 30 0 1, 40 40 0 1"));
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "20 35 0 1, 10 30 0 1, 10 10 0 1, 30 5 0 1, 45 20 0 1, 20 35 0 1"));
    LinearRing[] holes = new LinearRing[] { GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "30 20 0 1, 20 15 0 1, 20 25 0 1, 30 20 0 1")) };
    polygons[1] = GEOMETRY_FACTORY.createPolygon(shell, holes);
    Geometry geometry = GEOMETRY_FACTORY.createMultiPolygon(polygons);
    GeometryWrapper geometryWrapper = new GeometryWrapper(geometry, SRS_URI.WGS84_CRS, WKTDatatype.URI, new DimensionInfo(4, 3, 2));
    String expResult = "<" + SRS_URI.WGS84_CRS + "> MULTIPOLYGON ZM(((40 40 0 1, 20 45 0 1, 45 30 0 1, 40 40 0 1)), ((20 35 0 1, 10 30 0 1, 10 10 0 1, 30 5 0 1, 45 20 0 1, 20 35 0 1), (30 20 0 1, 20 15 0 1, 20 25 0 1, 30 20 0 1)))";
    String result = WKTWriter.write(geometryWrapper);
    // 
    // 
    assertEquals(expResult, result);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) DimensionInfo(org.apache.jena.geosparql.implementation.DimensionInfo) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) CustomCoordinateSequence(org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence) LinearRing(org.locationtech.jts.geom.LinearRing) Test(org.junit.Test)

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