måndag 22 januari 2018

Spatial Server Tools?

Over time Microsoft has a history of developing mapping services and applications. It spans from Encarta, with map tours, to lately announced Location based services on Azure. MSFT took the path through various online services such as MapPoint, Virtual Earth and Bing Maps.

I believe over time Microsoft have developed suite tools for handling GIS and spatial information (otherwise they would not deliver the services they have). One thing I found a bit odd is the current Bing Maps client is really powerful with the Spatial Math library in combination with services and the new routing API. But there are not so many tools on the server. Of course there is SQL Server and the library Microsoft.SqlServer.Types. Developing server side GIS with .Net you will probably end up with ESRI or NetTopology Suite (NTS), a powerful open source library).

But I believe there is room and a need for yet another library. For example:
  • Test geometry generator, create test geometries
  • IO, various formats.
  • Spatial Math
  • Geometry compression
Above are all low hanging fruit. In fact all of them are in Bing Maps client.

What if just the functions in Bing Maps client would also be available server side? That would include all four points above. Or what if just the Spatial Math library would be available as a NPM package? Or even a Nuget package and expand from that? 

torsdag 18 januari 2018

Drawing a donut in Bing Maps


Polygons with holes aka donuts have been a challange in web mapping. For not too long ago when customers asked if it is possible to have holes in polygons, I was avoiding the question. It is possible to send an array of rings to the polygon constructor. But within Spatial math library it is possible to achieve holes in polygons pretty easy. If a user draw a polygon on top of a polygon on the same layer, it might mean a hole.


  1. Create a polygon. The easiest way I know for this use case is to make a circle. 
  2. Create another smaller polygon, a polygon that I use to punch out the hole on the bigger polygon.
  3. Use the difference function in Spatial Math library to punch out the polygon.
  4. Add the donut polygon to the map. Done. 


