如何使用 Net Topology Suite 在 Entity Framework Core 中将一个点(地理)缓冲 1 米
How to Buffer a Point (Geography) by 1 Meter in Entity Framework Core with Net Topology Suite
我正在使用 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 的空间文档。正如@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 中以便更详细地查看它。由于目前存在,示例代码 "works" 但 仅适用于具有单个坐标的几何体。如果您传入具有多个坐标的多边形几何图形(例如我正在使用的缓冲区多边形),那么它只会转换第一个坐标并忽略其余坐标。您必须遍历剩余的坐标,从中创建点,转换点,并从中获取坐标。
经过反复试验,我更新了示例代码,使其可以处理单坐标和多坐标几何。在 Google 地图上检查转换后的坐标似乎可以验证它是否正确。
这里是更新后的投影代码,供遇到此问题的其他人使用:
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]);
}
}
我正在使用 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 的空间文档。正如@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 中以便更详细地查看它。由于目前存在,示例代码 "works" 但 仅适用于具有单个坐标的几何体。如果您传入具有多个坐标的多边形几何图形(例如我正在使用的缓冲区多边形),那么它只会转换第一个坐标并忽略其余坐标。您必须遍历剩余的坐标,从中创建点,转换点,并从中获取坐标。
经过反复试验,我更新了示例代码,使其可以处理单坐标和多坐标几何。在 Google 地图上检查转换后的坐标似乎可以验证它是否正确。
这里是更新后的投影代码,供遇到此问题的其他人使用:
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]);
}
}