SQL判斷經緯度在矩形內

2022-02-17 02:24:41 字數 3059 閱讀 6020

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)

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.執行結果

sql語句查詢經緯度範圍

指定乙個經緯度,給定乙個範圍值 單位 千公尺 查出在經緯度周圍這個範圍內的資料。經度 113.914619 緯度 22.50128 範圍 2km longitude為資料表經度字段 latitude為資料表緯度字段 sql在mysql下測試通過,其他資料庫可能需要修改 sql語句如下 select ...

sql語句查詢經緯度範圍

指定乙個經緯度,給定乙個範圍值 單位 千公尺 查出在經緯度周圍這個範圍內的資料。經度 113.914619 緯度 22.50128 範圍 2km longitude為資料表經度字段 latitude為資料表緯度字段 sql在mysql下測試通過,其他資料庫可能需要修改 sql語句如下 select ...

sql語句查詢經緯度範圍

指定乙個經緯度,給定乙個範圍值 單位 千公尺 查出在經緯度周圍這個範圍內的資料。經度 113.914619 緯度 22.50128 範圍 2km longitude為資料表經度字段 latitude為資料表緯度字段 sql在mysql下測試通過,其他資料庫可能需要修改 sql語句如下 select ...