fct2001140269的博客

如今你的气质里,藏着你走过的路,读过的书和爱过的人。” ——《卡萨布兰卡》...

Java com.vividsolutions.jts.geom.MultiPolygon 代码实例

转载出处:https://www.helplib.com/Java_API_Classes/article_67172

以下是展示如何使用com.vividsolutions.jts.geom.MultiPolygon的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


复制代码
private String transformMultiPolygonToJsonPolygonArray(MultiPolygon multipolygon){
	LOGGER.info("Transforming multipolygon to JSON polygon array");
	/* 
	 * For a multi polygon, we make several simple polygons and put them into an array
	 */
	String polygonArray = "[";
	boolean firstElement = true;
	for (int i = 0; i < multipolygon.getNumGeometries(); i++){
		Polygon polygon = (Polygon) multipolygon.getGeometryN(i);
		String aPolygon = transformPolygonToJsonPolygon(polygon);
		if (firstElement){
			polygonArray = polygonArray.concat(aPolygon);
			firstElement = false;
		} else {
			polygonArray = polygonArray.concat(", " + aPolygon);
		}
	}
	return polygonArray;
}
 

实例 2


代码复制成功
protected static Geometry transformMultiPolygon(CoordinateTransform ct, MultiPolygon multiPolygon) {
    Polygon[] polygon = new Polygon[multiPolygon.getNumGeometries()];
    for (int i = 0; i < polygon.length; ++i) {
        polygon[i] = multiPolygon.getFactory().createPolygon(transformCoordinates(ct,
                                                                                  multiPolygon.getGeometryN(i).getCoordinates()));
    }
    return multiPolygon.getFactory().createMultiPolygon(polygon);
}
 

实例 3


复制代码
/**
 * Converts a @link com.vividsolutions.jts.geom.MultiPolygon to a @link net.opengis.gml.v_3_2_1.MultiSurfaceType
 * Note:  MultiPolygon maps to gml MultiSurfaceType
 *
 * @param multiPolygon
 * @return MultiSurfaceType
 */
public static MultiSurfaceType convertToMultiSurfaceType(MultiPolygon multiPolygon,
        String srsName) {
    MultiSurfaceType multiSurfaceType = GML320_OBJECT_FACTORY.createMultiSurfaceType();
    for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
        Polygon poly = (Polygon) multiPolygon.getGeometryN(i);
        PolygonType polygonType = convertToPolygonType(poly, srsName);
        JAXBElement<PolygonType> polygonTypeJAXBElement = GML320_OBJECT_FACTORY
                .createPolygon(polygonType);
        SurfacePropertyType surfacePropertyType = GML320_OBJECT_FACTORY
                .createSurfacePropertyType();
        surfacePropertyType.setAbstractSurface(polygonTypeJAXBElement);
        multiSurfaceType.getSurfaceMember().add(surfacePropertyType);
    }
    multiSurfaceType.setSrsName(srsName);
    return multiSurfaceType;
}
 

实例 4


复制代码
private static void addElemInfo(List elemInfoList, MultiPolygon polys,
    final int STARTING_OFFSET, final int GTYPE) {
    Polygon poly;
    int offset = STARTING_OFFSET;
    int LEN = D(GTYPE) + L(GTYPE);
    for (int i = 0; i < polys.getNumGeometries(); i++) {
        poly = (Polygon) polys.getGeometryN(i);
        addElemInfo(elemInfoList, poly, offset, GTYPE);
        if( isRectangle( poly )){
            offset += (2 * LEN);                
        }
        else {
            offset += (poly.getNumPoints() * LEN);                
        }            
    }
}
 

实例 5


复制代码
public int getLength(Object geometry) {
    MultiPolygon multi;
    if (geometry instanceof MultiPolygon) {
        multi = (MultiPolygon) geometry;
    } else {
        multi = geometryFactory
                .createMultiPolygon(new Polygon[] { (Polygon) geometry });
    }
    int nrings = 0;
    for (int t = 0; t < multi.getNumGeometries(); t++) {
        Polygon p;
        p = (Polygon) multi.getGeometryN(t);
        nrings = nrings + 1 + p.getNumInteriorRing();
    }
    int npoints = multi.getNumPoints();
    int length;
    if (shapeType == ShapeType.POLYGONZ) {
        length = 44 + (4 * nrings) + (16 * npoints) + (8 * npoints) + 16
                + (8 * npoints) + 16;
    } else if (shapeType == ShapeType.POLYGONM) {
        length = 44 + (4 * nrings) + (16 * npoints) + (8 * npoints) + 16;
    } else if (shapeType == ShapeType.POLYGON) {
        length = 44 + (4 * nrings) + (16 * npoints);
    } else {
        throw new IllegalStateException(
                "Expected ShapeType of Polygon, got " + shapeType);
    }
    return length;
}
 

