我有一个shapefile定义覆盖城市的形状.我还有一组由纬度和经度定义的坐标.
我需要能够确定这些点是否在shapefile中的任何形状内.
目前我正在尝试使用easygis.net来解决这个问题,但它似乎并没有起作用.
我相信shapefile中的坐标是UTM,但偏移为北,我不知道如何纠正它,将其转换为纬度/经度,或转换我的纬度/长度对匹配.
我一直在寻找像dotpatial和sharpmap这样的其他库,但我没有看到问题的直观答案.
与此同时,我确信这是一个已经解决的问题.是否有任何库可以轻松完成此操作?或者我如何将地图的偏移UTM转换为纬度/经度,或者我的纬度/经度点转换为此偏移量UTM?
下面是一些示例代码,介绍如何使用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;
}
Run Code Online (Sandbox Code Playgroud)
或者如何将地图的偏移 UTM 转换为纬度/经度,或将我的纬度/经度点转换为该偏移 UTM?
这需要重新投影您的点(或 shapefile 的数据)。这可以通过Proj4Net完成(除非您的 GIS 已经支持)。
一旦完成,它就是多边形测试中的一个点。尽管我经常使用绕数法,但有很多选择。