Saturday, April 28, 2012

Parsing UTFGrid data to polygons

I've talked previously about UTFGrid. It provides, as described by Development Seed, "a standard, scalable way of encoding data for hundreds or thousands of features alongside your map tiles".

So, to sum it up, if we have this image tile for the hexagons of one of my previous posts.


We would have a corresponding json file with this UTFGrid
         !!!!!!!!!!!!!!!!!###################$$$$$$$$$$$$$$%%
            !!!!!!!!!!!!!!!!####################$$$$$$$$$$$$$$%%
             !!!!!!!!!!!!!!######################$$$$$$$$$$$$%%%
             !!!!!!!!!!!!!!######################$$$$$$$$$$$$%%%
             &&&&&&&&&&&&&&######################''''''''''''%%%
            &&&&&&&&&&&&&&&&####################''''''''''''''%%
            &&&&&&&&&&&&&&&&&###################''''''''''''''%%
           &&&&&&&&&&&&&&&&&&##################''''''''''''''''%
           &&&&&&&&&&&&&&&&&&&#################'''''''''''''''''
          &&&&&&&&&&&&&&&&&&&&################''''''''''''''''''
         &&&&&&&&&&&&&&&&&&&&&&##############'''''''''''''''''''
         &&&&&&&&&&&&&&&&&&&&&&##############'''''''''''''''''''
        &&&&&&&&&&&&&&&&&&&&&&&&############''''''''''''''''''''
        &&&&&&&&&&&&&&&&&&&&&&&&############''''''''''''''''''''
        &&&&&&&&&&&&&&&&&&&&&&&&((((((((((((''''''''''''''''''''
         &&&&&&&&&&&&&&&&&&&&&&(((((((((((((('''''''''''''''''''
          &&&&&&&&&&&&&&&&&&&&&(((((((((((((((''''''''''''''''''
          &&&&&&&&&&&&&&&&&&&&((((((((((((((((''''''''''''''''''
           &&&&&&&&&&&&&&&&&&(((((((((((((((((('''''''''''''''')
           &&&&&&&&&&&&&&&&&&(((((((((((((((((('''''''''''''''')
            &&&&&&&&&&&&&&&&((((((((((((((((((((''''''''''''''))
             &&&&&&&&&&&&&&&((((((((((((((((((((('''''''''''''))
             &&&&&&&&&&&&&&(((((((((((((((((((((('''''''''''')))
             **&&&&&&&&&&&&(((((((((((((((((((((('''''''''''+)))
             ***************(((((((((((((((((((((+++++++++++++))
            ****************((((((((((((((((((((++++++++++++++))
           ******************((((((((((((((((((++++++++++++++++)
           ******************((((((((((((((((((++++++++++++++++)
          ********************((((((((((((((((++++++++++++++++++
          *********************(((((((((((((((++++++++++++++++++
         **********************((((((((((((((+++++++++++++++++++
         ***********************(((((((((((((+++++++++++++++++++
        ************************((((((((((((++++++++++++++++++++
        ************************(((((((((((,++++++++++++++++++++
         ***********************,,,,,,,,,,,,,+++++++++++++++++++
         **********************,,,,,,,,,,,,,,+++++++++++++++++++
          ********************,,,,,,,,,,,,,,,,++++++++++++++++++
          ********************,,,,,,,,,,,,,,,,++++++++++++++++++
           ******************,,,,,,,,,,,,,,,,,,++++++++++++++++-
            *****************,,,,,,,,,,,,,,,,,,,++++++++++++++--
            ****************,,,,,,,,,,,,,,,,,,,,++++++++++++++--
             **************,,,,,,,,,,,,,,,,,,,,,,++++++++++++---
             **************,,,,,,,,,,,,,,,,,,,,,,++++++++++++---
             ..............,,,,,,,,,,,,,,,,,,,,,,////////////---
            ................,,,,,,,,,,,,,,,,,,,,//////////////--
            .................,,,,,,,,,,,,,,,,,,,//////////////--
           ..................,,,,,,,,,,,,,,,,,,////////////////-
           ...................,,,,,,,,,,,,,,,,,/////////////////
          ....................,,,,,,,,,,,,,,,,//////////////////
         ......................,,,,,,,,,,,,,,///////////////////
         ......................,,,,,,,,,,,,,,///////////////////
        ........................,,,,,,,,,,,,////////////////////
        ........................,,,,,,,,,,,,////////////////////
        ........................000000000000////////////////////
         ......................00000000000000///////////////////
          .....................000000000000000//////////////////
          ....................0000000000000000//////////////////
           ...................00000000000000000////////////////1
           ..................000000000000000000////////////////1
            ................00000000000000000000//////////////11
            ................00000000000000000000//////////////11
             ..............0000000000000000000000////////////111
              .............0000000000000000000000//////////22111
                          00000000000000000000000222222222222111

Thursday, April 26, 2012

Entity Framework 5 and Spatial Data (Part 2 - Comparison with NHibernate Spatial)

Part 1 - Introduction
Part 2 - Comparison with NHibernate Spatial

As promised in my previous post I'm going to do a comparison between NHibernate Spatial and Entity Framework.
 
I'll start by making a pros/cons list for both libraries, but just focusing on items that may differ between them.

 NHibernate Spatial

Pros:
  • True abstraction from the database with IGeoAPI/NetTopologySuite
  • Works with .NET 3.5 and newer
Cons:
  • No longer actively developed
  • Querying with LINQ/QueryOver doesn't work in many cases
  • Some spatial operators are not implemented


Entity Framework 5 Beta 2 Spatial

Pros:
  • Setup is a breeze
  • Querying with LINQ works flawlessly.
  • Natively integrated with Entity Framework
Cons:
  • The DbGeography/DbGeometry types belong to System.Data.Spatial. Therefore, we're always tied to the database model, even if just conceptually.
  • Requires .NET 4.5 (at least in the Beta 2. I don't know if support for 4.0 is on the road)

Wednesday, April 25, 2012

Entity Framework 5 and Spatial Data (Part 1 - Introduction)


I've blogged a lot on NHibernate Spatial. Although it works nicely, it seems to have been left for dead. It no longer compiles without modifying the source code and has many missing features and bugs.

On the other hand, Entity Framework is finally receiving native Spatial Support on version 5 (it's about time). Although it still is in Beta 2, I believe it's time to check it out and to see how it fancies itself. I'm going to create an example using it against a SQL Server database.

I'm going to use the following model:
  • There are 18 districts, each defined by a polygon
  • The are about 140.000 places, each defined by a geographic point
I've already used this model on a previous blog post.

I'll create two separate projects:
  • Class library with the model
  • Console application with some spatial operations

Friday, April 20, 2012

Maps and Boardgames (Part 3 - Client Side drawing + Optimizing)


In my previous post I've shown how one may interact with an hexagon layer on top of a Leaflet map.  The particular thing in that example is that it used hexagons embedded in the server-side tile images (created using TileMill). This has some pros and cons:

Pros:
- great performance
- works on most browsers (even pre-HTML5)
- less "visual noise", as the map is rendered at the same time as the hexagons (heck, they're in the same image tiles)

Cons:
- by itself it's difficult to have client-side behavior. It's possible using something like the UTFGrid technology, but that brings some additional complexity to the table and some limitations (I've used this approach in my previous post).
- the hexagons are static. So, if for example, we want to generate the hexagons with a different size or color or change them dynamically we're kind of screwed.

In this post I'm going to try a different approach, and return to the HTML5 Canvas implementation that I've started in the first post of this series.

Saturday, April 7, 2012

Maps and Boardgames (Part 2 - Using server tiles)


In my previous post I've created an hexagon layer in client-side using HTML5 Canvas. In this post I'm going to generate the hexagons on the server and display them on a map. So, the roadmap for this post will be:
  • Generate the hexagons  in C#
  • Store them as spatial data in SQL Server 2008 using NHibernate Spatial
  • Use QuantumGIS (or any other GIS tool for that mater) to read that data and tweak it 
  • Use Tilemill to generate a layer of .mbtiles, including UTFGrid data of the hexagons
  • Extract the .mbtiles file to image files
  • Use the data on a Leaflet map.
  • Create some basic interaction on the map



If you want you can skip to the end result.

Now, we've got two options for this. We can create hexagons that overlay perfectly on top of the map, all of them having the same pixel size, or all have the same edge size in meters, regardless of where they are on the world.