Archive for the ‘GIS’ category

Getting image of GIS map

April 17th, 2012

Have you been racking your brain trying to get an image from your map and have just about given up?  I have went through several GIS libraries including OpenLayers, ThinkGEO and some others and finally came across SharpMap which is an open-source .net library that does geo-spatial imagery and it just so happens to have a very nice little Image export routine that does the job and does it simple, here is all the code you need to generate a map from SharpMap:

//code to create Lat and Lon points
var list = new List<KeyValuePair<double, double>>();
 samplePoints.ForEach(delegate(ScoutSample sample)
 {
      list.Add(new KeyValuePair<double, double>(Latitude,Longitude));
});

var layer = new VectorLayer("Field Layer", provider);
layer.Style.Fill = new SolidBrush(Color.Green);
layer.Style.Outline = Pens.Black;
layer.Style.EnableOutline = true;
            
var map = new SharpMap.Map(new Size(250, 250));

//code to add the points
foreach (var pt in points)
{
     //Add a single Point
     var geomColl = new Collection<Geometry>();
     var vLayer = new VectorLayer("GeometryLayer");
     var point = new SharpMap.Geometries.Point(pt.Key, pt.Value);
     geomColl.Add(point);
     vLayer.DataSource = new GeometryProvider(geomColl);
     map.Layers.Add(vLayer);
}

map.Layers.Add(layer);            
map.BackColor = Color.White;
map.ZoomToExtents();
var image = map.GetMap() as Bitmap;

Of course I am not showing you my provider at the moment, I am still fine-tuning the code and I will update this code at a later time, but this is just so easy, just instantiate the map and then call GetMap(), that is how it should be done. There isn’t any methods in OpenLayers that I could see and ThinkGeo has to have a control loaded before you can run the GetMap, which is garbage.