GDAL Layer only showing zero coordinates on OpenLayers via WMS

Topics: Web Controls
Mar 24, 2015 at 5:20 PM
Hi guys,

I am working on a web application that is able to show Geographical data from different sources. Because I want it to be flexible and easy to adjust I would like to have a custom WMS server in combination with OpenLayers (for now). I have downloaded Sharpmap, GDAL, FWtools and Dotspatial. I installed it and used the WMS example as starting point. Because the data can have any projection I am using the Dotspatial library.

I managed to get it working for ShapeFiles. They are reprojected on the fly and are shown in OpenLayers. It is awesome :)

I really would like to have it working for raster files as well. Unfortunately I did not yet manage to do so. I am kind of stuck. After solving all FwTools/GDAL references I was able to show a raster at coordinates (0, 0) somewhere next to Africa. If I reproject the layer it is still only showing 4 cells at these coordinates. The weird thing is that I can retrieve the right attribute information from anywhere on the map. In other words: the map is reprojected correctly but it does only show 4 cells at the centre of the map.

I am using the following code to create the GDAL layer and to add it to the map:
 if (System.IO.Path.GetExtension(path) == ".tif")
           {

               ProjectionInfo proj = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;

               SharpMap.Layers.GdalRasterLayer rlay = new GdalRasterLayer("raster", path)
               {
                 SRID = 4326,
                 TargetSRID = 900913,
                 CoordinateTransformation = ProjHelper.LatLonToGoogle(proj)
               };

               minx = rlay.Envelope.MinX;
               miny = rlay.Envelope.MinY;
               maxx = rlay.Envelope.MaxX;
               maxy = rlay.Envelope.MaxY;

               Color c = Color.FromArgb(150, 100, 100, 100);
               Brush b = new SolidBrush(c);
               vlay.Style.Fill = b;
               vlay.Style.Outline = System.Drawing.Pens.Black;
               vlay.Style.EnableOutline = true;
               map.Layers.Add(vlay);
               map.Layers.Add(rlay);

           }
where ProjHelper is:
public static DotSpatial.Projections.ICoordinateTransformation LatLonToGoogle(DotSpatial.Projections.ProjectionInfo proj)
        {
                       
            DotSpatial.Projections.ProjectionInfo mercator = DotSpatial.Projections.ProjectionInfo.FromEpsgCode(3857);           
            DotSpatial.Projections.CoordinateTransformation transform = new DotSpatial.Projections.CoordinateTransformation();
            transform.Source = proj;
            transform.Target = mercator;
            return transform;
        }
The javascript to load the OSM layer:
$(document).ready(function () {
    var options, init;

    OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;
    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
    OpenLayers.Util.onImageLoadErrorColor = 'transparent';
    OpenLayers.Util.onImageLoadError = function() {
        this.src = '/Content/Images/sorry.jpg';
        this.style.backgroundColor = OpenLayers.Util.onImageLoadErrorColor;
    };

    options = {
        wms: 'WMS',
        wmslayers: ['shapefile', 'raster'].join(),
        controls: [],
        projection: 'EPSG:900913',
        displayProjection: 'EPSG:4326',
        format: 'image/png',
        wmsparams: {
            'MAP_TYPE': 'SPH'
        }
    };

   }
I am still learning C#/ Javascript, so it could be that I have missed something obvious. Does one of you guys have any idea what the problem could be? I would really appreciate your help.

Many thanks in advance,

Kas
Developer
Mar 25, 2015 at 7:33 AM
As far as I know, almost nothing, so sorry if I say something wrong, raster cannot be reprojected on the fly, and for sure it's a heavy task.
So my suggest is to reproject your data to 900913 with GDAL-Warp and then use the reprojected data inside sharpmap.
Mar 25, 2015 at 7:59 AM
Hi D_Guidi,

Thanks for your reply. Actually I thought it would be possible because of this thread: http://sharpmap.codeplex.com/discussions/286555.
I am going to try your suggestion. The problem is that I can and should not change the original files too much.
Developer
Mar 25, 2015 at 8:01 AM
Edited Mar 25, 2015 at 8:02 AM
knoedelk wrote:
Hi D_Guidi,
The problem is that I can and should not change the original files too much.
GDAL WARP should make reprojected copies of your original files.
Mar 25, 2015 at 8:24 AM
Ok, thanks! I will try.
Coordinator
Mar 25, 2015 at 12:06 PM
Edited Mar 25, 2015 at 12:11 PM
If you are using a fairly recent version of SharpMap (1.0/1.1), get rid of FWTools for your project.
GDAL/OGR is taken from a nuget package that places the relevant libraries, data files in the bin\gdal folder. All necessary paths are adjusted to that location. Having FWTools in your "PATH" variable might spoil that automatism.
The fact that your rasters show up at the origin (0, 0) makes me think if they are spatially referenced at all. What does gdalinfo tell you about your raster.
If you have meaningful values, I suppose the gdal_data folder is not included correctly and thus the coordinate transformation not set up.

Nonetheless, reprojecting rasters on the fly is not the way to go.
Mar 25, 2015 at 12:08 PM
It kind of works!

I used GDAL to warp the data into a temporary file. This file is used as WMS layer without reprojection. The image is correctly showed in OpenLayers.
Gdal.AllRegister();
Dataset ra = Gdal.Open(path, Access.GA_ReadOnly);
string refone = ra.GetProjection();
string refnew = "PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],EXTENSION[\"PROJ4\",\"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs\"],AUTHORITY[\"EPSG\",\"3857\"]]";

Dataset warp = Gdal.AutoCreateWarpedVRT(ra, refone, refnew, ResampleAlg.GRA_NearestNeighbour, 0.125);

OSGeo.GDAL.Driver dout = Gdal.GetDriverByName("GTiff");

Dataset ra_temp = dout.CreateCopy("temp.tif", warp, 0, null, null, "");

ShpPath = "temp.tif";
The only disadvantage is that GDAL is using Proj4 and Proj4 only supports a limited number of projections.
Mar 25, 2015 at 12:11 PM
Edited Mar 25, 2015 at 12:11 PM
Thanks for the information FObermaier.

I downloaded the latest version 2 weeks ago, so it should be recent enough. I will update all my references, remove FWTools as PATH variable and try again.
Apr 24, 2015 at 12:56 PM
Sorry for the late reply.

I updated everything and migrated from the trunk version to version 1.0. The results were exactly the same.
In my case it does not really matter because I think that the solution of D_Guidi is better. Reprojection on the fly demands too much of the server and makes the application really slow. I did not try it on a desktop application.