实例 6


复制代码
protected void writeGeometry(Geometry geom) throws IOException {
    MultiPolygon mpoly = (MultiPolygon) geom;
    for (int i = 0; i < mpoly.getNumGeometries(); i++) {
        super.writeGeometry(mpoly.getGeometryN(i));
    }
}
 

实例 7


复制代码
public void encodeMultiSurface(MultiPolygon mp, StringBuffer buff,
        Map<String, String> abstractGeometryAttributeMap, int srsDimension) {
    encodeStartTag("MultiSurface", buff, abstractGeometryAttributeMap);
    encodeStartTag("surfaceMembers", buff, null);
    for (int i = 0; i < mp.getNumGeometries(); i++) {
        Polygon p = (Polygon) mp.getGeometryN(i);
        encodePolygon(p, buff, null, srsDimension);
    }
    encodeEndTag("surfaceMembers", buff);
    encodeEndTag("MultiSurface", buff);
}
 

实例 8


复制代码
/**
 * Tests a valid multipolygon deserialization
 *
 * @throws java.io.IOException When an unexpected exception is thrown (fails the test)
 */
@Test
public void testMultiPolygon() throws IOException {
    String testString = "{ "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG:7.6:31370"}}, "type": "MultiPolygon", "coordinates": [    [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],    [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],     [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]    ]}";
    MultiPolygon result = (MultiPolygon) JTS.to(assembler.fromTransferObject(mapper.readValue(testString, MultiPolygonTo.class)));
    Assert.assertEquals(LAMBERT72.getCode(), result.getSRID());
    Assert.assertEquals(2, result.getNumGeometries());
    com.vividsolutions.jts.geom.Geometry first = result.getGeometryN(0);
    com.vividsolutions.jts.geom.Geometry second = result.getGeometryN(1);
    Assert.assertTrue(first instanceof Polygon);
    Assert.assertTrue(second instanceof Polygon);
    Polygon polygon = (Polygon) first;
    Assert.assertEquals(LAMBERT72.getCode(), polygon.getSRID());
    Assert.assertEquals(0, polygon.getNumInteriorRing());
    LineString ls = polygon.getExteriorRing();
    Assert.assertEquals(LAMBERT72.getCode(), ls.getSRID());
    Coordinate[] coords = ls.getCoordinates();
    Assert.assertEquals(5, coords.length);
    Assert.assertEquals(coords[0], coords[4]);
    polygon = (Polygon) second;
    Assert.assertEquals(LAMBERT72.getCode(), polygon.getSRID());
    Assert.assertEquals(1, polygon.getNumInteriorRing());
    ls = polygon.getExteriorRing();
    Assert.assertEquals(LAMBERT72.getCode(), ls.getSRID());
    coords = ls.getCoordinates();
    Assert.assertEquals(5, coords.length);
    Assert.assertEquals(coords[0], coords[4]);
    ls = polygon.getInteriorRingN(0);
    Assert.assertEquals(LAMBERT72.getCode(), ls.getSRID());
    coords = ls.getCoordinates();
    Assert.assertEquals(5, coords.length);
    Assert.assertEquals(coords[0], coords[4]);
}
 

实例 9


