I have this code, it takes the geometry of countries and also a set of points, and then it only returns the points within those countries :
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;
}
Current its quite slow, there are about 4000 points, with double lat/long values, the conversion from that into a SqlGeometry is slow (takes about 25 seconds -- I need this to be perhaps down to a second or two):
var s = new SqlChars(new SqlString(string.Format("POINT({0} {1})", p.Longitude, p.Latitude)));
var pGeo = SqlGeometry.STPointFromText(s, SPAT_REF_ID);
This is only done because the SqlGeometry.Point takes x,y instead of lat,long ... any tips on how can I speed this up?
I already know the SqlGeometry (c.Polygon)
could be reduced to speed up things, however I am unable to control that. What I am after is a way to speed up the conversion from lat/long to SqlGeometry point.
This is the solution I came up with in the end, it does the whole thing in half a second :