Determine if a string is a valid geographic location - php

I've got a bunch of "locations" - some are accurate (gaborone, botswana), some are geocodes (40.75,-73.997) and some are completely useless (#siliconcape). I need to find a way to run through the list and determine the City and Country of each string and geocode, and return nulls for the invalid locations.
Is there some sort of library/service/api/method that can be used to determine whether or not a given string represents a valid geographical location? While accounting for typos, ordering errors, etc?

Probably the easiest method would be to use something like the Google Geocoding API. It'll take a string and attempt to parse it to a location. You can get output as XML, JSON, CSV.
Here's some example CSV output:
input : gaborone, botswana
output: 200,4,-24.6541100,25.9087390
input : #siliconcape
output: 602,0,0,0
input : 40.75,-73.997
output: 200,8,"324 W 30th St, New York, NY 10001, USA"

I think I've found a solution - just sharing it here in case anyone else needs it:
Use Google Maps API geocoding - feed it addresses and get co-ordinates back. Details for doing it via PHP: http://www.phpriot.com/articles/google-maps-geocoding/

Related

How to parse coordinates to get only those that belong to a certain country?

i have a JSON file that contains lon and lat coordinates.
I want to parse this file and then create a new JSON file that contains only coordinates of a certain country.
Is it possible to do this? How to figure out what country the coordinates belong to?
In broad strokes, it sounds like you want to do what's called reverse geocoding. This is the process of taking a known set of coordinates and getting an address from it. The address would include the country.
Depending on the size of your data set, Google offers one that might suit you. You could write a routine to iterate your data set, reverse geocode each set of coordinates, and get a structured address (including country), which you could cache for future use. With the countries, you could then filter the list down to the specific country you're looking for.

Google Maps Geocode Issues: Certain Encoded URLs Failing

I am trying to get Lat/Lon for a list of locations from an address. It works for most addresses, but certain ones fail and return Zero Results.
Take this address as an example: 1045 Mono Way, Sonora, CA 95370
As you can see, that is a valid address on Google Maps - no issues there.
When pinging the Geocoder Api, I am encoding the address using urlencode(). This is the encoded address: 1045%20Mono%20Way%2C%20Sonora%2C%20CA%2095370
This is the link that I am accessing:
https://maps.googleapis.com/maps/api/geocode/xml?address=1045%20Mono%20Way%2C%20Sonora%2C%20CA%2095370&sensor=false
As you can see, zero results.
Now, if you change the first part of the address (1045) to any other number (e.g. 1044), you will get valid results.
I am lost.
Need help.
Please ;)
BTW, I am using this reply to get the lat/lon from the API: Google Maps - converting address to latitude & longitude - PHP backend?
I would bet that address just doesn't exist. The addresses are provided by local government, typically in a range of numbers that corresponds to a stretch of street. The fact that trying odd numbers doesn't work (1045, 1047, 1049) but even numbers do (1044, 1046, 1048) suggests that the addresses on one side of the road are not there. Or at least not in the dataset provided by the city or county.
The ideal solution would be a "fuzziness" argument to the API call, which would return a location similar to what their Maps site is. But since we can't rewrite third party APIs, I'd suggest checking for a "ZERO_RESULTS" response, and then resubmitting the request with just the street name if needed.
Also, you can do a geocode search without your API key, so you may want to keep that out of your question!

How do you fight against the entropy in the returned xml from google maps geocoding?

http://maps.googleapis.com/maps/api/geocode/xml?address=55124&sensor=false
For example ^ the url above will take the supplied zip code and return approximate lat and lng coordinates and a formatted address along with "address components" containing things like city,state,country and what not.
The question is (if you play with it and enter different types of addresses) how could you write code that would discern the differences in the address components? how could i be sure that the value i'm seeking is in fact the city and not the street?
It doesn't to be reliable xml. There is entropy if i'm not mistaken. Anyone know a way to guarantee a value?
Each address component has a type like
<type>postal_code</type>
You can then look for the type you're interested in.

Location searching

For the site I am building I want the user to be able to search for a location, however because I don't have a list of locations for the UK, I have developed a script in PHP which calls: http://ajax.googleapis.com/ajax/services/search/local in order to get the lat and lon coordinates of this location to store in the database for future use.
This works well apart from if I search for a town, for example Heaton, it brings back the one in Staffordshire rather than the one in Newcastle for example.
The whole reason I'm using the google ajax api is to cut down the google requests per day because I don't want to exceed the daily request limit.
As you can see though, I've hit a brick wall because the search isn't refined enough to bring back the correct location.
Can anyone suggest another way around this? I'm quite stuck. Thanks
I don't know if you have seen this already, but there is a parameter in the Google Search API that allows you to specify a boundary for the search:
GeocoderRequest object specification
The specification for a geocoding
request to be sent to the Geocoder.
Properties Type Description
address string Address. Optional.
bounds LatLngBounds LatLngBounds within which to search. Optional.
language string Preferred language for results. Optional.
location LatLng LatLng about which to search. Optional.
region string Country code top-level domain within which to search.
In your case I would specify a bounds around northumberland, so something like:
http://ajax.googleapis.com/ajax/services/search/local?q=heaton&v=3.1&region=GB&sensor=false&bounds=(55.395,%20-2.510%20,2054.342,%20-0.417)
might work...? I don't know if this would be relevant to your app though. Good luck!

google Maps getDistance without loading the page

Google maps has a function that lets you retreive the distance between two points: getDistance(), of the GDirections class.
Now, this is Javascript, so it'll only be able to calculate once the page has loaded.
My search form however, has the ability to indicate what's the maximum distance you want between yourself and another person and based on that, a list of search results has to be provided.
Since I can't use this function in PHP, is there another way for me to calculate the distance between 2 points on the earth? By giving up the street, postal code and city name, just like what Google maps needs.
This page has a list of google maps parameters for use in http connection. By specifying the output paramter you can choose to give back kml files or similar and can be used in any lanuage that can make http connections.
Looks like you want to calculate a Great Circle Distance
Formulas have been discussed here on stackoverflow before.
A point on earth is defined by it's latitude and longitude. If you want to calculate the distance between 2 points on earth by giving up the street, postal code and city name, you will need geo-referene data.
This data is available for free on the internet, but the accuracy and availability differ greatly from region to region. (USA data is of good quality, data for Kenia for example will be harder to come by)
So to answer your question:
Yes, there are other ways to calculate what you want. But they require more work/are more complex than just querying the google API.
You might want tot read: Creating a Store Locator with PHP, MySQL & Google Maps
Hope this points you into the right direction.
You can use kml file. It's xml-formatted file that you can recieve by link like http://maps.google.com/maps?f=d&hl=en&saddr=<src latitude>,<src longitude>&daddr=<dist latitude>,<dist longitude>&ie=UTF8&0&om=0&output=kml
in recieved file you can parse and summate distances from

Categories