Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking Latitude and Longitude?

Status
Not open for further replies.

JasonCannon

IS-IT--Management
Apr 11, 2013
30
0
0
US
I am trying to find the best method in VFP to check a given set of latitude and longitude coordinates.

Starting simple and just want to confirm that a set is in a given US state. And so far I have a table of the northern and southern latitude points and the eastern and western longitude points for each US state.

I can confirm if the given lat and long are or are not within the given state, but if they are not within that state, how can I find out which state they are in?

Table
State : North : South : East : West
VA : 39.4667 : 36.5333 : -75.2500 : -83.6833

Given Lat & Long
VA : 43.9454 : -90.7956

I use below to check if the lat and long are/are not in the given state:
( (lat > table.south AND lat < table.north) AND (long > table.west AND long < table.east) )

I cannot seem to figure out the math to find the state given the lat and long.

Any thoughts, suggestions would be very much appreciated.






"..if you give a man a fish he is hungry again in an hour. If you teach him to catch a fish you do him a good turn."
-- Anne Isabella Thackeray Ritchie.
 
Unless you are assuming all US states are essentially rectangular, this is not going to work.

You need a more granular approach, breaking each state down to squares of the size that match your resolution (4 decimal places of a long/lat).
If you then put those into a table/array against each state, you could do a pretty simple search to locate your position...



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Griff is completely correct. A solution based on comparing a point to boundaries might just about work for Colorado or Utah, but it will fall down in Idaho and completely choke in Maryland.

For a different approach, have you considered using an on-line mapping service or geocoding service, such as Google maps? You could pass it the co-ordinates of the required point, and get it to return the location of that point, which would include the state.

This is completely off the top of my head, and I cannot give you any guidance as to how you would go about it, but I do know that it is technically possible, because there are many apps available that do roughly the same thing.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Just to follow up my previous post ....

Here is a link to a Google service that appears to do what you are looking for:

Look for the information under "reverse geocoding".

If that doesn't meet your needs, try searching for "online geocoding service" or something similar.

It's unlikely that these services will be free, but presumably you are doing this as work- or business-related.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
[pre]sAddress ='1600 Pennsylvania Avenue NW Washington, dc 20500'
xhrRequest= Createobject("Msxml2.ServerXMLHTTP")
sQuery = "xhrRequest.Open ("GET", sQuery, .f.)
xhrRequest.send()
oXML=CREATEOBJECT('msxml.domdocument')
oXML.LoadXml(xhrRequest.responseText)
m.ixnStatus= oXml.selectSingleNode("/GeocodeResponse/status")
do CASE
case m.ixnStatus.text = 'OK'
m.ixnLat= oXML.selectSingleNode("/GeocodeResponse/result/geometry/location/lat")
m.ixnLng = oXml.selectSingleNode("/GeocodeResponse/result/geometry/location/lng")
? 'Latitude= ' +m.ixnLat.text
? 'Longitude= '+m.ixnlng.text
case inlist(m.ixnStatus.text,'ZERO_RESULTS','OVER_QUERY_LIMIT',;
'REQUEST_DENIED','INVALID_REQUEST','UNKNOWN_ERROR')
messagebox("La requête ne peut pas s'exécuter suite à une erreur serveur.",'Erreur')

P.S. Please disregard this post. Google may have moved things around, but the idea is the same.
endcase
[/pre]



If you want to get the best response to a question, please check out FAQ184-2483 first.
 
This seems to work (Get your own Bing Map keyy)

[pre]tcAddress = '1600 Pennsylvania Avenue NW'
tcCity = 'Washington'
TcZip = '20500'
tcCountry = 'USA'
tcState = 'DC'
? GetGps(tcAddress,tcCity,tcState,tcZip,tcCountry)

Procedure getGps(tcAddress,tcCity,tcState,tcZip,tcCountry)
*+++++++++ *** Changed 2015/02/14 new way of getting GPS coordinates, more accurate ++++++++++++++++++++
xhrRequest= Createobject("Msxml2.ServerXMLHTTP")
sQuery = "[tcCountry]&adminDistrict=[tcState]&locality=[tcCity]&postalCode=[tcZip]&addressLine=[tcAddress]&key=Ak3N0GxiMfdssziGDmDvJW3F0Zwo59dop-Q0st6eRNNmCw2Wuvn0RNb3EOg82WlR"
sQuery = Strtran(sQuery,'[tcCountry]',tcCountry)
sQuery = Strtran(sQuery,'[tcState]',tcState)
sQuery = Strtran(sQuery,'[tcCity]',tcCity)
sQuery = Strtran(sQuery,'[tcZip]',tcZip)
sQuery = Strtran(sQuery,'[tcAddress]',tcAddress)
sQuery = Strtran(sQuery,'Ak3N0GxiMfdssziGDmDvJW3F0Zwo59dop-Q0st6eRNNmCw2Wuvn0RNb3EOg82WlR','Ak3N0GxiMfdssziGDmDvJW3F0Zwo59dop-Q0st6eRNNmCw2Wuvn0RNb3EOg82WlR')
xhrRequest.Open ("GET", sQuery, .F.)
xhrRequest.Send()
oXML=Createobject('msxml.domdocument')
oXML.LoadXML(xhrRequest.ResponseText)
lcResponse = xhrRequest.ResponseText
Return lcResponse[/pre]