Assuming there is an instantiated Bing Maps variable named map...
 Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', () =>; {
    const outer = Microsoft.Maps.SpatialMath.getRegularPolygon(map.getCenter(),
                  30, 36, Microsoft.Maps.SpatialMath.DistanceUnits.Kilometers);

    const outerRing = new Microsoft.Maps.Polygon(outer);

    const  inner = Microsoft.Maps.SpatialMath.getRegularPolygon(map.getCenter(), 
                   10, 36, Microsoft.Maps.SpatialMath.DistanceUnits.Kilometers);

    const innerRing = new Microsoft.Maps.Polygon(inner);
    const donut = Microsoft.Maps.SpatialMath.Geometry.difference(outerRing, 


It is surprisingly easy to accomplish holes in polygons - and drawing circles. It is about ten lines of code depending on how you count it. Inside the module, it is in fact six lines that acctually do something.

It is powerful, easy to use and it solves reall world problems, like drawing islands on a lake. Or lakes on islands. 

fredag 20 oktober 2017

What does geometry.toString() mean?


In web development with JavaScript or similar, what is the meaning of geometry.toString()? I was developing a small application and started to think may be it is time to do something more interesting than [object] or Uncaught Not implemented. What if toString() would return the actual string representation of the object? That would make toString() useful.

Since JSON is the natural representation of the object would it  make sense to simple return a stringified GeoJson? I think so. For polygon the implementation would look something as:

Microsoft.Maps.Polygon.prototype.toString = function () {
    var _json = Microsoft.Maps.GeoJson.write(this);
    return JSON.stringify(_json);

Does it make sense? At least it does for me since I can just write geom.toString() when I need to communicate with the server.

Of course there are alternatives to GeoJSON, WKT, well known text for example. I think it would be elegant to just write geometry.toWKT(); Adding it to the module would make sense. While waiting for an update, adding it when the modules callback would look something like:

Microsoft.Maps.loadModule('Microsoft.Maps.WellKnownText', function(){
    Microsoft.Maps.Polygon.prototype.toWKT = function () {
        var wkt = Microsoft.Maps.WellKnownText.write(this);
        return wkt;

What if I could add this suggestions to GitHub issues for modules?

Friday afternoon geo dev thoughts.

Small example implementation; https://github.com/perfahlen/toString

fredag 8 september 2017

Cognitive routing

A bit background

The last couple of months I have checked out and tried Microsofts preview advanced routing projects. First when it comes to routing it is important to understand there is no rights or wrong. What is the best route? For some it might be:
  • The shortest route?
  • The fastest route?
Those are easy to find. For others they might be?
  • A route with minimun slope?
  • A route with rest areas with parking facilities?
  • The most beautiful route?
  • ... and so on.
So, the best route can be different depending on the purpose of the trip, vehicle and different regulations. So I always keep that in mind when it comes to routing.


So is it cognitive as the URL implies? When I am in the middle of development, it is algorithms and algorithms with predicates. Combining this new routing services with advanced client tools is for sure a powerful tool. Adding historic traffic data to predict calculate routes, isochrones and solving TSP is calculating complicated stuff in short period of time. So the rise of cognitive GIS is coming.

It is at least really powerful and can solve complex problem and will be useful for many organisations.

Down the road

In order to understand what is next I look back to be able to connect the dots ahead. Isochrones is a natural derivat of routing. Although I guess the challenge with an algorithm that is heavily heuristic (which I assume Bing Maps are) is to polish away the heuristic but still be effective to accomplish isochrones. Next in this area would be to calculate things that is n minutes/distances away, but not closer than n unites from a given point as I describes in this post. Another natural evolvement is travelling salesman problem. Natural to integrate into the service module in CRM and may be a Geo-Calendar is on its way from Bellevue/Redmond.  

What to expect?

From a technical perspective I guess this new services will be available in a C# API like the Bing Maps REST Toolkit. That feels like a safe bet. On the client side I would except modules for routing, including the new services.

Another thing I would like to see is that Microsoft truly adapts GeoJson as in every time geographical data is send as JSON - I expect GeoJson.

Moreover, I hope the new services will cover at least Europe as well. If it scales in North America, the hard job with scaling is probably already done.

Thanks to Fredrik Jonsson for illustration.

torsdag 31 augusti 2017

Playing with project Abu Dhabi

Abu Dhabi is yet another interesting preview project from Bellevue addressing Travelling Salesmans Problem.

I have calculated and bid on several quotes regarding different scenarios where one of the cornerstone and foundation were based on different routing algorithms, such as Travelling Salesman Problem (TSP). All kinds of businesses such as transporting kids to school, service staffing serving restaurant machines, telecom service staff, elderly care and so on. The characteristic of these projects where expensive, complicated with none or small amount of guarantee that the project would succeed within budget. The organizations that demanded these functions where small- and mid-size organizations. Many of the above projects dropped due to lack of funding. I believe vendors such as Microsoft play an important role providing these kind of organizations state of the art technology to enable them to be more efficient and maybe save a bit of the environment.

From a business perspective, it is easy to find applications within business applications such as Dynamics CRM. Or why not a geo-caching app within a city in combination with gamification. Well, may be geofication - Pokemon Go have been pretty successful.

But for me, in my life may be if the project is extended to cover Europe as well, Abu Dhabi might solve another problem - my weekends. A big part of my weekends is driving family, getting things from here and there and occasionally to football- (soccer) and hockey games. In between I need to eat and sometimes have time to see my friends. Unfortunately for me, at the moment Abu Dhabi is limited to US.

After playing a couple of hours with the API I have a couple of things running. It is pretty straight forward to query the API with HTTP requests. All of these extending routing projects from Microsoft will hopefully be backed up with an API that builds up the actual requst. And also a module to handle the actual result.

More information can be found here: https://labs.cognitive.microsoft.com/en-us/Project-Abu-Dhabi/documentation

måndag 7 augusti 2017

How far can my electric car take me?

Routing- and module pearls in Bing Maps

Bing maps v8 has been released quite some time now. At a first glance, the API offering what to expect. There are a few things that I would like to have seen as part of the core API.  For example, GeoJSON as it is more or less the industry standard transferring GIS data over network.  However, the sweet juicy bits are in modules and are available whenever needed. Be aware it is not JavaScript modules. The modules spans from visualization, parsing, digitizing to analyze. Some modules, such as GeoJSON, might over time be part of the core API. Adding GeoJSON parsing would indeed make sense. For a complete list of modules see https://msdn.microsoft.com/en-us/library/mt712663.aspx.

Microsoft.Maps.SpatialMath is a truly competent module. That module adds complex spatial calculations and analysis. And yet there is more, combining this module with projects like Nanjing opens up for interesting applications.  For example, how far can I get with my electric car? Or, consider truck drivers, at least in Sweden, they are allowed to drive for a limit of time without having a break. Let’s say 2 hours, then they need to have a break for 15 minutes or so, then they can continue. I don’t know the exact limits. The industry definitely has a need to plan the route with stops accordingly to regulations. For example they might need to find a stop along the route between 90-120 minutes.

To resolve the use case above one way could be:

  1. Calculate the route from A to B.
  2. Calculate Isochrones from start to 120 minutes.
  3. Calculate Isochrones from start to 90 minutes.
  4. Use Microsoft.Maps.SpatialMath.difference operation to resolve the area between 90-120 minutes.
  5. Buffer the area, Microsoft.Maps.SpatialMath.buffer
  6. Use the buffered area and Microsoft.Maps.SpatialMath.intersection operation to calculate the route that is between 90-120 minutes (intersection)
  7. Visualize the calculated (intersected) area.

In these steps, a pretty complex problem has been addressed. From the routing-service I got the isochrones, then it is easy to combine the isochrones with geometry math operations. The operations I used is:
  • Intersection (to see where the buffer overlaps the difference area.
  • Buffer (for better visualization of the located area)
  • Difference (between the isochrones)

Now it is easy to find a spot where the driver must find a place to stop. Remember, from the beginning this is not a trivial problem, by combining powerful geodataservices, client-side operations and knowing how to solve the pieces in the puzzle makes it is easy to finish.

I made a demo application to this post. https://github.com/perfahlen/BingMaps-Routing-And-Module-Pearls

måndag 17 juli 2017

Extended geospatial routing service in Bing Maps?

Sometimes GIS are complicated. Especially when it comes to access to data. I remember when I was building a dynamic routing engine based on PostGIS and data from Navteq. The dynamic part consisted of different vehicles behaving different in traffic. For example a truck or bus doesn't behave in the same way as a motorcycle or car. And moreover there can be obstacles, for example height and weight on bridges or the curve is too narrow. Most of the routing engines today are fast - not dynamic, or dynamic and less fast. 

However, after discussed with a good friend about the secret keys to routing, using most of networks SQL indexes skills, adding some routing tricks we manage to make it pretty fast and dynamic, for the time being (2011). But it was hard to make it scale. Routing is CPU heavy and there are no rights and wrongs when it comes to routing. For example, the best route can be the shortest, fastest, safest or maybe most beautiful! 


During that time I also played with isochrones. An isochrone is the routing extent, for example 50 km from a given point. Not just a circle. In every case 50 km radius covering a bigger area than the corresponding isochrone. I have seen many municipalities using radius for example for the fire department. However, project Nanjing addresses that issue.

Radius and Isochrone showing approximately 60 km from
Sundsvall, Sweden
Projects as Nanjing are important making complicated use cases available for the public. Many organizations doesn't have the knowledge, fundings etc to pull this off. Using real distances, or time to calculate coverage or use it for further processing. I hope many other data providers with API:s will consider taking a GeoJSON as an argument. In that way, it will be possible to from a GeoJSON as one criteria among others. Project Wollongong from Microsoft probably makes that behind the scenes. 

After trying the Nanjing API for the first time I was a bit surprised. I expected a GeoJSON as response, but it was a JSON with geographical information. It is not a big thing and it is easy to serialize - but I was surprised. My experience is to use standards whenever it possible. And in this case sending geographical information as JSON I use GeoJSON. The projects API is pretty straight forward to use and there are examples in a handful of languages.

However project Nanjing as a really new cool useful geospatial feature available for preview as today. I hope it will be part of the Bing Maps platform since it address an important useful feature.

There are several others project available for preview and a presentation on channel9.