-1

I have got polygon data for some neighborhoods in New York city from maponics and imported into a MySQL database. I have a requirement to find out the corresponding neighborhood from the polygon for a given lat/lon.

Is there anyway to achieve this in PHP?

Marc Baumbach
  • 10,323
  • 2
  • 30
  • 45
  • can u show the sample data that you have got – Nouphal.M Jan 10 '14 at 05:35
  • starting with mysql 4.1 you could harness spatial extensions to tackle this problem. doing it in php would most probably require you to iterate over a lot of points. – Roman Pickl Jan 10 '14 at 06:18
  • see http://stackoverflow.com/questions/5065039/find-point-in-polygon-php – Roman Pickl Jan 10 '14 at 06:26
  • polygon sample i have is "POLYGON((-73.84544499559114 40.87018417375253,-73.84490248511872 40.87077776510804,-73.84378250165469 40.87221441621899,-73.84307159895656))" sorry due to character limitation not able to paste full row. – Kumar Subramanian Jan 10 '14 at 09:18

1 Answers1

1

MySQL version 5.6 added ST_CONTAINS() function which will do a true spatial search if a point is within a polygon. You will need to do the following:

1) ST_CONTAINS( polygon, point ) : the first parameter should be the column name in the table containing your polygon.

2) The second parameter is a POINT datatype constructed using the spatial function GeomFromText()

3) The MySQL query below will return the row (neighborhood) which contains the lat/lng point.

SELECT * FROM your_table where ST_Contains( your_polygon_column,  GeomFromText( 'POINT( your_lat, your_lon )' ) );
Andrew - OpenGeoCode
  • 2,299
  • 18
  • 15