06年10月16日
需求:公司现在gis系统有大量的经纬度坐标,都是车辆行驶几个月采集过来的坐标。现在在做区域搜索功能(就是在上个月某个时间内车辆在哪里行驶)。因为现在数据库单个车辆的经纬度坐标太多,所以搜索起来非常慢(让人忍受不了的速度),随着时间的推移,可能搜索起来会越来越慢,所以公司决定考虑使用mysql的空间数据库特性。把经纬度坐标都转移到成空间数据库坐标.
公司使用mysql4.1.2,
参考资料:
官方中文资料: http://dev.mysql.com/doc/refman/5.1/zh/spatial-extensions-in-mysql.html#gis-class-polygon
说明:这是本人学习mysql的空间数据库特性时候解决的一些问题和和对一些问题的看法,肯定有错误的地方,希望看到这篇文章的朋友能够多多指教,给我留言。
06年10月18日 4 如何判断已有的经纬度点在某一区域内?
在mysal4.1官方的文档找到下列函数,应该就是解决问题4的
Relations on Geometry Minimal Bounding Rectangles (MBRs) MySQL provides several functions that test relations between minimal bounding rectangles of two geometries g1 and g2. The return values 1 and 0 indicate true and false, respectively. Returns 1 or 0 to indicate whether the Minimum Bounding Rectangle of g1 contains the Minimum Bounding Rectangle of g2. mysql> SET @g1 = GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))'); mysql> SET @g2 = GeomFromText('Point(1 1)'); mysql> SELECT MBRContains(@g1,@g2), MBRContains(@g2,@g1); ----------------------+----------------------+ | MBRContains(@g1,@g2) | MBRContains(@g2,@g1) | +----------------------+----------------------+ | 1 | 0 | +----------------------+----------------------+ Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries g1 and g2 are disjoint (do not intersect). Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries g1 and g2 are the same. Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries g1 and g2 intersect. Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries g1 and g2 overlap. Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries g1 and g2 touch. Returns 1 or 0 to indicate whether the Minimum Bounding Rectangle of g1 is within the Minimum Bounding Rectangle of g2. mysql> SET @g1 = GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))'); mysql> SET @g2 = GeomFromText('Polygon((0 0,0 5,5 5,5 0,0 0))'); mysql> SELECT MBRWithin(@g1,@g2), MBRWithin(@g2,@g1); +--------------------+--------------------+ | MBRWithin(@g1,@g2) | MBRWithin(@g2,@g1) | +--------------------+--------------------+ | 1 | 0 | +--------------------+--------------------+
现在我需要的功能是查找一辆车在某一段时间内是否在一段区域内经过,用点来说明的话,就是一个空间坐标点在一个特定时间段内是否包含在一个特定的矩形区域内。下面这个函数应该能达到这个功能:
Returns 1 or 0 to indicate whether the Minimum Bounding Rectangle of g1 is within the Minimum Bounding Rectangle of g2.
晕,突然发现官方有中文的文档,不用看洋文了!!! http://dev.mysql.com/doc/refman/5.1/zh/spatial-extensions-in-mysql.html#gis-class-polygon
下面是文档中解决问题4的方法。
在geom有6个点,现在构造一个Polygon((2.1 2.1,2.1 3.1,2.1 3.1,3.1 3.1)),判断是否有点包含在Polygon里,下面是实现过程。 按照判断应该有Point(3 3)应该包含在Polygon内。但是实际情况确实空集。出什么问题了呢?见下图。
查看Polygon的定义,查找原因。
YES!Polygon的外部边界(即第一参数)应该是封闭的。而我在上面构造的矩形只是我莫须有想出来的,不符合Polygon的规范 下面再实验下。
06年10月18日(晚)
我需要解决的问题中只需要矩形这个几何体,而在OpenGIS的Geometry Model的二维矩形表示用不带孔的Polygon就行了。Polygon是二维对象Surface的子类。
上图中的Polygon含有一个孔. 如果要构造一个矩形,只要构造一个没有孔的封闭四边形的Polygon。
带一个孔的Polygon的表示
看到上面的Polygon((0 0,0 3,3 0,0 0),(1 1,1 2,2 1,1 1))’;(Polygon第一个参数是外部边界,第二个或者[第三个,第四个]都是Polygon里面的孔) 0 0,0 3,3 0,0 0围成的图形是是一个三角形,中间有一个孔(相当于被挖空的一块),也是三角形(1 1,1 2,2 1,1 1))(在纸上画下就知道了) 通过面积也可以知道孔是被挖掉的(3*3/2-1*1/2=4); 我现在要构造一个不带孔的矩形
那现在要构造一个矩形就简单的多了
呵呵算一下面积正好是矩形的面积。那跟预想的一样。
现在矩形也有了,点也有了,我们用mysql提供的函数来实际解决下问题4吧。 (具体的函数见mysql4.1文档的空间数据库特性那节,现在mysql4.1只提供MySQL提供了一些可测试两个几何对象g1和g2最小边界矩形之间关系的函数。 OpenGIS规范定义了下述函数。目前在MySQL尚未按照规范实施它们。对于那些已实施的函数,它们返回的结果与对应的基于MBR的函数返回的相同。包括下面列出的函数,但Distance()和Related()除外。 在未来的版本中,可能会实施这些函数,为空间分析提供全部支持,而不仅仅是基于MBR的支持。)
我们用到的函数有2个: 返回1或0以指明g1的最小边界矩形是否包含g2的最小边界矩形。 返回1或0以指明g1的最小边界矩形是否位于g2的最小边界矩形内。
现在表中有6个点
我们来构造一个矩形。
现在来用MBRContains(g1,g2)测试下这个矩形包含了哪些点
返回了,和预想的一样:P 再来测试下MBRWithin(g1,g2)函数
呵呵,OK! 看上面的两张图也可以看出MBRContains和MBRWithin的区别了 两个函数的区别在于参数, 只要把 (第一个参数 + 函数名 + 第二个参数) 读出来读的通就行了,很好记忆。这不也正是SQL的优点吗~~~ 至此,第四个问题也解决了! |