I have been working a lot of with PHP and GIS consulting for CitySquares and the History Engine. I found searching for everything I needed to do basic processing & Google Integration tedious and painful. So here is a collection of common functions that helped me get through the massaging of the data and ready for integration.
- pnPoly - Used to determine if a coordinate falls inside a polygon.
Centroid - Find the center of a polygon..
Area - Calculate the area of a polygon.
googleGeoCoder - Extracts GIS information from Google Maps from an address.
PolylineEncoder - Takes a set of coordinates and encodes it for Google Maps.
If you ran into the problem I did, which is that a lot of the data is coming in the form of shp/dbf files and needs to be parsed out to something friendlier either KML or CSV, there are a couple of solutions for that. You can parse out the data with shp2text if your source coordinate format is already in lat/lng or if you have different coordinate system and use ArcGIS, you can try the plugin Export to KML 2.5.3 to help with the exporting of data with the ESRI suite of products.
Once your data is in SQL, the following query is an example of distance sorting with SQL. You can grab a copy of the zip_codes database here and play around with it.
SELECT *,
sqrt((69.1 * ("37.6" - latitude)) * (69.1 * ("37.6" - latitude)) +
(53.0 * ("-77.6" - longitude)) * (53.0 * ("-77.6" - longitude)))
AS distance
FROM `zip_codes`
HAVING distance < 10
ORDER BY distance ASC
Good. But two problems:
1) you don’t seem to consider the curvature of the earth’s surface in to consideration.
2) in some cases, the centroid calculation is erroneous compared to a normal mean of the lat longs calculated separately.
Or am I missing something completely?
1) I’m using a flat-earth model for performance and simplicity in the example above, which for the majority of calculations will suffice. If you need to do long distance calculations, you will need to switch over great-circle distance calculation.
2) Can you give me the example of this you are working with so I can try to debug the problem.
1) True. I agree. My latlongs are very close http://txtb.in/djf
results: your centroid:
12.982224693774, 77.624733038687
mean centroid:
12.982286543056, 77.624984548135
I haven’t tested this manually yet. But I am supposed to get a latlong a bit northeast.
I didn’t get enough time to test it with other latlong sets. Will do it.
Hey there, thanks for the tools. I just have one question: can you provide a simple doc for your pnpoly() function, at least expected input data formats?
$poly[] = array(’0′,’5′);
$poly[] = array(’0′,’0′);
$poly[] = array(’5′,’0′);
$poly[] = array(’5′,’5′);
if(pnpoly($poly,’1′,’1′))
echo “1,1 is inside the polygon\n”;
if(!pnpoly($poly,’1′,’6′))
echo “1,6 is not inside the polygon\n”;