Get GPS location from coordinates?

Is there anyway that I can get Matlab to tell me a location based on the input of GPS coordinates? So, for example, if I gave it the coordinates (42.3899, -89.0366), how can I get Matlab to tell me that that's Roscoe, Illinois?

4 comentarios

Cedric
Cedric el 29 de Jul. de 2013
Do you have the Mapping Toolbox, and/or ArcGIS? Is it for a large volume of coordinates?
Jacqueline
Jacqueline el 29 de Jul. de 2013
I believe I do have the Mapping Toolbox. It is for a large volume of coordinates
Sakina Jafferi
Sakina Jafferi el 7 de En. de 2019
can u provide me the code for GPS spoofing detection and mitigation
Walter Roberson
Walter Roberson el 7 de En. de 2019
Sakina Jafferi: in theory anyone who is not banned by court order or government regulation from providing you with code, could provide the code you requested . In practice it is unlikely that anyone would bother. If the code is available through search engines then you can do the search since you are the person who needs the results. If the code is not available through search engine then providing the code would take significant research and coding and testing , and it is unlikely that anyone would volunteer to do all that work on behalf of someone who has given no indication that they have put in any effort themselves . The people who have the skills and experience to code that efficiently and who are frequent volunteers are likely to also have the experience to recognize the task as being typical of a topic assigned as a 4th year honours project .

Iniciar sesión para comentar.

 Respuesta aceptada

Cedric
Cedric el 29 de Jul. de 2013
Editada: Cedric el 29 de Jul. de 2013

2 votos

There are several solutions, depending the amount of data that you have to process, the time that you are willing to spend on coding, the time that you can afford spending on processing data, etc. You have two main options (there might be newer tools that I am not aware of though):
1. You can use e.g. a dataset of cities with their coordinates (Shapefile, Excel file, DBF file, etc), and just look for the minimal distance between a point and cities locations. This has the advantage that it is fast and local (no internet), but the disadvantage that you probably won't find a dataset with finer information than significant town/cities. You might also need to buy a license for using "high quality" datasets (I don't know if you could use ESRI datasets without a license for example). EDIT: here are a few relevant links:
  • ESRI Data and Maps (your university/company might have a site license for ArcGIS and DVDs for Data and Maps).
  • Boutell Zip/Lat/Lon Database: here.
  • Another "similar" here.
  • GeoNames, that could be placed in the next category of solutions, but their data is available here as text files.
2. You can use some online service/API and manage sending requests and processing responses (XML, etc). This has the disadvantage that it requires an internet connection and that sending/processing requests/responses can be time consuming, especially if you have a limited access to the API (number/rate of requests). It has the advantage though that you can get much more information, because APIs can give you access to very large databases (e.g. Google). Here are a few relevant links:
FEX (untested, because I usually build my own tools on this matter):
EDIT: to illustrate the issue with local datasets, here is the ESRI Data&Maps 9.3 dataset of 3557 cities. As you can see, if you have locations in e.g. Alaska, Montana, Nevada, chance is that the closest city will be quite far according to this dataset..

8 comentarios

