Geocoding II

Geocoding is the processing of single addresses into geographic coordinates and can be performed by a number of free online web sites. Many of these same sites can also perform reverse geocoding, or the use of geographic coordinates to determine addresses or place names. The web site links below provide access to free single address or place name geocoding services. I want to show 2 of them, a commercial and a free one.
Pic:93_5_mX4mapbox_bonnaud0(2).png
  • 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.

function TAddressGeoCodeOSM(faddress: string): string; var url, res, display: string; jo, location: TJSONObject; urlid: TIduri; windown: TWinApiDownload; begin urlid:= TIdURI.create(''); url:= urlid.URLEncode('https://nominatim.openstreetmap.org/search? format=json&q='+fAddress); windown:= TWinApiDownload.create; windown.useragent:= 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1'; windown.url:= url; windown.download1(res); //windown.OnWorkStart StrReplace(res, '[{', '{'); jo:= TJSONObject.create4(res); try if jo.getString('place_id') <> ' ' then display:= jo.getstring('display_name'); result:= Format('Coords: lat %2.3f lng %2.3f :%s importance: %2.4f', [jo.getdouble('lat'),jo.getdouble('lon'),display, jo.getdouble('importance')]); except writeln('E: '+ExceptiontoString(exceptiontype, exceptionparam)); finally jo.Free; urlid.free; windown.free; end; end;
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;
with TWinApiDownload.create do begin Useragent:= 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1'; Url:= 'https://nominatim.openstreetmap.org/reverse?lat=' +flots(flat)+'&lon='+flots(flong)+'&zoom=10&format=json'; Download1(rest); //writeln(rest) with TJSONObject.create4(rest) do begin writeln('display_name: '+getstring('display_name')); free; end; free; //ApiDown end;
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
https://www.latlong.net/c/?lat=46.617&long=5.430
Pic: 93_mX4mapbox_osm_bonnaud_referencemap.jpg
Exception: TJSONObject["status--\u0020You\u0020must\u0020enable\u0020Billing\u0020on\u0020the\u0020Google\u0020Cloud\u0020Project\u0020at\u0020https\u003A//console.cloud.google.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).

function TAddressGeoCoding4(faddr, fcountry: string): String; var Url,API_KEY, source: string; jo, locate: TJSONObject; urlid: TIdURI; fLat,fLong: double; 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;
writeln(TAddressGeoCoding4('Halifax','Canada')); display_name: Halifax, Halifax Regional Municipality, Halifax County, Nova Scotia, Canada debug hash 7861896 Coordinates: lat 44.649 lng -63.586 :CanVec+ by Natural Resources Canada
jo:= TJSONObject.Create4(wdc_WinInet_HttpGet2(Url,Nil));

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.

https://www.latlong.net/c/?lat=46.617&long=5.430

Geocoding topics and Script

TEE NS/SBB

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store