简体   繁体   English

JTS:如何将多边形转换为MultiLineString

[英]JTS: How to convert polygon into MultiLineString

I have polygon shape and I want to convert it to MultiLineString. 我有多边形形状,我想将其转换为MultiLineString。 Note that usually the direction is different: From points, coords, lines etc. using GeometryFactory build polygon. 请注意,通常方向不同:使用GeometryFactory构建多边形从点,坐标,线等。 I started to thinking about GeometryTransformer but it's hard to understand the documentation there... So I have this: 我开始考虑GeometryTransformer,但很难理解那里的文档...所以我有这个:

import com.vividsolutions.jts.geom.*;
...
GeometryFactory gFactory = new GeometryFactory();
GeometryTransformer gTransform = new GeometryTransformer();
Polygon polygon = gFactory.createPolygon(someLinearRing, null);
MultiLineString mlString = polygon.TODO?

How to continue in the TODO ? 如何在TODO继续?

The method Polygon.getBoundary() computes the boundaries of the polygon. 方法Polygon.getBoundary()计算多边形的边界。 If the polygon has not holes (also only one boundary), a object of type LinearRing is returned. 如果多边形没有孔(也只有一个边界),则返回LinearRing类型的对象。 If the polygons has holes - also more than one boundary - a object of type MultiLineString is returned. 如果多边形有孔 - 也有多个边界 - 则返回MultiLineString类型的对象。

Use the methode Polygon.getNumInteriorRing() to check if the polygon has holes and than build a multilinestring is necessary: 使用方法Polygon.getNumInteriorRing()检查多边形是否有孔,并且需要构建多线串:

GeometryFactory gFactory = new GeometryFactory();
if (polygon.getNumInteriorRing() == 0){
  // polygon has not holes, so extract the exterior ring
  // and build a multilinestring
  return gFactory.createMultiLineString(polygon.getExteriorRing());
}

else{
  // Polygon has holes, also several boundaries. 
  // Simply run getBoundary(), it will return a multilinestring
  return polygon.getBoundary();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM