Geoffrey Emery
Data Stories..Letting Data Tell Your Story

SQL Spatial Tutorial 1: Beginning Spatial

May 16, 2008 16:24 by gemery

Spatial it taking root.. Well more like people are starting to realize that spatial rocks and that all data takes on new meaning once you take in its spatial aspect. Representing your data is like seeing your 3D after viewing the world in the 2D for your entire life. Some may say that this is exaggerating but wait until your data tells a 3D story.

So Lets start to dig into spatial.

3D vs 2D

The world is three dimensional object yet most of things we use to view are in 2d. For instance a paper map or a computer screen. Luckily for us we have Virtual Earth and SQL Spatial which takes in both 3D(geography) and 2D(geometry) shapes and types and displays in them in ways that we can visualize them both.

A great visualization from Hanes that shoes the 2D vs 3D visualization

image

Coordinate System

The next thing that never keep in mind is the coordinate system. I never am always amazed at the amounts of different coordinate systems that are out there. I have worked with at least 5 different coordinate systems and there are some that are so complex and so ridiculous that i never seem to start laughing.

For these articles we are going to be using the "WGS84 coordinate system have to consider an ellipsoidal shape of the earth and they can be described as either Cartesian coordinates or through latitude and longitude. Latitudes can have values between +90 and -90 degrees where 0 is at the equator and longitudes can have values between +180 and -180 degrees where 0 has been defined as a place in the Royal Observatory in Greenwich / United Kingdom. As mentioned earlier Virtual Earth in 2D-mode can only work between +85 and -85 degrees because of the Mercator projection."

 

image

So now that we have our basic understanding of 2d vs 3d maps we have or coordinate system lets dig into the SQL spatial


Virtual Earth Tutorial 2: Adding A Specific Map To A Web Page and Deep Dive into the Load Map Function

April 23, 2008 13:02 by gemery

 

 < Previous Tutorial Adding a basic Map 

LIVE DEMO | SOURCE

We are going to start off with the same code base of the last the Tutorial but this time we want to add a specific map to the page instead of the random map. There are actually several ways to do this but for his situation we are going to do it using a lat and longitude. The first thing we need to do is  get the latitude and longitude for the place that we want to have the map centered on. There are several way to do this but for now i just typed in "lat long for los angeles" and it returned this to me.

34.0° N - 118.2° W

So we are going to be taking this Lat long and use it to modify  the existing LoadMap() function. As you can see below we are going to new up a VELatLong object and pass in the lattitude and longitude to its constructor.

map.LoadMap(new VELatLong(34.0, -118.2)); 

Add in this to the sample from tutorial 1 and you get this map.

image

As you can see the map is centered on Los Angeles.This is really cool but it gets even better. This is where we do the deep dive into what the rest of the variables that  you can pass do. Below you will see a example of the function with all the possible variables that you can pass in.

LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer, mapOptions)

 

Lets talk about what each one of these variables does

VELatLong - A VeLatLong Class object that represents the center of the map..Like LA
zoom - A zoom level to display. Valid values range from 1 through 19. Optional. Default is 4. 1 being the highest(from Space)
style -

A VEMapStyle Enumeration value specifying the map style. Optional. Default is VEMapStyle.Road. 'h' -hybid, 'r' - road, 'o' - obilique , 'a'  -a ariel

Fixed - A Boolean value that specifies whether the map view is displayed as a fixed map that the user cannot change or move it around. Default is false.
mode - A VEMapMode Enumeration value that specifies whether to load the map in 2D or 3D mode. Optional. Default is VEMapMode.Mode2D or Mode3D. Note 3d requires a plug in
showSwitch - A Boolean value that specifies whether to show the map mode switch on the dashboard control this means change from 2d to 3d. Optional. Default is true (the switch is displayed).
TileBuffer - How much tile buffer to use when loading map. Default is 0 (do not load an extra boundary of tiles). This parameter is ignored in 3D mode. Bigger buffer here gives the user faster reaction when they start to drag the map around
mapOptions - A VEMapOptions Class that specifies other map options to set.

 

Now that we have that out of the way we can go ahead and play with all the values.Check out this demo i have set up for you. Of course the all source included. It works best in firefox right now due to a bug in ie. I  am working on a fix.

LIVE DEMO | SOURCE

image


Virtual Earth Tutorial 1: Adding A Map To A Web Page

April 16, 2008 20:18 by gemery

Live Demo | Download

This is a quick app to help you get started with adding map to web page.

1) The first thing we need to do is make a standard html file with a empty div called myMapdiv

   1: <html xmlns="http://www.w3.org/1999/xhtml">
   2: <head >
   3:    <title>Adding A Map To A Web Page</title>
   4:    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
   5: </head>
   6: <body >
   7:     <!-- the div element for the map to inject into -->
   8:     <div id="myMapdiv" ></div>
   9: </body>
  10: </html>

2)_ First thing you need to do is import the virtual earth JavaScript file

   1: <script type="text/javascript" 
   2:      src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1"></script>

The first thing that you will notice is that you are not calling a javascript file at all but some file that ashx end to it?

An ASP.NET HTTP Handler is a simple class that allows you to process a request and return a response to the browser. In this case we are returning the most recent version of the JavaScript file we are requesting.

3) Declare a JavaScript variable called map and set it equal to null. We are going to use this variable to store the virtual map object into.

We are then going to to write a function that news up VEMap object and then passes in the myMapDiv as the name of the div that we want to virtual earth to inject the map into. We then going to load the map with pass no variables therefor calling all the default.

   1: <script type="text/javascript">
   2:        var map = null;
   3:        function GetMap()
   4:        {
   5:            //the virtual earth map constructor takes in the div 
   6:            //elemetnt the map goes into.
   7:            map = new VEMap('myMapdiv');
   8:            // the default load map. Centers on the US many different 
   9:            //overloads can be placed here.
  10:            map.LoadMap();
  11:        }
  12:    </script>

4) Call our JavaScript function from onload attribute of the body tag, and we have map. Its important to not that we have to wait for the dom to fully load before we call the mapping function otherwise we risk the page not knowing about the myMapDiv div.

   1: <!-- Call the GetMap() function on page Load..
   2: .The Dom Needs to load before the map is created-->
   3: <body onload="GetMap()">
   4:     <!-- the div element for the map to inject into -->
   5:     <div id="myMapdiv" ></div>
   6: </body>
And this is the map that we get.

 

image

There you have it your first mapping WebSite.