MySQL地理數據庫是一個專門用于管理和查詢空間和地理位置數據的工具。單獨的MySQL數據庫或在其他關系型數據庫中添加可擴展的空間和地理位置功能。在MySQL地理數據庫中,可以使用空間索引來快速執行空間查詢。以下是使用MySQL地理數據庫查詢的一些基礎知識:
CREATE TABLE City ( name varchar(50) NOT NULL, location POINT NOT NULL, PRIMARY KEY (name), SPATIAL INDEX (location) ); INSERT INTO City (name, location) VALUES ('New York', POINT(-73.935242,40.730610)); INSERT INTO City (name, location) VALUES ('Los Angeles', POINT(-118.243683,34.052235)); INSERT INTO City (name, location) VALUES ('Chicago', POINT(-87.623177,41.881832)); INSERT INTO City (name, location) VALUES ('Houston', POINT(-95.369803,29.760427)); INSERT INTO City (name, location) VALUES ('Philadelphia', POINT(-75.163789,39.952583)); INSERT INTO City (name, location) VALUES ('Phoenix', POINT(-112.074037,33.448376)); INSERT INTO City (name, location) VALUES ('San Antonio', POINT(-98.493628,29.424120)); INSERT INTO City (name, location) VALUES ('San Diego', POINT(-117.161087,32.715736)); INSERT INTO City (name, location) VALUES ('Dallas', POINT(-96.797072,32.776665)); INSERT INTO City (name, location) VALUES ('San Jose', POINT(-121.886330,37.338207));
如上所示,可以創建一個包含城市名稱和城市位置的表。在每個記錄中,點用于表示城市的經度和緯度。 SPATIAL INDEX (location)用于為表中的位置字段創建空間索引。這里是一些定位的查詢示例:
-- Find all cities within 100 kilometers of New York City SELECT name, ST_DISTANCE_SPHERE(location, POINT(-73.935242,40.730610))/1000.00 AS distance_km FROM City WHERE ST_DISTANCE_SPHERE(location, POINT(-73.935242,40.730610))/1000.00<= 100 ORDER BY distance_km; -- Find all cities within the bounding box defined by the coordinates BOTTOMLEFT(-180,-90) and UPPERRIGHT(-100,90). SELECT name FROM City WHERE MBRContains(GeomFromText('LINESTRING(-180 -90,-100 90)'), location);
以上示例僅僅展示了如何使用MySQL地理數據庫。該工具提供了很多其他的功能,如判斷多邊形是否相交,計算兩個多邊形之間的距離等。它也支持多種格式的地圖數據。通過使用MySQL地理數據庫,您可以很容易地實現各種空間查詢和地理位置應用程序。
上一篇用css編寫簡單網頁代碼
下一篇用css編寫花店demo