Cedric
Cedric el 29 de Jul. de 2013
Editada: Cedric el 29 de Jul. de 2013
My advice is the following: start with a local solution, e.g. with the Boutell dataset (that you can open with Excel and save as XLSX), and implement the following (which should not be too difficult):
Read XLSX file (or CSV if you prefer).
For each of your locations:
Compute dist to all cities (it is a one shot operation).
If one city at minimal distance:
If distance > 5km (or a relevant threshold that you define):
Flag location as troublesome (too far) => must be treated by hand.
Else
Associate location with city.
Else
Flag location as troublesome (multiple) => must be treated by hand.
Once you have this working, you can test whether there are too many troublesome locations (and consider using an API) or if it is acceptable for a treatment by hand, and think about how to make it more efficient.
Jacqueline
Jacqueline el 29 de Jul. de 2013
I think the best idea would be to use Google Reverse Geocoding. Do you have any idea how I go about doing this? Is their a function somewhere that I can copy? I can't seem to find a place to sign up for it...
Cedric
Cedric el 29 de Jul. de 2013
Editada: Cedric el 29 de Jul. de 2013
I give two links on the Geocoding Google API in my answer (the first is the main page, and the second illustrates the data that you can get in return). I also linked a FEX contribution for using this API (not tested by me though), which might be a basis for developing what you need.
The FAQ for Google Maps API is available here: https://developers.google.com/maps/faq?hl=en
Cedric
Cedric el 29 de Jul. de 2013
.. but wait, I'll build a small example which doesn't require an account, to show you how to perform basic queries .. give me 5 minutes.
Jacqueline
Jacqueline el 29 de Jul. de 2013
The second link (the example) don't go to anything, or maybe it's not supposed to? Sorry, I am new to Matlab and this API stuff so I'm learning along the way!
Cedric
Cedric el 29 de Jul. de 2013
Editada: Cedric el 30 de Jul. de 2013
No problem; first, open the following link in a new tab (with Firefox if you have it):
looking at the link, you recognize the coordinates of your example. Your browser should display an XML document or start the download of this document (which you can then open with a text editor). If you look at the XML data, you will see that there are nested nodes or data structures, defined by tags.. which is the way XML works.
What you have to do in MATLAB is essentially to build the URL based on the latitude and longitude that you are interested in, and then get the response and extract relevant information.
The first part can be achieved with SPRINTF:
lat = 42.3899 ;
lon = -89.0366 ;
url = sprintf('http://maps.googleapis.com/maps/api/geocode/xml?latlng=%.4f,%.4f&sensor=true', lat, lon) ;
After running this code, typing url in the command window leads to:
>> url
url =
http://maps.googleapis.com/maps/api/geocode/xml?latlng=42.3899,-89.0366&sensor=true
At this point you see that if you are able to get lat and lon as numerical values from some database/dataset of locations of yours, you are able to generate the corresponding URL for requesting information to/from Google.
Now for the second part, type the following in your command window:
>> buffer = urlread(url)
this should display the content of buffer, which is a string that contains the XML data structure that you saw earlier in you browser. But now you have it in MATLAB. There are in fact several ways to wander through this data. One would be basic character matching using e.g. regular expressions:
>> results = regexp(buffer, '<formatted_address>(.*?)<', 'tokens') ;
>> for k = 1 : length(results), fprintf('%s\n', results{k}{1}) ; end
12734 Ventura Boulevard, Machesney Park, IL 61115, USA
Machesney Park, IL 61115, USA
Machesney Park, IL, USA
Harlem, IL, USA
Winnebago, IL, USA
Rockford, USA
Illinois, USA
United States
This code extracts all content within tags named formatted_address (there are eight occurrences), without taking in account the structure of the XML data. As you can see, Rockfort is one of the matches (#6), but can you be sure that you will always want the 6th entry?
The other solution is to read the response from Google with an XML parser, which allows you to wander through the structure/tree, and look for tags/content:
>> documentNode = xmlread(url) ;
this time you don't get a string but a more complicated object, which has methods/tools for extracting content ..
>> formattedAddressNodes = documentNode.getElementsByTagName('formatted_address') ;
>> for k = 1 : formattedAddressNodes.getLength()
fprintf('%s\n', ...
char(formattedAddressNodes.item(k-1).getTextContent())) ;
end
12734 Ventura Boulevard, Machesney Park, IL 61115, USA
Machesney Park, IL 61115, USA
Machesney Park, IL, USA
Harlem, IL, USA
Winnebago, IL, USA
Rockford, USA
Illinois, USA
United States
Same content because we looked for the same tag, but using this solution you would be able to wander through the XML data structure, which might provide you with enough information to extract the relevant address.
To conclude, this is the most basic example that I could imagine for illustrating sending a request to the Google Maps API. By learning more about this API, you might find out that there are ways to limit the response to the most relevant location only, which would simplify the processing of the response. You might find that there is no way to simplify the response, but have a better understanding of how to extract the relevant address. In any case, I guess that you will have to understand better the API and the XML parser.
Just a few final comments..
Timing
>> tic ; buffer = urlread(url) ; toc
Elapsed time is 0.783007 seconds.
as you can see, it takes almost 1s for sending/receiving a single request. This might be too long if you have a large amount of locations (and you might finally have no other choice than implementing a local solution).
Limits
Information in the "Limits" section here indicate that you would be limited to 2,500 requests per day (and 100k if you had an account), and the note seems to indicate that it would fall in the "prohibited usage" category as you would not be displaying a Google map .. but I didn't fully read their policy.
Possible criterion? No, see edit below.
Looking at the XML returned by Google specifically for the coordinates that you gave, it seems that the relevant formatted address is the one that lies within a 'result' node with no 'type' node/tag. This might be a criterion for selection, but it should be verified. EDIT: I checked it fast and it is not a valid criterion.
Another possible criterion
As indicated here: "Generally, addresses are returned from most specific to least specific; the more exact address is the most prominent result, .."
This means that you could try taking the 3rd or 2nd entry in reverse order, assuming that the last is the country, the one before is the state, and the one before is the county? the city? .. in conjunction with the type (see types listed here) to pinpoint the city/locality.
Jacqueline
Jacqueline el 30 de Jul. de 2013
This works perfectly! Thanks so much, I really appreciate your help
Cedric
Cedric el 30 de Jul. de 2013
You're welcome; let me know if you get stuck wrapping all that together. Parsing XML is not the easiest thing to do as it is based on tools which are not fully documented.

Iniciar sesión para comentar.

Más respuestas (1)

Gholamreza Akbarizadeh
Gholamreza Akbarizadeh el 11 de Dic. de 2019

0 votos

Hello all.
Using API keys is a restriction, and I think it is not an efficient way for an App. Is it another way to simply read the data form Google Map as a url, and get the location name specified by GPS coordinates?
Regrads.

Categorías

Preguntada:

el 29 de Jul. de 2013

Respondida:

el 11 de Dic. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by