【问题标题】:Detect if a LineString / MultiLineString is inside a Polygon or if it intersects it using JTS library使用 JTS 库检测 LineString / MultiLineString 是否在 Polygon 内,或者它是否与它相交
【发布时间】:2018-12-14 17:12:51
【问题描述】:

说明

所以基本上我想知道的是LineString(或MultiLineString)是否包含在MultiPolygon中,或者它是否与它相交。

我不完全确定它是否会影响,但实际情况是我有一个代表远足径的LineString,我需要知道通过哪些世界区域(这将是我的MultiPolygon s) 是那条轨道。或者如果它在我的世界区域之一内,我需要知道哪个区域包含该LineString

我的问题

我的问题是我得到了误报。如果我使用Point(查看它是否包含在MultiPolygon 中)而不是LineString 执行相同的检查,它工作得非常好。

实现目标的步骤:

  1. 将我的 LineString 从 EPSG 3857 格式处理为 EPSG 4326 格式(使用此公式 Converting coordinates from EPSG 3857 to 4326 DotSpatial ),因为我收到的 LineString 是 EPSG 3857 格式。
  2. 阅读我的Geometries(使用GsonJtsAdapter,因为它们是GeoJson格式,具体来说它们是MultiPolygons)。 这点应该不是问题,因为我用它来检测一个点是否在其他地方的多边形内,并且工作完全正常。
  3. 检查我的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


    【解决方案1】:

    好吧,我把 MultiLineString 从 EPSG 3857 格式转换为 EPSG 4326 格式弄错了。基本上,我没有转换MultiLineString,只是转换了第一个坐标。但是MultiLineString 中有很多Geometry 对象,每个对象都有很多坐标。

    所以第 2 步和第 3 步是正确的。

    原来的转换是:

    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
        }
    }
    

    但应该是:

    for (i in 0 until geometry.numGeometries) {
        for (j in 0 until geometry.getGeometryN(i).coordinates.size) {
            val coordinate = geometry.getGeometryN(i).coordinates[j]
            geometry.getGeometryN(i).coordinates[j].x = (coordinate.x * 180) / X
            geometry.getGeometryN(i).coordinates[j].y = coordinate.y / (X / 180)
            geometry.getGeometryN(i).coordinates[j].y = ((Math.atan(Math.pow(e,((PI / 180) * geometry.getGeometryN(i).coordinates[j].y)))) / (PI / 360)) - 90
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-13
      • 1970-01-01
      • 2011-11-13
      • 2011-10-27
      • 1970-01-01
      • 2011-09-28
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多