Geography Reference
In-Depth Information
Conclusion
The final thing we need to do is create the two methods to retrieve our city and town lists
from our GIS database.
SharpMap is perfectly capable of taking the polygon outline we found earlier and querying
the other layers in the map to tell us which points fall within which area. However, as this
book is primarily about using a GIS database, I'd like to conclude by letting Postgres do the
heavy lifting for us once more.
The two methods are fairly similar, so I've copied both of them out and just described them
as one.
private static
List
<
string
> GetTownsForCounty(
string
countyName)
{
string
sql =
string
.Format(
"SELECT t.Name FROM ukcountys c, uktowns t WHERE name2 = :county AND
ST_Within(t.geometry,ST_Transform(c.the_geom,27700))"
);
List
<
string
> results =
new
List
<
string
>();
using
(
NpgsqlConnection
conn =
new
NpgsqlConnection
(_connString))
{
conn.Open();
using
(
NpgsqlCommand
command =
new
NpgsqlCommand
(sql, conn))
{
command.Parameters.Add(
new
NpgsqlParameter
(
"county"
,
NpgsqlDbType
.Varchar));
command.Parameters[0].Value = countyName;
using
(
NpgsqlDataReader
dr = command.ExecuteReader())
{
while
(dr.Read())
{
results.Add(dr.GetString(0));
}
}
}
}
return
results;