【问题标题】:How to determine if a point is within a shape in a shapefile?如何确定一个点是否在 shapefile 的形状内?
【发布时间】:2013-05-31 17:31:18
【问题描述】:

我有一个 shapefile 定义了覆盖城市的形状。我还有一组由纬度和经度定义的坐标。

我需要能够确定这些点是否在 shapefile 中的任何形状内。

目前我正在尝试使用 easygis.net 来解决这个问题,但它似乎不起作用。

我相信 shapefile 中的坐标在 UTM 中,但有一个偏移北,我不知道如何更正它,将其转换为纬度/经度,或转换我的纬度/经度对以匹配。

我一直在查看其他库,例如 dotspatial 和 sharpmap,但我没有看到问题的直观答案。

同时,我确信这是一个已经解决的问题。有没有图书馆可以轻松做到这一点?或者如何将地图的偏移 UTM 转换为 lat/long,或者我的 lat/long 点如何转换为这个偏移 UTM?

【问题讨论】:

  • 你的数据是北半球还是南半球?
  • 北半球。东移似乎是正确的,但北移将位置置于纬度 1 度,这是不正确的。

标签: c# gis


【解决方案1】:

这里有一些示例代码,介绍如何使用 DotSpatial 首先处理重投影,然后使用 Contains 方法测试点是否在多边形中。

    public bool PointInShape() {
        // Load a shapefile.  If the shapefile is already using your custom projection, we don't need to change it.
        Shapefile wierdShapefile = Shapefile.OpenFile("C:\\MyShapefile.shp");

        // Note, if your shapefile with custom projection has a .prj file, then we don't need to mess with defining the projection.
        // If not, we can define the projection as follows:

        // First get a ProjectionInfo class for the normal UTM projection
        ProjectionInfo pInfo = DotSpatial.Projections.KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone10N;

        // Next modify the pINfo with your custom False Northing
        pInfo.FalseNorthing = 400000;

        wierdShapefile.Projection = pInfo;

        // Reproject the strange shapefile so that it is in latitude/longitude coordinates
        wierdShapefile.Reproject(DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984);

        // Define the WGS84 Lat Lon point to test
        Coordinate test = new Coordinate(-120, 40);

        foreach (Feature f in wierdShapefile.Features) {
            Polygon pg = f.BasicGeometry as Polygon;
            if (pg != null)
            {
                if (pg.Contains(new Point(test)))
                {
                    // If the point is inside one of the polygon features
                    return true;
                }
            }
            else {
                // If you have a multi-part polygon then this should also handle holes I think
                MultiPolygon polygons = f.BasicGeometry as MultiPolygon;
                if (polygons.Contains(new Point(test))) {
                    return true;
                }
            }
        }

        return false;
    }

【讨论】:

  • 这与我最终实现的非常接近。
  • 酷 =)。我希望其他偶然发现此问题的人有一个代码示例。如果速度给您带来问题(例如特征加载缓慢),我认为您可以使用形状本身进行相交测试。 Shapes 只加载向量,不需要 shapefile 读取所有属性。
【解决方案2】:

或者我如何将地图的偏移 UTM 转换为 lat/long,或者我的 lat/long 点到这个偏移 UTM?

这需要重新投影您的点(或 shapefile 的数据)。这可以通过Proj4Net 完成(除非您的 GIS 已经支持它)。

一旦完成,它就是多边形测试中的一个点。还有many options for this,虽然我经常用winding number method

【讨论】:

  • 我很肯定这会奏效,但我在转换时遇到了问题。我从投影文件中读取 WKT,然后创建一个从 WGS84 到读取投影的转换器。将我的纬度/经度坐标弹出到变压器中,它应该可以工作。对?相反,我得到的坐标很差。
【解决方案3】:

如果您只需要将投影文件转换为地理坐标,那么 Qgis 是我认为最容易使用的工具,它是一款免费且开放的基于 GRASS 的轻量级 GIS 软件。使用直接的投影工具。 http://www.qgis.org/

【讨论】:

  • 谢谢。我看了看,但我不确定它是否符合我的要求。我在网站上没有看到 API,也没有看到可以与 C# 交互的东西。我确实设法与 DotSpatial 和 Proj.Net 一起工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 2020-04-01
  • 2012-02-02
相关资源
最近更新 更多