If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike G,

Yes, that does seem to work. I just tried plugging in my own address; it gave me the correct coordinates.

But I think Jason was asking for the opposite, that is, he wants to give it the coordinates and get back the state. Presumably, that would just be a matter of adjusting the query parameters?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Jason, that particular geo coordinate you presented in your example is in Wisconsin, right?

I just add the reverse geocode feature to the overHere library, a VFP set of class to wrap the access to the Here location platform.

Looking for the state of a specific geo coordinate would be something like:

Code:
* set up the libraries 
DO LOCFILE("here_api.prg")
DO LOCFILE("reverse.prg")

LOCAL OH AS overHere
LOCAL RG AS oh_ReverseGeocode
LOCAL RS AS oh_SearchResponseType

* instantiate a ReverseGeocode resource
m.OH = CREATEOBJECT("overHere")
m.OH.SetCredentials("<your credentials>")

m.RG = m.OH.SetResource("ReverseGeocode")

* set the parameters
m.RG.Proximity.Set(43.9454, -90.7956)
m.RG.Level.Set("state")
m.RG.Mode.Set("retrieveAreas")

* access the resource at the Here platform
IF !ISNULL(m.RG.GetLocation(.T.))

	* get the result
	m.RS = m.RG.Location
	* and show the state code (WI, in this case)
	? "State:", m.RS.Views(1).Results(1).Location.Address.State.Get()

ENDIF
 
First off, thanks to all for the responses. They have all been most helpful.

I got to the point to where I did not think I would be able to use my NEWS table to do a reverse lookup of the Lat and Long, but also know I am not the best at math so just wanted to ask.

The suggestions for the "reverse geocoding" through Google and the overHere library do look most promising. Gotta do some studying!

Again, thank you all for the input!



"..if you give a man a fish he is hungry again in an hour. If you teach him to catch a fish you do him a good turn."
-- Anne Isabella Thackeray Ritchie.
 
I see this already is solved. It's a nice little geometric problem though, in case you could really rely on rectangles.

A point simply is within a rectangle, if two between cheeks are both true, whether it's x (or latitude) is between min/max of the region and whether it's y (or longitude) is between min/max of the region. There's not even much math involved. Two betweens or four comparisons xmin<=x and x<=xmax and ymin<=y and y<=ymax.

The complexity of real geography is too high to approximate this on your own, others already mapped lots of data, and using their services is the only thing you can really afford to do as a single person, unless you want to spend the rest of your life as a cartographer. Think alone how many single geographical GPS positions google measured in their street view project and what map material they have for Google Earth.

Here's still another one I found easy to make use of and well documented:
Of course, Google Maps is a well-known brand name and will stay, and I mean Google maps, not just because Google is a big brand. Ther map search comes next to web searches for me. Dev documentation also is great, I'm just sometimes fed up with change frequencies.

Bye, Olaf.



Olaf Doschke Software Engineering
 
atlopes,

Trying to test the code you supplied. Getting the error "RG is not an object" on this line:

Code:
m.RG.Proximity.Set(43.9454, -90.7956)

Sure I've missed something, but do not know what. I have downloaded the following PRGs to the same folder:
- here_api.prg
- reverse.prg
- geocode.prg

Still need something else?



"..if you give a man a fish he is hungry again in an hour. If you teach him to catch a fish you do him a good turn."
-- Anne Isabella Thackeray Ritchie.
 
atlopes,

I'm willing to download all that is needed, just not sure where it goes.

In the "C:\Program Files (x86)\Microsoft Visual FoxPro 9\" folder?

Or in the folder of my program, X:\VFPPrograms\CheckGeoCodes\?

X:\VFPPrograms\CheckGeoCodes\overHere-master\?
X:\VFPPrograms\CheckGeoCodes\xml-master\?
X:\VFPPrograms\CheckGeoCodes\names-master\?

"..if you give a man a fish he is hungry again in an hour. If you teach him to catch a fish you do him a good turn."
-- Anne Isabella Thackeray Ritchie.
 
Jason,

You can put them in folders of your choice. During the instantiation of the objects, you will be asked for the location of their dependencies if the location is not yet in the VFP path.

That said, I wouldn't put general-purpose libraries, mine or others, under a specific project folder structure. That's what it seems that you're doing with "CheckGeoCodes", but again, it's your choice.
 
Jason,

You might notice that the first two lines of Atlopes' code contains DO LOCFILE. That means that, when you run the program, it will prompt you for the location of the downloaded files. So download them to any convenient folder. Then, when you run the program, just navigate to that folder (twice).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Learn something new everyday!

I have those three project folder in a more central location, instead of directly in this project's folder, and atlopes's code (from this thread) is working for me, now.

atlopes said:
If you don't want to clone or download all the required projects...
What other projects are required other than those three you mentioned?

"..if you give a man a fish he is hungry again in an hour. If you teach him to catch a fish you do him a good turn."
-- Anne Isabella Thackeray Ritchie.
 
Understood. Much thanks, again.

"..if you give a man a fish he is hungry again in an hour. If you teach him to catch a fish you do him a good turn."
-- Anne Isabella Thackeray Ritchie.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top