复制代码
public void testMultiSurface() {
        GeometryAttribute[] array = new GeometryAttribute[2];
        AbstractPolicy p1 = TestSupport.policyFromFile(TestSupport.getGeoXACMLFNFor("gml3",
                "MultiSurfacePolicy.xml"));
        array[0] = TestSupport.getGeometryAttribute(p1);
        AbstractPolicy p2 = policyFromAttributeEncoding(array[0], xmlTemplate);
        array[1] = TestSupport.getGeometryAttribute(p2);
        for (GeometryAttribute attr : array) {
            assertTrue(attr.getSrsName().equals("EPSG:4326"));
            assertTrue(attr.getGid().equals("MultiSurface"));
            assertTrue(attr.getGmlVersion() == GMLVersion.Version3);
            MultiPolygon mpoly = (MultiPolygon) attr.getGeometry();
            Polygon poly = (Polygon) mpoly.getGeometryN(0);
            Coordinate[] coords = poly.getExteriorRing().getCoordinates();
            assertTrue(coords[0].x == 0);
            assertTrue(coords[0].y == 0);
            assertTrue(coords[1].x == 0);
            assertTrue(coords[1].y == 100);
            assertTrue(coords[2].x == 100);
            assertTrue(coords[2].y == 100);
            assertTrue(coords[3].x == 100);
            assertTrue(coords[3].y == 0);
            assertTrue(coords[4].x == 0);
            assertTrue(coords[4].y == 0);
            poly = (Polygon) mpoly.getGeometryN(1);
            coords = poly.getExteriorRing().getCoordinates();
            assertTrue(coords[0].x == 0);
            assertTrue(coords[0].y == 0);
            assertTrue(coords[1].x == 0);
            assertTrue(coords[1].y == -100);
            assertTrue(coords[2].x == -100);
            assertTrue(coords[2].y == -100);
            assertTrue(coords[3].x == -100);
            assertTrue(coords[3].y == 0);
            assertTrue(coords[4].x == 0);
            assertTrue(coords[4].y == 0);
        }
    }
 

实例 10


复制代码
/**
 * Returns a completed multi type.
 *
 * @param geometryFactory The factory this method should use to create the
 *        multi type.
 *
 * @return Appropriate multi geometry type.
 */
public Geometry create(GeometryFactory geometryFactory) {
    if (internalType.equals("Point")) {
        Point[] pointArray = geometryFactory.toPointArray(geometries);            
        MultiPoint multiPoint = geometryFactory.createMultiPoint(pointArray);
        multiPoint.setUserData( getSRS() );
        multiPoint.setSRID( getSRID() );
        LOGGER.fine("created " + multiPoint);
        return multiPoint;
    } else if (internalType.equals("LineString")) {
        LineString[] lineStringArray = geometryFactory
                .toLineStringArray(geometries);
        MultiLineString multiLineString = geometryFactory.createMultiLineString(lineStringArray);
        multiLineString.setUserData( getSRS() );
        multiLineString.setSRID( getSRID() );
        LOGGER.fine("created " + multiLineString);
        return multiLineString;
    } else if (internalType.equals("Polygon")) {
        Polygon[] polygonArray = geometryFactory.toPolygonArray(geometries);
        MultiPolygon multiPolygon = geometryFactory.createMultiPolygon(polygonArray);
        multiPolygon.setUserData( getSRS() );
        multiPolygon.setSRID( getSRID() );
        LOGGER.fine("created " + multiPolygon);
        return multiPolygon;
    } else {
        return null;
    }
}
 

实例 11


复制代码
private MultiPolygon randomMultiPolygon(int srid) {
    MultiPolygon geometry =
            geometryFactory.createMultiPolygon(new Polygon[] { randomPolygon(srid), randomPolygon(srid),
                    randomPolygon(srid) });
    geometry.setSRID(srid);
    return geometry;
}
 

实例 12


复制代码
/**
 * Returns a completed multi type.
 *
 * @param geometryFactory The factory this method should use to create the
 *        multi type.
 *
 * @return Appropriate multi geometry type.
 */
public Geometry create(GeometryFactory geometryFactory) {
    if (internalType.equals("Point")) {
        Point[] pointArray = geometryFactory.toPointArray(geometries);            
        MultiPoint multiPoint = geometryFactory.createMultiPoint(pointArray);
        multiPoint.setUserData( getSRS() );
        multiPoint.setSRID( getSRID() );
        LOGGER.fine("created " + multiPoint);
        return multiPoint;
    } else if (internalType.equals("LineString")) {
        LineString[] lineStringArray = geometryFactory
                .toLineStringArray(geometries);
        MultiLineString multiLineString = geometryFactory.createMultiLineString(lineStringArray);
        multiLineString.setUserData( getSRS() );
        multiLineString.setSRID( getSRID() );
        LOGGER.fine("created " + multiLineString);
        return multiLineString;
    } else if (internalType.equals("Polygon")) {
        Polygon[] polygonArray = geometryFactory.toPolygonArray(geometries);
        MultiPolygon multiPolygon = geometryFactory.createMultiPolygon(polygonArray);
        multiPolygon.setUserData( getSRS() );
        multiPolygon.setSRID( getSRID() );
        LOGGER.fine("created " + multiPolygon);
        return multiPolygon;
    } else {
        return null;
    }
}
 

