【发布时间】:2018-12-14 17:12:51
【问题描述】:
说明
所以基本上我想知道的是LineString(或MultiLineString)是否包含在MultiPolygon中,或者它是否与它相交。
我不完全确定它是否会影响,但实际情况是我有一个代表远足径的LineString,我需要知道通过哪些世界区域(这将是我的MultiPolygon s) 是那条轨道。或者如果它在我的世界区域之一内,我需要知道哪个区域包含该LineString。
我的问题
我的问题是我得到了误报。如果我使用Point(查看它是否包含在MultiPolygon 中)而不是LineString 执行相同的检查,它工作得非常好。
实现目标的步骤:
- 将我的
LineString从 EPSG 3857 格式处理为 EPSG 4326 格式(使用此公式 Converting coordinates from EPSG 3857 to 4326 DotSpatial ),因为我收到的LineString是 EPSG 3857 格式。 - 阅读我的
Geometries(使用Gson和JtsAdapter,因为它们是GeoJson格式,具体来说它们是MultiPolygons)。 这点应该不是问题,因为我用它来检测一个点是否在其他地方的多边形内,并且工作完全正常。 - 检查我的
LineString是否与任何MultiPolygons 相交或包含在其中,并注册此MultiPolygons 的所有ID。
我的代码:
1.将我的 LineString 从 EPSG 3857 转换为 EPSG 4326:
private fun reprojectFromEpsg3857ToEpsg4326(geometry: Geometry): Geometry
{
val e = 2.7182818284
val X = 20037508.34
if (geometry is LineString) {
for (i in 0 until geometry.numPoints) {
val coordinate = geometry.getCoordinateN(i)
geometry.getCoordinateN(i).x = (coordinate.x * 180) / X
geometry.getCoordinateN(i).y = coordinate.y / (X / 180)
geometry.getCoordinateN(i).y = ((Math.atan(Math.pow(e, ((PI / 180) * geometry.getCoordinateN(i).y)))) / (PI / 360)) - 90
}
} else if (geometry is MultiLineString) {
try {
for (i in 0 until geometry.numGeometries) {
for (j in 0 until geometry.getGeometryN(i).coordinates.size) {
val coordinate = geometry.getGeometryN(i).coordinates[i]
geometry.getGeometryN(i).coordinates[i].x = (coordinate.x * 180) / X
geometry.getGeometryN(i).coordinates[i].y = coordinate.y / (X / 180)
geometry.getGeometryN(i).coordinates[i].y = ((Math.atan(Math.pow(e,((PI / 180) * geometry.getGeometryN(i).coordinates[i].y)))) / (PI / 360)) - 90
}
}
} catch (e: ArrayIndexOutOfBoundsException) {
}
}
return geometry
}
2.使用 Gson 和 Jts 适配器阅读我的Geometries:
private fun getGeometries(gson: Gson): HashMap<Long, Geometry>
{
val geoJsonGeometries = HashMap<Long, Geometry>()
val geometriesFolder = File("geometries-folder")
geometriesFolder.listFiles(getFilenameFilter("geojson")).forEach {
val reader = FileReader(it)
var geometry = gson.fromJson(reader, Geometry::class.java)
if (geometry.geometryType == "GeometryCollection") {
geometry = geometry.getGeometryN(0)
}
val geometryId = java.lang.Long.parseLong(it.name.replace(".geojson", ""))
geoJsonGeometries[geometryId] = geometry
}
return geoJsonGeometries
}
3. 执行检查以查看哪些MultiPolygons 与我的LineString 相关(通过包含或交叉):
val geometryIds = ArrayList<Long>()
geometries.forEach { geometryId, mapBoundaries ->
if (routeAsLineString.intersects(mapBoundaries) || mapBoundaries.contains(routeAsLineString)) {
geometryIds.add(geometryId)
}
}
任何帮助将不胜感激!
【问题讨论】:
标签: java kotlin intersection jts