【发布时间】:2014-02-24 23:11:51
【问题描述】:
假设我想用 JTS 计算两个几何图形之间的距离,但中间还有一个我不能穿过(好像是一堵墙)。它可能看起来像这样:
我想知道如何计算。
在这种情况下,这些形状 geom1 和 geom2 距离为 38.45 米,因为我是直接计算出来的。但如果我不想越过那条线,我应该从北边围起来,距离大概有70多米。
我们可以认为我们可以在中间有一条线、一个多边形或其他任何东西。
我想知道 JTS 中是否有任何内置函数,或者我能告诉你的其他东西。我想如果有什么问题,我应该检查一下其他解决方法,因为尝试解决复杂的路由问题超出了我的知识范围。
这是一段直接使用 JTS 作为距离的代码,它仍然不会考虑中间的几何。
import org.apache.log4j.Logger;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
public class distanceTest {
private final static Logger logger = Logger.getLogger("distanceTest");
public static void main(String [] args) {
//Projection : EPSG:32631
// We build one of the geometries on one side
String sGeom1="POLYGON ((299621.3240601513 5721036.003245114, 299600.94820609683 5721085.042327096, 299587.7719688322 5721052.9152064435, 299621.3240601513 5721036.003245114))";
Geometry geom1=distanceTest.buildGeometry(sGeom1);
// We build the geometry on the other side
String sGeom2=
"POLYGON ((299668.20990794065 5721092.766132105, 299647.3623194871 5721073.557249224, 299682.8494029705 5721049.148841454, 299668.20990794065 5721092.766132105))";
Geometry geom2=distanceTest.buildGeometry(sGeom2);
// There is a geometry in the middle, as if it was a wall
String split=
"LINESTRING (299633.6804935104 5721103.780167559, 299668.99872434285 5720999.981241705, 299608.8457218057 5721096.601805294)";
Geometry splitGeom=distanceTest.buildGeometry(split);
// We calculate the distance not taking care of the wall in the middle
double distance = geom1.distance(geom2);
logger.error("Distance : " + distance);
}
public static Geometry buildGeometry(final String areaWKT) {
final WKTReader fromText = new WKTReader();
Geometry area;
try {
area = fromText.read(areaWKT);
}
catch (final ParseException e) {
area = null;
}
return area;
}
}
【问题讨论】:
标签: geometry distance geospatial spatial jts