实例 13


复制代码
/**
 * Returns a completed multi type.
 *
 * @param geometryFactory The factory this method should use to create the
 *        multi type.
 *
 * @return Appropriate multi geometry type.
 */
public Geometry create(GeometryFactory geometryFactory) {
    if (internalType.equals("Point")) {
        Point[] pointArray = geometryFactory.toPointArray(geometries);            
        MultiPoint multiPoint = geometryFactory.createMultiPoint(pointArray);
        multiPoint.setUserData( getSRS() );
        multiPoint.setSRID( getSRID() );
        LOGGER.fine("created " + multiPoint);
        return multiPoint;
    } else if (internalType.equals("LineString")) {
        LineString[] lineStringArray = geometryFactory
                .toLineStringArray(geometries);
        MultiLineString multiLineString = geometryFactory.createMultiLineString(lineStringArray);
        multiLineString.setUserData( getSRS() );
        multiLineString.setSRID( getSRID() );
        LOGGER.fine("created " + multiLineString);
        return multiLineString;
    } else if (internalType.equals("Polygon")) {
        Polygon[] polygonArray = geometryFactory.toPolygonArray(geometries);
        MultiPolygon multiPolygon = geometryFactory.createMultiPolygon(polygonArray);
        multiPolygon.setUserData( getSRS() );
        multiPolygon.setSRID( getSRID() );
        LOGGER.fine("created " + multiPolygon);
        return multiPolygon;
    } else {
        return null;
    }
}
 

实例 14


复制代码
protected MultiPolygon buildMultiPolygon(String multip) {
    try {
        WKTReader reader = new WKTReader();
        MultiPolygon mp = (MultiPolygon) reader.read(multip);
        mp.setSRID(4326);
        return mp;
    } catch (ParseException ex) {
        throw new RuntimeException("Unexpected exception: " + ex.getMessage(), ex);
    }
}
 

实例 15


复制代码
@Test
public void multiPolygonToISOTest() throws Exception {
    GeometryFactory fact = new GeometryFactory();
    final Coordinate[] coordinates = new Coordinate[5];
    coordinates[0] = new Coordinate(0, 0);
    coordinates[1] = new Coordinate(0, 1);
    coordinates[2] = new Coordinate(1, 1);
    coordinates[3] = new Coordinate(1, 0);
    coordinates[4] = new Coordinate(0, 0);
    LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
    Polygon poly = new Polygon(linear, null, fact);
    poly.setSRID(2154);
    final Coordinate[] coordinates2 = new Coordinate[5];
    coordinates2[0] = new Coordinate(10, 10);
    coordinates2[1] = new Coordinate(10, 11);
    coordinates2[2] = new Coordinate(11, 11);
    coordinates2[3] = new Coordinate(11, 10);
    coordinates2[4] = new Coordinate(10, 10);
    LinearRing linear2 = new GeometryFactory().createLinearRing(coordinates2);
    Polygon poly2 = new Polygon(linear2, null, fact);
    poly2.setSRID(2154);
    Polygon[] polygons = new Polygon[2];
    polygons[0] = poly;
    polygons[1] = poly2;
    MultiPolygon m = new MultiPolygon(polygons, fact);
    m.setSRID(2154);
    AbstractGeometry gml = JTStoGeometry.toGML("3.2.1", m);
    final Geometry geom = GeometrytoJTS.toJTS(gml);
    //System.out.println(geom);
    System.out.println("SRID:"+ geom.getSRID());
}
 

阅读更多
个人分类: GIS
上一篇scala(List)
下一篇设计模式学习总结
想对作者说点什么? 我来说一句

java 接口 代码实例

2011年04月02日 358B 下载

java多数据源代码实例

2018年05月29日 56.21MB 下载

java 异常处理 代码实例

2011年04月02日 5KB 下载

批处理执行java代码

2009年01月12日 4KB 下载

Java代码实例

2016年06月21日 6.78MB 下载

JAVA一个代码实例

2008年08月04日 340B 下载

Tray Java托盘代码实例

2009年04月09日 3KB 下载

Java 实例代码1

2010年05月17日 17.72MB 下载

没有更多推荐了,返回首页

关闭
关闭