7. Challenge 5 - Solution

The web site http://nominatim.openstreetmap.org/ offers a search service http://nominatim.openstreetmap.org/search/ that when given a street name will return occurrences of such street name, each one with characterizing information such as longitude and latitude. In this challenge, you should write an expression that returns a collection of location using for example a pair whose key is the description of the location and the value its latitude and longitude.

For example we did a query with Rue de Paris and we selected two results and they are as follows:

'La Rue de Paris, Lainsecq, Auxerre, Yonne, Bourgogne-Franche-Comte, France metropolitaine, 89520, France'->(47.5612874@3.2933713) .
'Rue de Paris, Boulogne-Billancourt, Hauts-de-Seine, Ile-de-France, France metropolitaine, 92100, France'->(48.8416645@2.236876) .

7.0.1. Hints:

  • We suggest to ask the server to return JSON.
  • To specify the format, put 'jsonv2' to 'format' in the query.
  • You can also define a limit to the number of results using the parameter 'limit' and put it to '10'.
  • To parse JSON, have a look at the STON class
  • Use the message -> to build a pair.

7.0.2. Solution

	| query result entries |
	query := UIManager default request: 'Find location for:'.
	result := STON fromString: 
			 (ZnClient new
			 	url: 'http://nominatim.openstreetmap.org/search/', query;
				queryAt: 'format' put: 'jsonv2';
				queryAt: 'limit' put: '10';
	(result collect: [ :dct |
		(dct at: 'display_name') -> 
			((dct at: 'lat') asNumber @ (dct at: 'lon') asNumber)
			]) inspect