SQLite Forum

Country-coding from Lat/Long, using GeoPoly and/or R-Tree
Login
I implemented something similar for our traffic notification app. We were working on complex shapes which have convex and concave sides, do not intersect and do not have holes in them.

We did it using a combination of techniques.

1. For each area we create an outer rectangle held in an R-tree to allow a quick check to see if any point matches. This could have overlapping rectangles.

2. For each group of rectangles matched, we then went and checked against an outer hull, this is the area that is a polygon but has no concave faces.

3. If we then got a match here we then checked against it being inside the proper polygon as described by Gunter earlier.

We did try to go straight to point 3, but we found that the two earlier checks made things faster as most points are NOT in an area of interest. Your use case would probably be best started at point 2 if you know that the point is always in a country and not over a sea. 

Defining the boundaries of a country could be rather complex as borders often follow weird lines as rivers or mountain ranges or roads. Also countries borders do change. How low a level of detail do you want? If you are relying on GPS then the accuracy is variable, sometimes 100m, sometimes 5m. 

We could not implement all of this in SQLite so used Perl (yes I'm old school) and JavaScript on the client. 

Rob