Reputation: 21
I wish to determine the US state and county in which a given geographic point (GPS coords known) falls. I believe that I can obtain data as to the coordinates of the corners of states and counties but I then need an algorithm to calculate in which state and county the point falls.
I am not a Java or C user but I am a very experienced programmer in Panorama (provue.com), a database management system with a very comprehensive and powerful programming language. So I need access to a generically-defined algorithm.
michael
Upvotes: 2
Views: 110
Reputation: 4821
First you should have a set of polygons, each one corresponding to a US state like in http://www.nws.noaa.gov/geodata/catalog/national/html/us_state.htm
Then you can use the following algorithm given in http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html#The C Code
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
nvert: Number of vertices in the polygon. Whether to repeat the first vertex at the end is discussed below.
vertx, verty: Arrays containing the x- and y-coordinates of the polygon's vertices.
testx, testy: X- and y-coordinate of the test point.
Upvotes: 1
Reputation: 53535
Quick & dirty:
you can use this project in the following way as HTTP request:
http://www.ngs.noaa.gov/cgi-bin/spc_getpc.prl?LatBox=N385930.99999&LonBox=W0985930.99999
and parse the response to extract the state.
Upvotes: 0