gpt4 book ai didi

c# - 如何加快经纬度到 SqlGeometry 的转换?

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:21 24 4
gpt4 key购买 nike

我有这段代码,它采用国家的几何形状和一组点,然后它只返回这些国家内的点:

public static IEnumerable<Point> RemovePointsOutsideBorders(IEnumerable<Point> points, IEnumerable<Country> countries)
{
var cc = new List<Point>();

var SPAT_REF_ID = 4326;
foreach (var p in points)
{
var validText = new SqlChars(new SqlString(string.Format("POINT({0} {1})", p.Longitude, p.Latitude)));
var geoPoint = SqlGeometry.STPointFromText(validText, SPAT_REF_ID);

foreach (var c in countries)
{
if(c.Polygon.STIntersects(geoPoint))
{
cc.Add(p);
break;
}
}
}

return cc;
}

当前它非常慢,大约有 4000 个点,具有双倍经/纬度值,从它到 SqlGeometry 的转换很慢(大约需要 25 秒——我需要这个可能下降到一两秒) :

var s = new SqlChars(new SqlString(string.Format("POINT({0} {1})", p.Longitude, p.Latitude)));
var pGeo = SqlGeometry.STPointFromText(s, SPAT_REF_ID);

之所以这样做,是因为 SqlGeometry.Point 采用 x、y 而不是 lat、long ...关于如何加快速度的任何提示?

我已经知道 SqlGeometry (c.Polygon) 可以减少以加快速度,但我无法控制它。我所追求的是一种加快从纬度/经度到 SqlGeometry 点的转换的方法。

最佳答案

这是我最后想出的解决方案,它在半秒内完成了所有事情:

public static IEnumerable<Point> RemovePointsOutsideBorders(IEnumerable<Point> points, IEnumerable<Country> countries)
{
// join all the country polygons into one (make a stamp)
var allCountryPolygon = countries.Select(x => x.Polygon).UnionAll();

// make a single geometry shape from our evenly spaced extent points (cookie dough)
var pointsGeo = PointsToGeometry(points);

// do an intersect (stamp country shape over extent based geometry)
var cookieOfPoints = pointsGeo.STIntersection(allCountryPolygon);

// how many points left inside? pick them all back out
for (int n = 1; n <= cookieOfPoints.STNumPoints(); n++)
{
var insidePoint = cookieOfPoints.STPointN(n);
yield return new Point
{
Longitude = insidePoint.STX.Value,
Latitude = insidePoint.STY.Value
};
}
}

public static SqlGeometry PointsToGeometry(IEnumerable<Point> points)
{
var bld = new SqlGeometryBuilder();
bld.SetSrid(4326);
bld.BeginGeometry(OpenGisGeometryType.MultiPoint);

foreach (var p in points)
{
bld.BeginGeometry(OpenGisGeometryType.Point);
bld.BeginFigure(p.Longitude, p.Latitude);
bld.EndFigure();
bld.EndGeometry();
}

bld.EndGeometry();
return bld.ConstructedGeometry;
}

public static class ExtensionMethods
{
/// <summary>
/// Joins many geometries into one
/// </summary>
/// <param name="geometries">geometries to join</param>
/// <returns>composite geometry</returns>
public static SqlGeometry UnionAll(this IEnumerable<SqlGeometry> geometries)
{
var compositeGeometry = geometries.First();
foreach (var g in geometries.Skip(1))
{
compositeGeometry = compositeGeometry.STUnion(g);
}
return compositeGeometry;
}
}

关于c# - 如何加快经纬度到 SqlGeometry 的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27185487/

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