gpt4 book ai didi

c# - 如何使用 Net Topology Suite 在 Entity Framework Core 中缓冲一个点(地理)1 米

转载 作者:行者123 更新时间:2023-12-03 08:50:03 24 4
gpt4 key购买 nike

我正在将 Entity Framework Core (3.1.0) 与 Net Topology Suite 包一起使用 recommended here ,并且我很难缓冲一个点以在其周围创建圆形多边形。如果我将值 1 传递给 Buffer(),我相信这意味着 1 米,那么我最终会得到一个半径接近于的圆120 英里。

我应该用什么值来表示 1 米?我使用 SRID 为 4326 的 GeometryFactory 来创建点和多边形。我正在使用的代码大致如下:

GeometryFactory Geography = NtsGeometryServices.Instance.CreateGeometryFactory(4326);

var point = Geography.CreatePoint(coordinate.Latitude, coordinate.Longitude);
var polygonCoordinates = point.Buffer(1).Normalized().Reverse().Coordinates;
var polygon = Geography.CreatePolygon(polygonCoordinates);

最佳答案

发布问题后,我决定再次阅读 EF Core 的 Spatial 文档。正如 @Michael Entin 在评论中提到的,文档指出客户端 NTS 在执行计算时会忽略 SRID。您的输入必须投影到不同的坐标系中,进行计算并投影回来。就我而言,我必须从 4326 转到 2855。

NOTE: 2855 is the US transformation in meters, for feet use 2926. For spatial references look here, and for their visualizations look here.

我查看了示例代码,并决定将其复制到 LINQPad 中,以便我可以更详细地浏览它。正如当前存在的那样,示例代码“有效”仅适用于具有单个坐标的几何图形。如果您传入具有多个坐标的多边形几何图形(例如我正在使用的缓冲区多边形),那么它只会转换第一个坐标并忽略其余坐标。您必须循环遍历剩余的坐标,从中创建点,变换点,并从中获取坐标。

经过更多的尝试和错误,我更新了示例代码,以便它可以处理单坐标和多坐标几何图形。在 Google map 上检查转换后的坐标似乎可以验证其是否正确。

这是为其他遇到此问题的人提供的更新的投影代码:

public static class CoordinateExtensions {
public static Coordinate ProjectTo(
this Coordinate coordinate,
int fromSrid,
int toSrid) {
var point = new Point(coordinate) {
SRID = fromSrid
};

return point.ProjectTo(toSrid).Coordinate;
}
}

public static class GeometryExtensions {
private static readonly CoordinateSystemServices Services = new CoordinateSystemServices(new Dictionary<int, string> {
{ 4326, GeographicCoordinateSystem.WGS84.WKT },
{ 2855, @"
PROJCS[""NAD83(HARN) / Washington North"",
GEOGCS[""NAD83(HARN)"",
DATUM[""NAD83_High_Accuracy_Reference_Network"",
SPHEROID[""GRS 1980"",6378137,298.257222101,
AUTHORITY[""EPSG"",""7019""]],
TOWGS84[0, 0, 0, 0, 0, 0, 0],
AUTHORITY[""EPSG"", ""6152""]],
PRIMEM[""Greenwich"", 0,
AUTHORITY[""EPSG"", ""8901""]],
UNIT[""degree"", 0.0174532925199433,
AUTHORITY[""EPSG"", ""9122""]],
AUTHORITY[""EPSG"", ""4152""]],
PROJECTION[""Lambert_Conformal_Conic_2SP""],
PARAMETER[""standard_parallel_1"", 48.73333333333333],
PARAMETER[""standard_parallel_2"", 47.5],
PARAMETER[""latitude_of_origin"", 47],
PARAMETER[""central_meridian"", -120.8333333333333],
PARAMETER[""false_easting"", 500000],
PARAMETER[""false_northing"", 0],
UNIT[""metre"", 1,
AUTHORITY[""EPSG"", ""9001""]],
AXIS[""X"", EAST],
AXIS[""Y"", NORTH],
AUTHORITY[""EPSG"", ""2855""]]
" }
});

public static Geometry ProjectTo(
this Geometry geometry,
int toSrid) {
if (geometry.Coordinates.Length == 1) {
return geometry.ProjectToSingle(toSrid);
}

return geometry.ProjectToMany(toSrid);
}

private static Geometry ProjectToSingle(
this Geometry geometry,
int toSrid) {
var transformation = Services.CreateTransformation(geometry.SRID, toSrid);
var transformer = new MathTransformFilter(transformation.MathTransform);
var transformed = geometry.Copy();

transformed.Apply(transformer);
transformed.SRID = toSrid;

return transformed;
}

private static Geometry ProjectToMany(
this Geometry geometry,
int toSrid) {
var fromSrid = geometry.SRID;
var transformed = geometry.Copy();

for (var i = 0; i < transformed.Coordinates.Length; i++) {
var coordinate = transformed.Coordinates[i].ProjectTo(fromSrid, toSrid);

transformed.Coordinates[i].CoordinateValue = coordinate.CoordinateValue;
}

transformed.SRID = toSrid;

return transformed;
}
}

public sealed class MathTransformFilter :
ICoordinateSequenceFilter {
private readonly MathTransform Transform;

public MathTransformFilter(
MathTransform transform) => Transform = transform;

public bool Done => false;
public bool GeometryChanged => true;

public void Filter(
CoordinateSequence seq,
int i) {
var result = Transform.Transform(new[] {
seq.GetOrdinate(i, Ordinate.X),
seq.GetOrdinate(i, Ordinate.Y)
});

seq.SetOrdinate(i, Ordinate.X, result[0]);
seq.SetOrdinate(i, Ordinate.Y, result[1]);
}
}

关于c# - 如何使用 Net Topology Suite 在 Entity Framework Core 中缓冲一个点(地理)1 米,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59557470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com