Question

If I have a column called Poly of type polygon in MySQL and I want to get the NW corner and the NE corner and the SE corner and the SW corner, how would I do that? From an Envelope() there should be lat1, lat2, lon1, and lon2 that form the four corners as follows lat1,lon1 is NW; lat1,lon2 is NE; lat2,lon2 is SE; and lat2,lon1 is SW. When I try X(PointN(Envelope(Poly),1)) AS lat1 it always returns NULL. Can this be done in MySQL?

SELECT
    X(PointN(Envelope(Poly),1)) AS lat1, X(PointN(Envelope(Poly),3)) AS lat2,
    Y(PointN(Envelope(Poly),1)) AS lon1, Y(PointN(Envelope(Poly),2)) AS lon2
FROM boundaries.mt_us_zip5_2013_boundaries_polys_bin
WHERE zip = '00601';

The query above returns:

NULL,NULL,NULL,NULL

Here's what the Envelope looks like:

SELECT AsText(Envelope(Poly))
FROM boundaries.mt_us_zip5_2013_boundaries_polys_bin
WHERE zip = '00601';

This last query returns:

POLYGON((18.111929 -66.836366,18.250344 -66.836366,18.250344 -66.659293,18.111929 -66.659293,18.111929 -66.836366))

I'm using MySQL version 5.5.36, would upgrading to a new version of MySQL give me the functions I need?

Était-ce utile?

La solution

PointN is only defined on LineString, but luckily you can call ExteriorRing on the Polygon to get a LineString. In your example:

SELECT
    X(PointN(ExteriorRing(Envelope(Poly)),1)) AS lat1,
    X(PointN(ExteriorRing(Envelope(Poly)),3)) AS lat2,
    Y(PointN(ExteriorRing(Envelope(Poly)),1)) AS lon1, 
    Y(PointN(ExteriorRing(Envelope(Poly)),2)) AS lon2
FROM boundaries.mt_us_zip5_2013_boundaries_polys_bin
WHERE zip = '00601';

Autres conseils

I'm not familiar enough with MySQL if it has this built-in but my first thought was to first find in the center of the polygon, then compute the bearing at each point on the poly from the center. A bearing of 0 would be north and a bearing of 90 deg would be east, thus north-east would be 45 deg. Find the points closest to the 45 deg angles gives you the corners.

Center of the polygon is here: Computing latitude longitude center point of a Polygon in PHP

Computing bearing from 2 lat/lon points: http://www.movable-type.co.uk/scripts/latlong.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top