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

2人阅读 评论(0) 收藏 举报
分类:

转载出处: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());
}
 

查看评论

java 接口 代码实例

  • 2011年04月02日 10:43
  • 358B
  • 下载

java 异常处理 代码实例

  • 2011年04月02日 10:59
  • 5KB
  • 下载

批处理执行java代码

  • 2009年01月12日 14:12
  • 4KB
  • 下载

Java代码实例

  • 2016年06月21日 22:44
  • 6.78MB
  • 下载

JSP写Java代码举例

一、引入Java的Jar包二、写Java代码             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")...
  • suyu_happy
  • suyu_happy
  • 2018-01-19 00:39:18
  • 35

JAVA一个代码实例

  • 2008年08月04日 10:11
  • 340B
  • 下载

JAVA之23种设计模式的完整实例代码和注释

  • 2017年02月15日 09:18
  • 2.47MB
  • 下载

Java Concurrency代码实例之四-锁

本文的读者应该是已经掌握了基本的Java多线程开发技巧,但不熟悉Java Concurrency包的程序员。本文是本系列的第四篇文章,前三篇文章请看这里: https://zhuanlan.zhih...
  • LogicTeamLeader
  • LogicTeamLeader
  • 2017-06-24 22:24:55
  • 376

java设计模式之-建造者模式

建造者模式能够将复杂的构建与其表示相分离,是的同样的构建过程可以创建出不同的表示。     建造者模式与抽象工厂的区别是:在建造者模式里,有个指导者,这个指导者来管理建造者,用户与指导者相互联系,指...
  • asdf544265772
  • asdf544265772
  • 2014-08-23 17:31:25
  • 1279

设计模式之状态模式Java版本实现

  • 2013年05月05日 19:32
  • 60KB
  • 下载
    个人资料
    等级:
    访问量: 196
    积分: 118
    排名: 125万+
    文章存档