假设我想要计算两个几何体之间的距离与JTS,但是中间还有一个我不能穿过的距离(就好像它是一堵墙)。它看起来是这样的: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.5, 299647.3623194871 5721073.557249224, 299682.8494029705 5721049.148841454, 299668.20990794065 5721092.5))";
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;
}
}
这太棒了,乔恩。我认为它适合我的实际要求。使用我使用的Java库的JTS进行编码应该很容易。非常感谢你。 – krause
@krause我们非常欢迎您,很高兴我能提供帮助。 –