简体   繁体   English

地理空间数据的表结构

[英]Table structure for Geo Spatial Data

What is the suggested way to structure a table in a MYSQL Database containing Geo Spatial data. 在包含Geo Spatial数据的MYSQL数据库中构造表的建议方法是什么。 As a POC i am working to insert the data as in the image as below into a database table. 作为POC,我正在努力将数据插入数据库表中,如下图所示。

数据库

This below is the snapshot of the table creation of Phpmyadmin table creation 以下是Phpmyadmin表创建表创建的快照

phpmyadmintable

looking for suggestions to create table for geospatial data as in screenshots. 寻找建立地理空间数据表的建议,如截图所示。

EDIT: I am working on XAMPP V1.8.3 Windows8 running MYSQL version 5.6.16. 编辑:我正在使用运行MYSQL版本5.6.16的XAMPP V1.8.3 Windows8。 Created Table geomduplicate and columns and inserted data as in screenshot with the below sql 使用下面的sql创建表格geomduplicate和列以及插入数据

CREATE TABLE geomduplicate1(
zip INTEGER(3) NOT NULL PRIMARY KEY, 
latitude NUMERIC(9,6), 
longitude NUMERIC(10,6),
city VARCHAR(10),
state VARCHAR(2),
county VARCHAR(9)
);
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(501,40.922326,-72.637078,'Holtsville','NY','Suffolk');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(544,40.922326,-72.637078,'Holtsville','NY','Suffolk');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(601,18.165273,-66.722583,'Adjuntas','PR','Adjuntas');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(602,18.393103,-67.180953,'Aguada','PR','Aguada');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(603,18.455913,-67.14578,'Aguadilla','PR','Aguadilla');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(604,18.49352,-67.135883,'Aguadilla','PR','Aguadilla');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(605,18.465162,-67.141486,'Aguadilla','PR','Aguadilla');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(606,18.172947,-66.944111,'Maricao','PR','Maricao');
REPLACE INTO geomduplicate1(zip,latitude,longitude,city,state,county) VALUES
(610,18.288685,-67.139696,'Anasco','PR','Anasco');

and the data was successfully inserted in mysql database. 并且数据已成功插入mysql数据库中。

Store it is as a geometry data type . 将其存储为几何数据类型 MySQL supports Geometry (generic), as well as Point, Linestring and Polygon data types, see creating spatial data types . MySQL支持Geometry(通用),Point,Linestring和Polygon数据类型,请参阅创建空间数据类型 A single longitude or latitude value can not be a geometry on its own, as you have it in your screen shot. 单个经度或纬度值本身不能是几何体,就像在屏幕截图中一样。

If you go the route of using geometry types, it gives you two advantages over having separate latitude and longitude fields: you can add a spatial index and you will be able to use some of MySQL's spatial operator functions such as ST_Buffer, ST_Intersects, ST_Distance to do further analysis. 如果你使用几何类型的路线,它比拥有单独的纬度和经度字段有两个优点:你可以添加一个空间索引 ,你将能够使用一些MySQL的空间操作符函数 ,如ST_Buffer,ST_Intersects,ST_Distance to做进一步的分析。 Spatial indexes are based on R-trees and will perform far better than two B-tree indexes on non spatial columns, latitude and longitude -- and this performance difference will grow as your table size grows. 空间索引基于R树,并且在非空间列,纬度和经度上的性能远远优于两个B树索引 - 并且这种性能差异将随着表大小的增长而增长。

You can still get the latitude and longitude values back by using the X and Y point functions so you will not lose anything by storing your data as a Point. 您仍然可以通过使用X和Y点函数来获取经度和纬度值,这样您就不会因为将数据存储为Point而丢失任何内容。

If you already have your data in two separate lat/lon columns, and you want to go the geometry/point datatype route, you can use the Point function to create the Point datatype: 如果已将数据放在两个单独的lat / lon列中,并且想要使用geometry / point数据类型路由,则可以使用Point函数创建Point数据类型:

alter table mytable add column pt POINT;
update mytable set pt=Point(longitude, latitude);
alter table mytable modify pt POINT NOT NULL;
create spatial index ix_spatial_mytable_pt ON mytable(pt);

Note that the Point function was only introduced in MySQL 5.1.x (it isn't too well documented, so I'm not sure of exact version), and before that you had to use concat with the GeomFromText function, see Moving lat/lon text columns into a 'point' type column for some more information on this, although note that Quassnoi's answer has lon and lat the wrong way round -- it is Point(lon, lat) not the other way, though this is a very common mistake. 请注意,Point函数仅在MySQL 5.1.x中引入(它没有太多文档,所以我不确定确切的版本),在此之前你必须使用concat与GeomFromText函数,请参阅移动lat / lon文本列进入'点'类型列有关此的更多信息,虽然注意到Quassnoi的答案有lon和lat错误的方式回合 - 它是Point(lon,lat)而不是其他方式,虽然这是一个非常常见的错误。

NOTE: Until recently, you could only index a spatial column if using the MyISAM engine. 注意:直到最近,如果使用MyISAM引擎,您只能索引空间列。

EDIT: In the upcoming release, MySQL 5.7.5 , InnoDB will finally support indexes on spatial data types (and not just store spatial types without an index, which is considerably less useful). 编辑:在即将发布的MySQL 5.7.5版本中 ,InnoDB最终将支持空间数据类型的索引(而不仅仅是在没有索引的情况下存储空间类型,这种方法相当不太有用)。 This means you can have foreign keys, ACID guarantees, spatial indexes all in one engine, which has been a long time in coming. 这意味着您可以在一个引擎中拥有外键,ACID保证,空间索引,这已经很长时间了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM