Interactive Mapping Blog

Mapping Solutions News

Archive for September, 2008

Virtual Earth makes a ‘Quantum Leap’ in 3d

Thursday, September 25th, 2008

This new release of virtual earth has some major updates to the 3d plugin. The two biggest changes being real time clouds and ’street view’ imagery for 3d models.

The first time I saw the real time clouds demo’d last week my first thought was “this looks just like the opening titles to Quantum Leap!”. Zooming through the clouds at as if in a plane is really cool even if it is a bit of a gimmick.

To give you an idea of what it looks like take a look at the video below of Mount Snowdon in Wales. Unfortunately I couldn’t get our normal capture program (fraps) working with the latest 3d plugin so it’s not very smooth, and certainly not ‘zooming’ but it still adds a lot of atmosphere to the landscape.

Go and give it a try for yourself here are a few mountains to try:

Now if only we could commercially sell these 3d videos flyovers, like we currently do with Google Earth video tours, it would be brilliant, but Microsoft’s 3rd party licensing deals are always the issue.

That’s all for now next time we will look at the new 3d street view imagery that’s been added to some of the modeled cities.

Virtual Earth, Microsoft releases ver 6.2 today!

Sunday, September 21st, 2008

Its been rumoured for a few days but now its official the new version of the Virtual Earth ajax web control has been released. Considering this is just a minor release it  has a lot of features packed into it:

  1. Localised map tiles for German, French, Italian and Spanish

    image

  2. Built in client side pushpin clustering
  3. Landmark based routing, currently supported in the USA for fast food restaurants and petrol stations
  4. Massive improvements in European and Worldwide addresses parsing specifically improved for UK, France, Italy and Spain
  5. Static maps for mobile (see more below)
  6. Ability to disable the default virtual earth tiles, when adding custom tile layer (no word on how to license this scenario though)

There is now also a set of Virtual Earth web services (seperate from the mappoint web services):

  1. Imagery meta data webservice – gives date range of imagery and provider details (only for ortho imagery)
  2. Geocoding web service – using the virtual earth geocoder, not the mpws one and with the improvements as mentioned above
  3. Reverse geocoding worldwide – available wherever routing is available
  4. Search and routing – web service methods to match the ajax controls existing methods
  5. Image tiles and static maps – you can now request tiles via the web service rather than directly to use in your own custom map controls and also for mobile devices

There have even been some updates to the 3D control and content:

  1. Realtime weather and clouds, the clouds look great as you fly through them

    London's real time rain clouds ;-)

  2. Improved installed for the 3d plugin
  3. More options available to user including disabling weather (when its raining and you are showing the client there lovely new beach resort :-) )
  4. Improved texture quality on 3d buildings
  5. Improved ’street level’ textures on buildings – this is Microsofts answer to Google Streetview where they have textured the lower parts of buildings with imagery taken at street level
  6. Import of models from your own hosting (no need to upload to live.com) in the lightwave obj format
  7. New version of 3dvia shape

Getting Stuck In

To get started with version 6.2 first try the interactive sdk then read through the reference sdk including the whats new section.

Summary

There seem to have been a bit of a delay getting this release live as it was due to be released on the 22nd but it goes to show how big a release it is. So as you can see the Virtual Earth dev team have been really busy. Its brilliant to see how many features requested by the developer community have been released. To be honest when I was asked what new features we wanted to see in the future there weren’t many I could think of any more, it just goes to show how mature a product the ajax control is becoming. Now access to the tiles have been officially supported it will be interesting to see what commercial uses people find for this and the mobile maps.

It’s also great to see the hard work of the MultiMap team making it into the Virtual Earth control as they contributed to the pushpin clustering and the massive improvements in worldwide geocoding and address parsing.

Creating simple drive time maps with Virtual Earth

Saturday, September 20th, 2008

download code download code

distance coverable from central london in 30 minutes A feature that we are sometimes asked for with our mapping products and solutions is “can you show a map of how far I can travel by car from X in Y minutes?”. I was recently inspired by a demo of the Masternaut fleet management system at the UK Virtual Earth partners day, that showed very nice detailed polygons of how far you could travel my car from a given point in a given time.

Now I’m sure Masternauts solution, which works almost instantly, has a massive backend data store to query all this information but it got me wondering if we could do a very simple version of this just using the data available from Virtual Earth?

How might we achieve this?

image The basic process behind this idea is to create a set of “spokes” at evenly distributed angles around a centre point (the start location) like a bicycle wheel.

The diameter of the “wheel” being set to the maximum possible distance that could be travelled in a straight line from the centre of the “wheel”.

We then need to get the latitude and longitude of the point at the end of each “spoke” and use this to get directions, using Virtual Earth, from the centre of the “wheel” to each “spoke” end point.

We should then be able to go through the details of each “spoke” route returned and work out how far along the route we could get in the given time. This should give us a set of latitude / longitude points around the centre of our “wheel” indicating how far along each “spoke” we can travel in the given time, join them together in a polygon and you have your map!

So the basic process is:

1) Get X spoke point positions (lat / lon) at equal distance and equally distributed around a start point

2) Calculate the full driving directions from the start point to each of the X spoke positions

3) Go through each route returned and work out how far along the route we could travel in the given time, get the position (lat / lon) of this point

4) Add all the points from 3 and draw a polygon on the map showing the boundaries of the possible locations.

The Code

I would usually write this as a javascript class using the asp.net ajax libraries but to make it as readable as possible and portable to any framework it is coded as simple function calls with a few global variables.

First we have to work out the position of each end point of the “spokes”:

//starts the process of calculating the driving distances
function GetDrivingDistance(startpoint, mins, numspokes, maxspeedkmph) {
    gmins = mins; //need to use later
    gnumspokes = numspokes;
    gstartpoint = startpoint;
    
    //add original start point as pushpin to map
    var pin = new VEShape(VEShapeType.Pushpin, startpoint);
    gmap.AddShape(pin);
    
    //calculate maximum distance possible in a straight line
    //at the maxspeed
    var maxdist = (maxspeedkmph/60) * mins;
    
    gspokearray = new Array(); //new array to contain spoke VELatLong objects
    groutesarray = new Array(); //new array to contain the VERoute objects calculated
    
    //setup verouteoptions for directions
    gropts = BuildRouteOptions();
    
    //calculate position, in lat lon, of each spoke
    //assuming maximum distance away
    for (i = 0; i < numspokes; i++) {
        var bearing = (360 / numspokes) * i; //get bearing of current spoke
        gspokearray[i] = GetPointAtDistanceAndBearing(startpoint, maxdist, bearing);
 
        //get first route between startpoint and spoke endpoint
        //cannot call more than one GetDirections at once
        if (i == 0) {
            document.getElementById("progress").innerHTML = "Proccessing step 1 of " + gnumspokes;
 
            //plot spoke
            if (gdebug) gmap.AddShape(new VEShape(VEShapeType.Pushpin, gspokearray[i]));
            
            //get directions
            gmap.GetDirections(new Array(startpoint, gspokearray[i]), gropts);
        }
    }
}

This  function starts by setting some global variables and then adds a pushpin to the map at the location of the supplied VELatLong startpoint parameter.

The we calculate the maximum possible distance coverable in a straight line at the parameter maxspeedkmph (max speed in km per hour).

Then we setup a few arrays for storing results in later, and create the VERouteOptions class to be used in all routing (see full code for function BuildRouteOptions). The route options set a callback method called OnRoutingComplete when the route is complete

Now we loop through the number of spokes requested (parameter numspokes) and work out the bearing of each spoke around the start point using the GetPointAtDistanceAndBearing method (see full code).

After updating the UI with our progress we optionally plot the spoke end point if the code is in debug mode (see full code) just so we can see what is going on.

Finally we fire off the first request to VEMaps GetDirections method for the first spoke point.

When the GetDirections is complete the OnRoutingComplete function is called:

//the callback function when a route completes
function OnRoutingComplete(result) {
    //add new route to groutesarray
    groutesarray.push(result);
    if (groutesarray.length == gnumspokes) {
        //now all routing is complete start next stage
        ProcessDrivingRoutes();
    }
    else {
        //update progress
        document.getElementById("progress").innerHTML = "Proccessing step " + (groutesarray.length + 1) + " of " + gnumspokes;
 
        //debug plot spoke and route end
        if (gdebug) {
            gmap.AddShape(new VEShape(VEShapeType.Pushpin, gspokearray[groutesarray.length]));
            if(result.Distance>0) gmap.AddShape(new VEShape(VEShapeType.Pushpin, result.RouteLegs[0].Itinerary.Items[result.RouteLegs[0].Itinerary.Items.length-1].LatLong));//end of route, to see how close it got to spoke
        }
          
    
        //get next route
        gmap.GetDirections(new Array(gstartpoint, gspokearray[groutesarray.length]), gropts);
    } 
}

This adds the returned VERoute object to an array for processing later and then either calls ProcessDrivingRoutes if all routes have been completed or calls itself again with the next “spoke” position.

Once all routes have been completed the ProcessDrivingRoutes function is called:

function ProcessDrivingRoutes() {
    var pointsarray = new Array();
    var numroutesnotfound = 0;
    //process each route result
    for (i = 0; i < gnumspokes; i++) {
        //check route found, or if was in middle of sea etc so nothing returned!
        if (groutesarray[i].Distance > 0) {
            //work out how far down route we can actually get in the time supplied
            var totaltime = 0;
            for (var ii in groutesarray[i].RouteLegs[0].Itinerary.Items) {//will only be one RouteLeg
                //is current leg time greater than time supplied?
                var items = groutesarray[i].RouteLegs[0].Itinerary.Items;
                totaltime += items[ii].Time; //calculate the cumlative time of the route so far
                if (totaltime >= gmins * 60) {
                    //work out average speed between this point and the last
                    var routetimesecs = items[ii - 1].Time;
                    var avspeedkmph = 0
                    //check route time is greater than zero, as some itineraries can have zero time and distance
                    if (routetimesecs > 0) {
                        avspeedkmph = items[ii - 1].Distance / (routetimesecs / 360);
                    }
 
 
                    //work out aprox how far along route between this point and the previous
                    //you can get in the remaining time at the average speed
                    var timeleftsecs = (gmins * 60) - (totaltime - items[ii].Time); //time left from previous point
                    var maxdist = (avspeedkmph / 360) * timeleftsecs;
 
                    //work out bearing between previous point and this one
                    var bearing = GetBearingBetweenPoints(items[ii - 1].LatLong, items[ii].LatLong);
 
                    //work out new point along route between previous point and this one
                    //that can be reached in the remaining time
                    var newpoint = GetPointAtDistanceAndBearing(items[ii - 1].LatLong, maxdist, bearing);
                    pointsarray.push(newpoint);
                    break; //exit loop as have maximum point reachable in time given     
                }
            }
        }
        else {
            numroutesnotfound++;
        }
    }
    
    //debug
    if (gdebug) {
        alert("routes not found: " + numroutesnotfound);
        alert("routes found: " + pointsarray.length);
    }
 
    //we have all our maximum points in each direction
    //now draw the polygon representing the points
    var poly = new VEShape(VEShapeType.Polygon, pointsarray);
    poly.HideIcon();
    gmap.AddShape(poly); //add to map
    gmap.SetMapView(pointsarray);
 
    //reset button
    document.getElementById("btnCalc").disabled = false;
    document.getElementById("btnCalc").value = "Calculate";
 
    document.getElementById("progress").innerHTML = "Proccessing complete";  
}

This loops through the routes returned in the groutesarray and first checks if the route has any distance, if it doesn’t we ignore it as is means a route could not be found between the startpoint and a specific “spoke” end point.

Now we loop through each stage of the route (there will only be one route leg) working out how far we can get along the route before the maximum time in minutes (as supplied to GetDrivingDistance as parameter mins)  is exceeded.

Once we find the 2 stages of the route that are either side of the maximum time we calculate the distance between the 2 stages of the route and the average speed along that part of the route (distance travelled / time taken). We can then work out the distance from the first of the 2 route stages to the final position possible to reach in the time given, and the direction of travel in degrees between the 2 stage’s start points. Finally we can work out the position (lat / lon) of this final reachable point by calling the GetPointAtDistanceAndBearing function again (see full code).

Now we have the point which we can reach along the route to the end point of the “spoke” in the given time limit. Finally we add this to an array and draw all the points in the array as a polygon on the map, zooming the map out using SetMapView so we can see the whole polygon.

So does it work?

As you can see in the demo, it does indeed plot a drive time polygon, which improves its accuracy as you increase the number of spokes. You can manually check a location within the polygon to confirm that you can actually reach it within the given time, and most of the time that works.

However there are a few issues…

What doesn’t work?

Well unfortunately there is a lot that doesn’t work:

1) Try centering the map near a costal region, you will see quite often the polygon drawn does not even include the start point. This is either because Virtual Earth used shipping lines to get a route (which head inland first to get to the port), or could not even find a route out to the middle of the sea.

image

2) Even in inland locations if there is not a road even remotely close to the end point of a spoke Virtual Earth will get a route to the nearest road which may be miles away in any direction. To see this for yourself change the gdebug global variable to true and see each spoke end point and each route end point ploted on the map, many of which are far apart.

3) Often the polygons points end up not plotting in order of increasing degrees around the start point mostly due to point 2 above. This can lead to points in the polygon crossing over as below:

image

4) Its SLOOOOOOWWW. As we seem to only be able to call one GetDirections at a time it takes forever to plot a map with a high number of spokes.

5) Exactly how accurate or even useful the results are is certainly questionable

6) It will of course not work if directions are not available in the region the map is covering.

7) Routes to the endpoint of the spoke might go a long way off the angle of the “spoke” to get to the final destination so getting a point along this route is never going to give a really accurate answer to the problem.

How could it be made better?

There are lots of simple ways this could be made to work better including:

1) Using country boundaries to work out when we are asking for a route in the sea and instead ask for a route to the very edge of the coast.

2) Fix the polygon points so they are in order when plotting on the map which should remove the crossing over effect discussed above.

3) Right now we are using the routleg itineraries to plot the data but these are not the exact route points but only the turn by turn points so are not as accurate. With a commercial account we could get to the full route data which might be able to be used to get a more accurate position of the polygons points.

Got an idea to make this better?

Do you have an idea for making this work better? Please add it to the comments below.

download code download code