ASP.NET xDomainProxy for OpenLayers getInfo requests

After a long time of just talking about using geoserver we have eventually installed it on our server. Making it work behind IIS is a subject for another article and I am hoping to post it soon.

Anyway, we decided to make our geoserver available at geoserver.cartoninjas.net, while the applications we write are likely to be hosted under different subdomains or even under different domains. Since x-domain requests are not allowed in JavaScript due to some security restrictions I needed to create a simple server side proxy that would be exposed to application as a local resource and would take care of pulling the info when a getInfo requests are issued by OpenLayers.

I started with setting up a proxyHost in OpenLayers:

//proxy host
OpenLayers.ProxyHost = 'xDomainProxy.ashx?url=';

Then a server side script for a generic handler:

<%@ WebHandler Language="C#" Class="xDomainProxy" %>

using System;
using System.Web;

public class xDomainProxy : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        //OpenLayers.ProxyHost = 'xdomainProxy.aspx?url=' so the requested url is passed in a url param
        string requestUrl = HttpUtility.UrlDecode(context.Request.QueryString["url"]);

        //test if the request string was passed and of so request data from the destination server
        if (requestUrl != null)
        {
            //create a new HttpWebRequest
            System.Net.HttpWebRequest webRequest;
            webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(requestUrl);
            webRequest.Method = "GET";

            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)webRequest.GetResponse();

            //check if the data was successfully retrieved
            if (response.StatusCode.ToString().ToLower() == "ok")
            {
                //set the appropriate response content type
                context.Response.ContentType = response.ContentType;

                //Read the stream associated with the response.
                System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());

                //and write it to response
                context.Response.Write(reader.ReadToEnd());
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

And voila, it's ready to be used ;)

A side note: although in many cases this proxy will work properly this is a mini version of the code and should not be implemented in the production environment - it does not catch any errors and will fail if the connection is not available. Also it was written to work with OpenLayers specifically (OpenLayers.ProxyHost) and needs some extra work before it can act as a bit more flexible proxy allowing one to pull data without having to escape the passed url.

Running GeoServer on Windows 7 (x32 or x64) and IIS 7.5

 

GeoServer is one of the most powerful and rapidly evolving geo-server, capable of serving geographical data in-line with standards developed by Open Geospatial Consortium (OCG).

More information about GeoServer can be found on www.geoserver.org.
What is so attractive in GeoServer?

  • it is Open Source software
  • follows OGC standards
  • functionality hugely improves with every new relase
  • it’s fast and scalable
  • it’s free

GeoServer is written in Java and requires Java Virtual Machine to run on. This can be advantageous as it is operating system independent in sense that it will run on any operating system supporting Java Virtual Machine architecture.

Still, most widely used operating system is Microsoft Windows OS.
And many existing applications and geo systems are required to run from Microsoft IIS (for e.g. because applications require .NET environment, that is exactly what Manifold needs).

So our situation:

Application requires IIS and GeoServer requires Java and Java based Web Server like Apache, Jetty etc.

Possible solutions:
1)Keep them separate.
    Install IIS on one server and Apache or Jetty with GeoServer on different machine (can be virtual).
Probably you will need kind of proxy tool especially if you want to be able to exchange data in different format than images.

2)Make your Apache default server.
Make your Apache default server for e.g. running on port 80 and install IIS on different port for e.g. 8080.
Then you can redirect calls using Apache configuration, this might not be loved by application developers developing .NET web applications

Web Client <-> Apache/Jetty (GeoServer) <-> IIS

3)Install IIS and make it your default Web Server.
Make IIS default Web Server (for e.g.  on port 80) and add Apache/Jetty and GeoServer as secondary web server
   
    Web Client <-> IIS <-> Apache/Jetty (GeoServer)

I think the most popular and easiest to set up is 3rd option.
And here is how this can be done.

The key for this is use of Microsoft “URL Redirection” IIS plug-in.

Here I assume that you have:
Windows 7 Operating System with IIS 7.5 running on port 80
you have downloaded and installed Microsoft URL Redirection plug-in

 

 

Apache with GeoServer or standalone GeoServer distribution running on Jetty using port 8080 so direct call to GeoServer instance can be made as below:
http://localhost:8080/geoserver/web/

Where we want to be:

Client call hits IIS first, and then request is passed to URL Rewriter which checks URL for rules we are going to set up. And if fits in rules request is processed by IIS or is sent to Apache/Jetty

Steps to follow:

1)from IIS Manager select “Default Web Site”
2)start “URL Rewrite” application
3)go to “Actions” and select “Add Rule(s)...”

we will use “Blank rule”

populate rest fields as shown below

Don't worry about “Conditions” - there are from my development environment.
Further down you should set fields like below:


“Action” section is particularly important.
So:
Action type: Rewrite
Action Properties:
    Rewrite URL: http://localhost:8080/{R:1}   <- this could be {R:0} read more from URL Rewrite docs
    Append query string: ticked
    Log rewritten URL: un-ticked

