是否有任何 StackExchange 更快的方法来验证该点是否在使用 Redis 的多边形中。
我有一个纬度和经度点,想知道它落在哪个城市。我有城市的多边形数据。
目前我正在使用此代码并且它可以工作,但是当我必须在数百个城市多边形中进行检查时,我发现它效率太低且不可扩展。想用 Redis 来做这件事。
请协助。
City1.Add(new GeoPoints(5.9581, 1.11865));
City1.Add(new GeoPoints(4.99572, -1.47411));
City1.Add(new GeoPoints(6.19298, -2.00145));
City1.Add(new GeoPoints(6.98422, 0.38257));
City1.Add(new GeoPoints(5.9581, 1.11865));
City2.Add(new GeoPoints(-6.4461, 39.11052));
City2.Add(new GeoPoints(-7.19336, 39.68732));
City2.Add(new GeoPoints(-7.23151, 38.98969));
City2.Add(new GeoPoints(-7.00528, 38.55296));
City2.Add(new GeoPoints(-6.4461, 39.11052));
public bool IsPointInPolygon(List<GeoPoints> poly, GeoPoints point)
{
int i, j;
bool c = false;
for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
{
if ((((poly[i].Lt <= point.Lt) && (point.Lt < poly[j].Lt))
|| ((poly[j].Lt <= point.Lt) && (point.Lt < poly[i].Lt)))
&& (point.Lg < (poly[j].Lg - poly[i].Lg) * (point.Lt - poly[i].Lt)
/ (poly[j].Lt - poly[i].Lt) + poly[i].Lg))
c = !c;
}
return c;
}