Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

C#: Getting started with Nuget Package nGeo

Hi
I'm Hopping someone can give me a clue where to start
Background
I have a bunch of coordinates (Latitude, Longitude in WGS84) I need to locate where they are specifically their Country Admin Subdivision ISO-3166-2 codes (ISO-3166-1 is country)
Unfortunately the gadm.org  data doesn't hold the ISO codes bellow level 0 probably because they may not exist for some regions nor dose Google Maps API

https://www.geonames.org/  have an API which Will return the information  I need

Example: (don't use demo)
http://api.geonames.org/countrySubdivision?lat=52.235&lng=-0.151&username=demo&level=2

Open in new window


returns:
<geonames>
<countrySubdivision>
<countryCode>GB</countryCode>
<countryName>United Kingdom</countryName>
<adminCode1>ENG</adminCode1>
<adminName1>England</adminName1>
<code level="1" type="ISO3166-2">ENG</code>
<adminCode2>C3</adminCode2>
<adminName2>Cambridgeshire</adminName2>
<code level="2" type="ISO3166-2">CAM</code>
<distance>0</distance>

</countrySubdivision>


</geonames>


Open in new window



Could someone show me how to do this using the Nuget Package nGeo
The documentation on the How can I use it? is limited

using (var geoNamesClient = new NGeo.GeoNames.GeoNamesClient()) {    var toponym = geoNamesClient.Get(6295630, "demo"); // replace with your own username    // do something with the data }

Open in new window

Or an alternative

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

I think you could be right I've been playing with this again this morning it's not giving back what I need most of what is returned is NULL

the given URL returns XML is there a simple way of converting it to an object?


Sure. XML deserialization. Copy the XML and do Edit\Paste Special\Class from XML in a new code file. Then you can simple create your object graph from it.

Or you look at the source code of the client library and parse it manually.

I created a new countrySubdivision.cs   class and pasted as xml

Do I need to do some kind of pre-processing on the incoming stream before deserializing
I'm doing this
string URL = "http://api.geonames.org/countrySubdivision?lat=52.38333&lng=0&username=demo&level=5";

            countrySubdivision CountrySubdivision = new countrySubdivision();

            CountrySubdivision = Fetch(URL);

Open in new window


private static countrySubdivision Fetch(string URL)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(countrySubdivision));
            using (XmlReader reader = XmlReader.Create(URL))
            {
                countrySubdivision CountrySubdivision = (countrySubdivision)deserializer.Deserialize(reader);
                return CountrySubdivision;
            }
        }

Open in new window


I'm get the error below

System.InvalidOperationException
  HResult=0x80131509
  Message=There is an error in XML document (2, 2).
  Source=System.Xml
  StackTrace:
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
   at GeoNames.Program.Fetch(String URL) in E:\VB\GeoNames\GeoNames\Program.cs:line 66
   at GeoNames.Program.Main(String[] args) in E:\VB\GeoNames\GeoNames\Program.cs:line 21

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: <geonames xmlns=''> was not expected.


Open in new window

You'll need one XML containing all possible tags for using Paste Special. That's probable the reason why the client library does manual parsing and populating their object graph.
OK how do I get that? I pasted the response from the browser 
They explain all in their documentation.
BTW all the  .NET client library   for GeoNames are at least 5 years old
I've fingered out how to do this by using the JSON return instead of XML
Thanx for your help

Wer lesen kann..

a .NET client library (inclusive source code) to look into?
I wrote from the beginning that you should use that library to look into the coding concepts.