简体   繁体   English

Mysql数据库设计 - 存储单个和多个lat长

[英]Mysql Database design - Storing single and multi lat longs

I have a mysql database table that will store locations for buildings and events and a few other things. 我有一个mysql数据库表,它将存储建筑物和事件的位置以及其他一些东西。 All locations are stored in one table, and linked to buildings, events etc through their own many to many table. 所有位置都存储在一个表中,并通过他们自己的多个表链接到建筑物,事件等。 That way I can just display dots on a map, and also allow filtering etc. 这样我就可以在地图上显示点,也可以过滤等。

However the problem comes with some things having a single location so 1 lat,long but some like a track has a number of lat long positions, and something like a large stadium might have a polygon over it. 然而问题来自于一些具有单个位置的东西,所以1纬度,长但有些像轨道具有多个纬线长位置,并且像大型体育场的东西可能在其上具有多边形。 These are also stored as a list of lat,longs with the first and last being the same. 这些也存储为lat,long的列表,第一个和最后一个是相同的。

Im wondering how I should store this in the mysql db though. 我想知道我应该如何将它存储在mysql数据库中。 Originally I just had a column for lat, long and id for the lookup table. 最初我只有一个lat,long和id的列用于查找表。 Should I have ANOTHER lookup table for the co-ordinates or serialise the data before putting it into the DB in some way or should I just store the whole string in one field lat1,long1 lat1,long1;lat2,long2;lat1,long1 我是否应该为坐标设置另一个查找表,或者在将数据以某种方式放入数据库之前将其序列化,或者我应该将整个字符串存储在一个字段lat1,long1 lat1,long1; lat2,long2; lat1,long1中

Any suggestions? 有什么建议?

I wouldn't de-normalize the data from the start, by pushing a whole "serialized" polygon into a single field. 通过将整个“序列化”多边形推入单个字段,我不会从一开始就对数据进行去规范化。

Rather, I'd have a Polygons table (with polygon ID and possibly auxiliary per-polygon info, such as whether it's an actual closed polygon or just a polyline -- though that might alternatively be represented in the following table by having the last point equal to the first one for a certain polygon), and a PointsInPolygon table (with coordinates of the point, polygon ID foreign key, vertex number within polygon -- the latter two jointly being unique). 相反,我有一个多边形表(带有多边形ID和可能的辅助每个多边形信息,例如它是一个实际的闭合多边形还是只是一个折线 - 尽管可能也可以通过最后一个点在下表中表示等于某个多边形的第一个)和PointsInPolygon表(具有点的坐标,多边形ID外键,多边形内的顶点数 - 后两者共同唯一)。

Normalization will make (as usual) your life much simpler for ad-hoc queries (including in this case "polygons near X", point in polygon, etc). 标准化将使(如常)您的生活更加简单,即使是临时查询(在本例中包括“X附近的多边形”,多边形点等)。 Again as usual, you can later add redundant denormalized values if and when you determine that some specific query really needs to get optimized (at some cost to table updates, integrity checks, etc). 与往常一样,如果确定某些特定查询确实需要优化(以表更新,完整性检查等为代价),您可以稍后添加冗余非规范化值。 Geodata are not all that different from other kinds in this regard. 在这方面,地理数据与其他类型没有什么不同。

Since you're not doing any lookups on the locations, and you're using (I'm assuming) Google Maps API, the simplest solution would probably be to encode a list of lat/lon as JSON and store in a varchar column. 由于您没有对位置进行任何查找,并且您正在使用(我假设)Google Maps API,因此最简单的解决方案可能是将lat / lon列表编码为JSON并存储在varchar列中。

You can just output the JSON straight from the database for your Google Maps API code to use. 您可以直接从数据库输出JSON,以供您使用的Google Maps API代码。 I would suggest you to use some simple JSON structure like so: ["point",1.23456,2.34567] or ["line",1.23456,2.34567,3.45678,4.56789] and so on. 我建议你使用一些简单的JSON结构,如: ["point",1.23456,2.34567]["line",1.23456,2.34567,3.45678,4.56789]等等。

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

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