<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spatial Horizons &#187; GeoNames</title>
	<atom:link href="http://spatialhorizons.com/category/geonames/feed/" rel="self" type="application/rss+xml" />
	<link>http://spatialhorizons.com</link>
	<description>Exploring Geographic Technologies</description>
	<lastBuildDate>Tue, 16 Aug 2011 23:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>GeoNames Search (3)</title>
		<link>http://spatialhorizons.com/2007/03/11/geonames-search-3/</link>
		<comments>http://spatialhorizons.com/2007/03/11/geonames-search-3/#comments</comments>
		<pubDate>Sun, 11 Mar 2007 12:36:31 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GeoNames]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/03/11/geonames-search-3/</guid>
		<description><![CDATA[In our last post, we created an application that queried the GeoNames webservice and returned the results into a table. A logical next step would be to add the capability to map the results on a world map. So today will be use some of the code we already wrote for the WorldMapper project to [...]]]></description>
			<content:encoded><![CDATA[<p>In our last <a target="_blank" href="http://spatialhorizons.com/2007/03/04/geonames-search-2/">post</a>, we created an application that queried the GeoNames webservice and returned the results into a table.  A logical next step would be to add the capability to map the results on a world map. So today will be use some of the code we already wrote for the <a target="_blank" href="http://spatialhorizons.com/2007/02/15/world-mapper-1/">WorldMapper</a> project to add that functionality.</p>
<p>Here is how my final form looked after I added a world map picture and 2 new buttons:</p>
<p><img width="400" height="431" id="image29" alt="GeoNames3" src="http://spatialhorizons.com/wp-content/uploads/2007/03/geonames3.jpg" /></p>
<p><span id="more-30"></span></p>
<p>The only new code I had to write was for the &#8220;Map It&#8221; button.  I needed to access the selected row in the results table, grab the longitude and latitude, and add a point on the map at that location.</p>
<p>Here is how I did that:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> btnMapIt_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  DataGridViewSelectedRowCollection rows <span style="color: #008000;">=</span> grdResults<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedRows</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>DataGridViewRow row <span style="color: #0600FF; font-weight: bold;">in</span> rows<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> longX <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDouble</span><span style="color: #008000;">&#40;</span>row<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;lng&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> latY <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDouble</span><span style="color: #008000;">&#40;</span>row<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;lat&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Point mapPoint <span style="color: #008000;">=</span> ConvertLatLon2ImagePosition<span style="color: #008000;">&#40;</span>longX, latY<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    PlotPosition<span style="color: #008000;">&#40;</span>mapPoint<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</p>
<p>The source code for the two functions, ConvertLatLon2ImagePosition and PlotPosition, was already covered in the <a target="_blank" href="http://spatialhorizons.com/2007/02/15/world-mapper-1/">WorldMapper</a> post.  The &#8220;Clear Map&#8221; button&#8217;s code is also found in that post.</p>
<p>In the above screenshot, I queried GeoNames for Toronto and was able to map the location of the city on the map.  For non-US locations, be sure to look up the county code <a target="_blank" href="http://www.geonames.org/countries/">here</a>.</p>
<p>This concludes our GeoNames posts for now.  Many of these early projects are really laying the groundwork for some much cooler projects to come.  If you have any questions about what we&#8217;ve already covered or have an idea for a project you would like to see, send us an e-mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/03/11/geonames-search-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GeoNames Search (2)</title>
		<link>http://spatialhorizons.com/2007/03/04/geonames-search-2/</link>
		<comments>http://spatialhorizons.com/2007/03/04/geonames-search-2/#comments</comments>
		<pubDate>Sun, 04 Mar 2007 00:09:10 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GeoNames]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/03/04/geonames-search-2/</guid>
		<description><![CDATA[In this demonstration, a simple C# application will be created to search the GeoNames database and display the results in tabular form. Let&#8217;s begin by creating a new Windows Application in Visual C# Express. A simple form can be set up to capture the necessary input parameters: location name, country, and the number of rows [...]]]></description>
			<content:encoded><![CDATA[<p>In this demonstration, a simple C# application will be created to search the GeoNames database and display the results in tabular form.  Let&#8217;s begin by creating a new Windows Application in <a href="http://msdn.microsoft.com/vstudio/express/visualcsharp/default.aspx" target="_blank">Visual C# Express</a>.</p>
<p>A simple form can be set up to capture the necessary input parameters: location name, country, and the number of rows returned.  At the bottom of the form, add a DataGridView control to display the XML results.  My completed form looks like this:</p>
<p><img id="image25" src="http://spatialhorizons.com/wp-content/uploads/2007/03/geonamessearch1.jpg" alt="GeoNamesSearch1" width="400" height="327" /></p>
<p><span id="more-24"></span></p>
<p>Since we are going to be dealing with XML in our code, add this using statement to the code:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Xml</span><span style="color: #008000;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>In this example, we only need to add code to the button to perform the search.  Double-click on the button itself to create a Click event in the code view.  Here is how I performed the search:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> searchPhrase <span style="color: #008000;">=</span> txtSearch<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> searchCountry <span style="color: #008000;">=</span> txtCountry<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">int</span> maxRows <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
  maxRows <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt16</span><span style="color: #008000;">&#40;</span>txtMaxRows<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">catch</span>
<span style="color: #008000;">&#123;</span>
  MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Enter a valid number of rows!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>maxRows <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span> url <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://ws.geonames.org/search?q=&quot;</span><span style="color: #008000;">;</span>
  url <span style="color: #008000;">+=</span> searchPhrase <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&amp;amp;maxRows=&quot;</span> <span style="color: #008000;">+</span> maxRows<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&amp;amp;country=&quot;</span> <span style="color: #008000;">+</span> searchCountry<span style="color: #008000;">;</span>
  lblURL<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> url<span style="color: #008000;">;</span> 
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">try</span>
  <span style="color: #008000;">&#123;</span>
    DataSet dsResults <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataSet<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;xmlResults&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    dsResults<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadXml</span><span style="color: #008000;">&#40;</span>url<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    grdResults<span style="color: #008000;">.</span><span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> dsResults<span style="color: #008000;">;</span>
    grdResults<span style="color: #008000;">.</span><span style="color: #0000FF;">DataMember</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;geoname&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">catch</span>
  <span style="color: #008000;">&#123;</span>
    MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error in connection or URL!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>In the above code, we perform several steps.  We first capture and validate the input parameters from the form&#8217;s controls (txtSearch, txtCountry, txtMaxRows).  We then construct the URL as we discussed in the <a href="http://spatialhorizons.com/2007/02/25/geonames-search-1/">previous post</a>.  Finally, we use the URL to perform the search and return the results in an XML dataset.  The results are easily displayed in the DataGridView for display.</p>
<p>Here is what the results for an &#8220;Oakland&#8221; search looks like:</p>
<p><img id="image27" src="http://spatialhorizons.com/wp-content/uploads/2007/03/geonamessearch2.jpg" alt="GeoNamesSearch2" width="400" height="326" /></p>
<p>C# and the .NET framework provide easy access for obtaining and using XML data.  As the above example shows, it makes no difference if the XML data is on your local hard drive or a website.  Further processing of XML is also very straightforward and will be explored in future projects.  For example, the GeoNames search results could be added onto a map or exported to a local file.</p>
<p><a title="GeoNames Search (3)" href="http://spatialhorizons.com/2007/03/11/geonames-search-3/">Continue to part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/03/04/geonames-search-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GeoNames Search (1)</title>
		<link>http://spatialhorizons.com/2007/02/25/geonames-search-1/</link>
		<comments>http://spatialhorizons.com/2007/02/25/geonames-search-1/#comments</comments>
		<pubDate>Sun, 25 Feb 2007 03:20:12 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[GeoNames]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/02/25/geonames-search-1/</guid>
		<description><![CDATA[Geonames.org is a great resource for free geographic information. It is a collection of millions of locations that can be searched online or downloaded for offline use. In this project, we will build a simple application to query the Geonames web service and display the results in a table and on a map. The Geonames [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.geonames.org/" target="_blank">Geonames.org</a> is a great resource for free geographic information.  It is a collection of millions of locations that can be searched online or downloaded for offline use. In this project, we will build a simple application to query the Geonames web service and display the results in a table and on a map.</p>
<p><span id="more-22"></span></p>
<p>The Geonames web service accepts a query as a URL and returns an XML or JSON document.  For example&#8230;</p>
<p><a href="http://ws.geonames.org/search?q=detroit&amp;maxRows=3" target="_blank">http://ws.geonames.org/search?q=detroit&amp;maxRows=3</a><a href="http://ws.geonames.org/search?q=michigan&amp;maxRows=3" target="_blank"><br />
</a></p>
<p>returns an XML document with the top 3 results for Detroit.  As expected, the city of Detroit in the State of Michigan (USA) is returned first which is probably what the search was intended to find.</p>
<p>The query can be confined to a particular country like this&#8230;</p>
<p><a href="http://ws.geonames.org/search?q=detroit&amp;maxRows=3&amp;country=US" target="_blank">http://ws.geonames.org/search?q=detroit&amp;maxRows=3&amp;country=US</a></p>
<p>Geonames offers a number of other web services, including recent earthquakes, weather conditions, and reverse geocoding.  While we may explore these later, for now let&#8217;s focus on obtaining latitude and longitude information for places.</p>
<p>The results above are returned as XML in your web browser.  This sort of display is fine for casual viewing but not for custom mapping applications.  Fortunately, C# provides an easy framework for obtaining and working with XML documents.</p>
<p><a title="GeoNames Search (2)" href="http://spatialhorizons.com/2007/03/04/geonames-search-2/">Continue to part 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/02/25/geonames-search-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

