GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Getting all Cities (NOT Counties) for a State/Province/Region  XML
Forum Index -> Administrative Divisions
Author Message
bkerr_test



Joined: 16/01/2014 15:23:43
Messages: 2
Offline

Hello,

I'm using the Java API and making calls to WebService.children(); to populate lists of Continents, Countries, State/Province/Region according to the chosen parent.
For example, North America will give Canada, USA, Mexico, Cuba, etc etc. Selecting Canada will give British Columbia, Yukon Territories, Alberta, Ontario, etc etc.
From here I want to be able to select a Province and get all of its cities, but it gives Counties.

The most confusing part of this is that if I use the web service to find the hierarchy of a city, for example London, Ontario, it gives me what I'm after: London > Ontario > Canada > North America > Earth. But if I use the web service to get children of Ontario, I get all of the counties, including Middlesex (the county where London is located).

I don't see the link in children between province and city that exists in the hierarchy. Am I missing something in this?

Any advice would be greatly appreciated.
bkerr_test



Joined: 16/01/2014 15:23:43
Messages: 2
Offline

Thanks to a question and answer on StackOverflow (http://stackoverflow.com/questions/4812020/get-state-province-from-geonames-data), I've learned that the connection a city has with its Province/State/etc is that the AdminCode1 will match. Using this, I do the following:

Code:
 private List<Toponym> getCities(Toponym region)
     {
         List<Toponym> cities = new ArrayList<Toponym>();
 
         WebService.setUserName(WEBSERVICE_USERNAME);
 
         ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria();
         ToponymSearchResult searchResult;
 
         try {
             searchCriteria.setCountryCode(region.getCountryCode());
             searchCriteria.setStyle(Style.FULL);
             searchCriteria.setAdminCode1(region.getAdminCode1());   // AdminCode1 is for top-level Administrative division (Region)
             searchCriteria.setFeatureClass(FeatureClass.P); // use the FeatureCode of P (Populated Area)
 
             searchResult = WebService.search(searchCriteria);
             searchResult.setStyle(Style.FULL);
 
             for (Toponym t : searchResult.getToponyms())
             {
                 cities.add(new AdtToponym(t));
             }
 
         } catch (InvalidParameterException e) {
            
         }
         catch (InsufficientStyleException e) {
           
         } catch (Exception e) {
             
         }
 
         return cities;
     }
 


I hope that this can help someone else. There may be a better way to do this, and I hope that there is a proper way to use hierarchy to get this information. If anyone ever learns how, please update this thread with proper instructions, but until then, this works.
 
Forum Index -> Administrative Divisions
Go to:   
Powered by JForum 2.1.5 © JForum Team