The nice thing about Stormy is that it integrates with SQL so easily! That seems logically, but note how difficult it is to use geometry within nHibernate or the Entity Framework!

So here is some Quick Start for SqlGeometry, assuming on the database side:

  1. a database called stormy (in SQL Server), adapt your connection string if necessary
  2. a table called cities, created by something like this: create table cities (id int identity, name nvarchar(80), location geometry); 
  3. table is filled with some cities by lines like this: insert into cities(name,location) values('Amsterdam', geometry::STGeomFromText('POINT(4.9 52.3667)', 4326));

Here is the example:

 

using System;
using System.Collections.Generic;

// 0. Use the SqlClient; SqlServer types, and Stormy
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Types;
using Stormy;


// 1. Create a "Model" (usually, we have one already)
public class City
{
    public string Name { get; set; }
    public SqlGeometry Location { get; set; }
}

// 2. Create a "Mapper" 
public class CityMapper : ISelectable<City>
{
    public City ApplySelect(IDataReader reader)
    {
        return new City()
            {
                Name = reader["name"].ToString(),
                Location = (SqlGeometry)reader["location"]
            };
    }
}


namespace StormyGeometry
{
    class Program
    {
        static void Main(string[] args)
        {
            // 3. Register the mapper, such that it is used for model City
            Orm.Register<City>(new CityMapper());

            // 4. Create the connection (connection strings are just ADO.NET)
            var connection = new Stormy.Connection(new SqlConnection(
                    @"
                        Data Source=localhost\SQLEXPRESS;
                        Initial Catalog=stormy;
                        Integrated Security=SSPI;
                    "));

            // 5. Get list of models (here Cities) using SQL 
            var cities = connection.Select<City>("select * from cities");
            foreach (City city in cities)
            {
                // Just use the SqlGeometry methods such as STX, STY, STIntersects, etc.
                System.Console.WriteLine("City {0} location lat={1}, lon={2}", 
                    city.Name, city.Location.STY, city.Location.STX);
            }
        }
    }
}

Last edited Jan 1, 2012 at 10:28 PM by barendgehrels, version 10

Comments

No comments yet.