Geocoding: Maxbox Starter93 With Geocoding
Geocoding: Maxbox Starter93 With Geocoding
Geocoding
______________________________________________________________________________
maXbox Starter93 – Code with Geocoding
In the beginning there was nothing, which exploded.
— Terry Pratchett
Pic:93_5_mX4mapbox_bonnaud0(2).png
Just like every actual house has its address (which includes the
number, the name of the street, city), every single point on the
surface of earth can be specified by the latitude and longitude
coordinates (lat & lng).
For example you see the location Bonnaud on the map above, we
convert them to lat and long:
Coords: lat 46.621 lng 5.434 :Bonnaud, Val-Sonnette, Lons-le-
Saunier, Jura, Bourgogne-Franche-Comté, France métropolitaine,
39190, France importance: 0.7350
1/7
Geographic coordinates of Delphi, Greece
Latitude: 38°28'45" N
Longitude: 22°29'36" E
Elevation above sea level: 560 m = 1837 ft
OpenStreetMap
So how I did this? With the OpenStreetMap API Nominatim.
The OSM Nominatim is a search engine for OpenStreetMap data. From
this site you may search for a name or address (like Route de
Bonnaud, Bonnaud, France), or look up place names by geographic
coordinates. Each result will link to details page where you can
inspect what data about the object is saved in the database and
investigate how the address of the object has been computed (URI
and JSON for example):
I use an URI, a JSON and a HTTPGet object and call the function:
You should in particular verify that you have set a custom HTTP
referrer or HTTP user agent (windown.useragent:=) that identifies
your application, and that you are not overusing the service with
massive bulk requests. Otherwise you get following message:
2/7
<p>You have been blocked because you have violated the<a
href="https://operations.osmfoundation.org/policies/nominatim/">us
age policy</a>
of OSM's Nominatim geocoding service. Please be aware that OSM's
resources are limited and shared between many users. The usage
policy is there to ensure that the service remains usable for
everybody.</p>
In case you missed the user agent, I passed it already with the
constructor:
constructor TWinApiDownload.Create;
begin
inherited;
fUserAgent:= 'Mozilla/5.001(windows; U; NT4.0; en-US;) Gecko/25250101';
fProgressUpdateInterval:= 100;
fCachingEnabled:= True;
fStop:= False;
fActive:= False;
end;
https://www.latlong.net/c/?lat=46.617&long=5.430
3/7
Two attributes in OSM are of interest, the zoom and the
importance. Renderers can't display on a map in each zoom level,
so they have to make a selection. The more important an object is,
the earlier (in zooming in) the object should be rendered. Also
the this key "importance" with a value which refers to the area
this object is important for, e.g. regional or urban:
res back_: Coords: lat 46.62084 lng 5.43424 :Bonnaud, Val-
Sonnette, Lons-le-Saunier, Jura, Bourgogne-Franche-Comté, France
métropolitaine, 39190, France importance: 0.7350
Pic: 93_mX4mapbox_osm_bonnaud_referencemap.jpg
Pic: 93_mX4mapbox_weatherapp_bonnaud_sat_reference.jpg
4/7
By the way you can simply open the map in maXbox with the command:
OpenWeb('https://www.latlong.net/c/?lat=46.617&long=5.430');
Exception:
TJSONObject["status--\u0020You\u0020must\u0020enable\u0020Billing\u0020on\u0020t
he\u0020Google\u0020Cloud\u0020Project\u0020at\u0020https\u003A//console.cloud.g
oogle.com/project/_/billing/enable\u0020Learn\u0020more\u0020at\u0020https\u003A
//developers.google.com/maps/gmp-get-started"] not found.
Geocodio
Geocoding (also known as forward geocoding) allows you to convert
one or more addresses into geographic coordinates (i.e. latitude
and longitude). Geocoding will also parse the address and append
additional information (e.g. if you specify a zip code, Geocodio
will return the city and state corresponding zip code as well).
Geocodio supports geocoding of addresses, cities and zip codes in
various formats. Geocodio provides bulk geocoding and reverse
lookup services through a REST API. The API is able to process a
single address, as well as handle bulk requests of up to 10,000
addresses. The results are returned with an accuracy score
indicating the confidence Geocodio has in the accuracy of the
result. It is also able to parse addresses into individual
components.
So I started with Geocodio https://www.geocod.io/docs/#geocoding
and registered to get an API Key. Geocodio’s RESTful API allows
you to perform forward and reverse geocoding lookups. They support
both batch requests as well as individual lookups, but and this is
the disadvantage, for the moment only for USA and Canada.
The base API url is https://api.geocod.io/v1.7/.
All HTTP responses (including errors) are returned with JSON-
formatted output. For the code I tested with Python and maXbox
with an Wininet and JSON Object:
function TAddressGeoCoding4(faddr, fcountry: string): String;
var
Url,API_KEY, source: string;
jo, locate: TJSONObject;
urlid: TIdURI;
fLat,fLong: double;
5/7
begin
urlid:= TIdURI.create('');
API_KEY:='785b4141b...................'; //get your own one please
if fcountry <> '' then
Url:= urlid.URLEncode('https://api.geocod.io/v1.7/geocode?q='+
fAddr+'&country='+fcountry+'&api_key='+API_KEY) else
Url:= urlid.URLEncode('https://api.geocod.io/v1.7/geocode?q='+
fAddr+'&api_key='+API_KEY);
jo:= TJSONObject.Create4(wdc_WinInet_HttpGet2(Url,Nil));
try
jo.getString('input')
locate:=
jo.getJSONArray('results').getJSONObject(0).getJSONObject('location');
source:= jo.getJSONArray('results').getJSONObject(0).getString('source');
//geometry.getJSONObject('coordinates');
fLat:= locate.getDouble('lat')
fLong:= locate.getDouble('lng');
result:=Format('Coordinates: lat %2.3f lng %2.3f :%s',[flat,flong,source]);
except
Xraise(Exception.Create(jo.getString('error')));
finally
jo.Free;
urlid.free;
end;
end;
6/7
like Cologne, Paris or Bern all over the world we should also
check the source as identity authentication too.
If a city is provided without a state, Geocodio will automatically
guess and add the state based on what it is most likely to be.
Geocodio also understands shorthand's for both streets and cities,
for example NYC, SF, etc., are acceptable city names. Each
geocoded result is returned with an accuracy score (like the
importance in OpenStreetMap), which is a decimal number ranging
from 0.00 to 1.00. This score is generated by an internal Geocodio
engine based on how accurate the result is believed to be. The
higher the score, the better the result. Results are always
returned ordered by accuracy score.
Conclusion
Geocoded locations expressed in latitude, longitude coordinates
can be obtained one at a time in web maps such as OSM, Geocodio,
Bing Maps or Google Maps. Reverse geocoding is the process of
converting latitude and longitude into a street address. The
relative ease of geocoding and resulting accuracy can vary widely
depending on a number of factors. What is the nature or the source
of the data? How accurate is it and what format is it in? What
geocoding technique will be used like REST or batch or scripts?
Determining a geocoding strategy that best suits a particular
need is not always clear.
The key to confidently geocoding data lies in understanding the
reference table of the GIS which the data is being matched to, how
a match is found, and the resulting spatial accuracy.
Scriptname: 1108_CAI_GeocodingTWininetAPI_Maponly.pas
7/7