source

Storing Lat Lng values in MySQL using Spatial Point Type

nicesource 2023. 9. 21. 20:34
반응형

Storing Lat Lng values in MySQL using Spatial Point Type

Tech used: MySQL 5.1 and PHP 5.3

I am just designing a new database for a site I am writing. I am looking at the best way of now storing Lat and Lng values.

In the past I have been using DECIMAL and using a PHP/MySQL select in the form:

SQRT(POW(69.1 * (fld_lat - ( $lat )), 2) + POW(69.1 * (($lon) - fld_lon) * COS(fld_lat / 57.3 ), 2 )) AS distance

to find nearest matching places.

Starting to read up more on new technologies I am wondering if I should use Spatial Extensions. http://dev.mysql.com/doc/refman/5.1/en/geometry-property-functions.html

Information is quite thin on the ground though and had a question on how to store the data. Instead of using DECIMAL, would I now use POINT as a Datatype?

Also, once stored as a POINT is it easy just to get the Lat Lng values from it in case I want to plot it on a map or should I additionally store the lat lngs as DECIMALS again as well?

I know I should prob use PostGIS as most posts on here say I just don't want to learn a new DB though!

Follow up

I have been playing with the new POINT type. I have been able to add Lat Lng values using the following:

INSERT INTO spatialTable (placeName, geoPoint) VALUES( "London School of Economics", GeomFromText( 'POINT(51.514 -0.1167)' ));

I can then get the Lat and Lng values back from the Db using:

SELECT X(geoPoint), Y(geoPoint) FROM spatialTable;

This all looks good, however the calculation for distance is the bit I need to solve. Apparently MySQL has a place-holder for a distance function but won't be released for a while. In a few posts I have found I need to do something like the below, however I think my code is slightly wrong:

SELECT
 placeName,
 ROUND(GLength(
  LineStringFromWKB(
   LineString(
    geoPoint, 
    GeomFromText('POINT(52.5177, -0.0968)')
  )
  )
))
AS distance
FROM spatialTable
ORDER BY distance ASC;

In this example geoPoint is a POINT entered into the DB using the INSERT above.

GeomFromText('POINT(52.5177, -0.0968)'는 거리를 계산하고자 하는 Lat Lng 값입니다.

More Follow-up

Rather stupidly I had just put in the ROUND part of the SQL without really thinking. Taking this out gives me:

SELECT
placeName,
(GLength(
LineStringFromWKB(
  LineString(
    geoPoint, 
    GeomFromText('POINT(51.5177 -0.0968)')
  )
 )
))
AS distance
FROM spatialTable
ORDER BY distance ASC

Which seems to give me the correct distances I need.

I suppose the only thing currently that needs answering is any thoughts on whether I am just making life difficult for myself by using Spatial now or future-proofing myself...

I think you should always use the highest level abstraction easily available. If your data is geospatial, then use geospatial objects.

그러나 조심하라.Mysql은 최악의 지리공간 데이터베이스입니다.점은 괜찮지만 모든 다각형 함수가 완전히 깨져 있습니다. 다각형을 경계 사각형으로 변경한 다음 이에 대한 답을 수행합니다.

최악의 예는 일본을 대표하는 다각형이 있는데 일본에 어떤 곳이 있는지 물어보면 블라디보스토크가 그 목록에 들어간다는 거예요!

오라클 앤 포스트GIS에는 이런 문제가 없습니다.MSSQL과 JTS를 엔진으로 사용하는 자바 데이터베이스는 없을 것으로 예상합니다.지리공간 좋음.MySQL 지리공간 불량.

여기서 읽어보세요 MySQL 공간 쿼리를 사용하여 X 반경의 모든 레코드를 찾는 방법은 무엇입니까?5.6.1에 고정되어 있다는 것.

호라!

Mysql GIS yagni:

GIS에 대한 경험이 없다면, 공간 확장을 배우는 것은 사실상 새로운 데이터베이스를 배우는 것과 같고, 약간의 수학과 많은 두문자어를 배우는 것과 같습니다.지도, 프로젝션, 스리드, 포맷...일정한 lat/long이 주어진 점 사이의 거리를 계산하려면 이 모든 것을 배워야 합니까? 아마도 그렇지 않을 것입니다. 타사 GIS 데이터를 통합하거나 점보다 더 복잡한 작업을 수행할 것입니까? 어떤 좌표계를 사용할 것입니까?

야그니로 돌아가기: 가능한 한 간단한 일을 하고, 이 경우 코드를 php 또는 간단한 SQL로 구현합니다.장벽에 도달하여 공간이 필요하다고 판단되면 GIS 시스템을 읽고 시스템, 프로젝트 및 규약을 조정합니다.

그 때쯤이면 당신은 아마도 포스트를 원할 것입니다.GIS.

그러면 쿼리에 공간 인덱스를 사용할 수 있기 때문에 이는 좋은 일입니다.경계 상자로 제한합니다. 예를 들어 비교할 행 수를 제한합니다.

백엔드에 추가 코드를 넣을 수 있다면 Geohash를 사용하십시오.

접두사가 더 넓은 영역을 나타내는 방식으로 좌표를 문자열로 인코딩합니다.끈이 길수록 정밀도가 높아집니다.

그리고 다양한 언어에 대한 바인딩을 가지고 있습니다.

http://en.wikipedia.org/wiki/Geohash https://www.elastic.co/guide/en/elasticsearch/guide/current/geohashes.html

언급URL : https://stackoverflow.com/questions/4995428/storing-lat-lng-values-in-mysql-using-spatial-point-type

반응형