SQL判断经纬度在矩形内

1,将城市地图拆分等距拆分为矩形

数据结构如图:

 

2.查看高德JS API (点是否在多边形内)核心代码:

a=[114.069564,22.545774];
b=[
    [114.067595,22.552082],
    [114.077322,22.552082],
    [114.077322,22.543099],
    [114.067595,22.543099],
];
c=true

lf: function(a, b, c) {
    var d = a[0];
    a = a[1];
    var f = !1, g, h, k, l, m = b.length, n = 0;
    for (l = m - 1; n < m; l = n,
    n += 1) {
        var p = !1;
        g = b[n][0];
        h = b[n][1];
        k = b[l][0];
        l = b[l][1];
        if (g === d && h === a || k === d && l === a)
            return c ? !0 : !1;
        if (h < a === l >= a) {
            g = (k - g) * (a - h) / (l - h) + g;
            if (d === g)
                return c ? !0 : !1;
            p = d < g
        }
        p && (f = !f)
    }
    return f
}

3.将js代码解析为SQL (未考虑使用函数,因在其他平台上使用)

SELECT * FROM map_grid a
WHERE 
(CASE 
    WHEN 
				(a.first_longitude = 114.069564 and a.first_latitude=22.545774) or 
				(a.second_longitude = 114.069564 and a.second_latitude=22.545774)  or 
				(a.third_longitude = 114.069564 and a.third_latitude=22.545774)  or 
				(a.fourth_longitude = 114.069564 and a.fourth_latitude=22.545774) 
		THEN TRUE

		WHEN (a.first_latitude<22.545774)=(a.fourth_latitude>=22.545774) 
				AND (a.fourth_longitude-a.first_longitude)*(22.545774-a.first_latitude)/(a.fourth_latitude-a.first_latitude)+ a.first_longitude = 114.069564
		THEN TRUE

		WHEN (a.second_latitude<22.545774)=(a.first_latitude>=22.545774) 
				AND (a.first_longitude-a.second_longitude)*(22.545774-a.second_latitude)/(a.first_latitude-a.second_latitude)+ a.second_longitude = 114.069564
		THEN TRUE

		WHEN (a.third_latitude<22.545774)=(a.second_latitude>=22.545774) 
				AND (a.second_longitude-a.third_longitude)*(22.545774-a.third_latitude)/(a.second_latitude-a.third_latitude)+ a.third_longitude = 114.069564
		THEN TRUE

		WHEN (a.fourth_latitude<22.545774)=(a.third_latitude>=22.545774) 
				AND (a.third_longitude-a.fourth_longitude)*(22.545774-a.fourth_latitude)/(a.third_latitude-a.fourth_latitude)+ a.fourth_longitude = 114.069564
		THEN TRUE

		WHEN(
					(
						CASE
						WHEN (a.first_latitude<22.545774)=(a.fourth_latitude>=22.545774) 
								AND (a.fourth_longitude-a.first_longitude)*(22.545774-a.first_latitude)/(a.fourth_latitude-a.first_latitude)+ a.first_longitude > 114.069564
						THEN 1
						ELSE 0 END
					)+(
						CASE
						WHEN (a.second_latitude<22.545774)=(a.first_latitude>=22.545774) 
								AND (a.first_longitude-a.second_longitude)*(22.545774-a.second_latitude)/(a.first_latitude-a.second_latitude)+ a.second_longitude > 114.069564
						THEN 1
						ELSE 0 END
					)+(
						CASE
						WHEN (a.third_latitude<22.545774)=(a.second_latitude>=22.545774) 
								AND (a.second_longitude-a.third_longitude)*(22.545774-a.third_latitude)/(a.second_latitude-a.third_latitude)+ a.third_longitude > 114.069564
						THEN 1
						ELSE 0 END
					)+(
						CASE
						WHEN (a.fourth_latitude<22.545774)=(a.third_latitude>=22.545774) 
								AND (a.third_longitude-a.fourth_longitude)*(22.545774-a.fourth_latitude)/(a.third_latitude-a.fourth_latitude)+ a.fourth_longitude > 114.069564
						THEN 1
						ELSE 0 END
					) 
				) % 2=1
			THEN TRUE

			ELSE FALSE
			END
)

4.执行结果

posted @ 2017-03-04 20:42  stone_wl  阅读(2354)  评论(0编辑  收藏  举报