“Rewrite URL” is an “url” pointing to your Web Server hosting GeoServer including port

So we have now IIS redirecting every call to http://localhost:8080/  (which in my case is Apache)

Now we want IIS to process every call having certain string in URL request.
In other words we need to build list of redirection exceptions.
Exceptions will be our web applications.

So if we want to add new application/web site to IIS, simply go to “Default Web Site”, and add application. For e.g. our test application is latest GeoExt library, as we want to check examples
http://localhost/GeoExt-1.0

Using IIS Manager
create new application called GeoExt-1.0
select “Default Web Site” from IIS Manager
open “URL Rewrite” application
select redirection rule “GeoServer Redirect” in my example
right-top – Condition -> Add...
make sure “Check if input string:” is set to “Does Not Match the Pattern”, so every thing that is different to “/geoext-1.0” (ignore case) will be passed to Apache and if it has  “/geoext-1.0” as in url request string will be processed by IIS

populate fields like shown below:

 

So now we can call our application like:

 

Main advantage is that we can call GeoServer installed on same server as IIS
And we can call our application:
http://localhost/myApp
and GeoServer
http://localhost/geoserver/web

So we shouldn’t have “same origin” policy rule violated. This is extremely important when exchange data in  formats like XML, JSON, JavaScript and so on.

We still can use full functionality of both, even having IIS running on port 80 and Apache/Jetty on port 8080.
And we don’t need to provide port for this. As per screenshot below, test GeoExt WMS-Capabilities from examples directory where I have changed url to data from “data/wms.json” to http://localhost/geoserver/ows?service=wms&version=1.1.1&request=Getcapabilities

 

 

Hope this helps

Around the world on a motorbike

For the last 3 years I have been almost exclusively providing cartographic services for Bezdroza, one of the largest tourist guide publishers in Poland. Bezdroza has a distinguished protfolio of books and therefore the maps they publish may vary a lot, although they are usually rather guide-like maps like:

This time I was invited as a cartographer to take a part in a project that appeared to be quite unusual - I was to design maps for a book of travel. Nothing unusual so far, rigth? The unusual part was that the book was not to have any maps inside, the maps were supposed to make the cover of the book.

The book tells a story of two journalists that decided to travel around the world on their motorbikes. When they agreed it would be a great fun, they did not have their motorbike driving licenses nor they could ride the bikes. This made me think that it should be fun for me too to design such a cover for this book.

The data came from the Natural Earth data repository and from the GPS tracks supplied by the authors of the book.
For the front cover map I used the the 1:100M data set and generalised GPS data; for the back cover both source datasets were generalised even further.

When designing the 17 globe maps for the back cover I was tempted to script the data preparation process (prepare the data, center and project it, and finally clip it) though after all I took the quicker path and decided to create them manually. Maybe next time, when we are about to design some globe maps for the folks that visited more than 20 countries ;-)

Designing these maps was an enjoyable experience, hopefully you'll like them too.

Front cover:

Back cover:

Both covers:

Higher res files:

Front cover (3,11 mb)

Back cover (2,13 mb)

Both covers (6,14 mb)

A generic error occurred in GDI+

Recently I have been working on a tile serving utility that would generate tiles on the fly but also cache them at the same time for future usage. After releasing our map tiling tool for manifold this was the next step.

The tile rendering functions were working nicely and the process was fairly quick so enabling data caching functionality could only speed things up ;-) So far so good... It was supposed to be just a matter of saving the output bitmap to a file... So I did it the way I usually do and tried to save my tile this way:

mapImageBitmap.Save(path, _outputTileFormat);

 Apparently this was throwing an error. A very descriptive one: A generic error occurred in GDI+... Not very helpful, is it?

After googling for a while it looked like this was supposed to be a permissions problem but allowing my IUSR to write to the specified folder did not help at all. What's worse I have found some info on the msdn that one should avoid using the System.Drawing namespace in ASP.NET: Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

Nice huh?

Another solution I found on the web was to clone the bitmap in question and then save it, though that gave the same error. No avail in my case.

Luckily after messing with the problem a bit more I have discovered that writing a bitmap to a memory stream and then saving the data using Sytem.IO.File.WriteAllBytes did the trick:

System.IO.MemoryStream outStream = new System.IO.MemoryStream();
mapImageBitmap.Save(outStream, _outputTileFormat);
System.IO.File.WriteAllBytes(_requestedTilePath, outStream.ToArray());

Defitions of Polish projections for manifold

With version 8.0.19 manifold team added some of the Polish projections to manifold projection presets. Basically they added five zones of the National Coordinate System 1965 (PUWG 1965, zones I, II, III, IV and V). There seems to be a mistake though with the way they named the projections: National Coordinate System 1965 was named 1942 while the only one that has 5 zones for Poland is 1965.

For those interested in using Polish coordinate systems with manifold attached is a custom projection xml (it will display in a folder Poland_Custom in the assign / change projection dialog). This is the best we came up with so far so please bear in mind that it may not be 100% perfect. For some more details please see a discussion at georeference.org.