Adding JPG file to a SharpMap

Topics: SharpMap v0.9 / v1.x, Web Controls
Nov 16, 2007 at 2:03 PM
I am new to GIS programming and SharpMap, and trying to find my way through it. In the web example code MapHelper.InitializeMap, I found the map object being loaded with various layers of ShapeFiles, what I would like to do is add a layer of JPG image to the map (version 0.9). How do i do that? Any help will be greatly appreciated.

//Initialize a new map of size 'imagesize'
SharpMap.Map map = new SharpMap.Map(size);

//Set up the countries layer
SharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
layCountries.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\countries.shp"), true);
Developer
Nov 17, 2007 at 11:13 AM
http://www.codeplex.com/SharpMap/Wiki/View.aspx?title=Data%20formats%20supported&referringTitle=Home
using ogrprovider
Coordinator
Nov 17, 2007 at 7:59 PM
Right, since a JPG is a raster (vs. vector) format, you need to use a raster data provider. The OGR data provider gives you this capability. I understand that the setup of the OGR provider can either go swimmingly, or be some trouble, but I don't have enough experience with it to know why it's one or the other. It worked for me the one time I used it, but I know others have had problems setting it up. This situation should improve for SharpMap v2.0.
Nov 20, 2007 at 4:40 PM

codekaizen wrote:
Right, since a JPG is a raster (vs. vector) format, you need to use a raster data provider. The OGR data provider gives you this capability. I understand that the setup of the OGR provider can either go swimmingly, or be some trouble, but I don't have enough experience with it to know why it's one or the other. It worked for me the one time I used it, but I know others have had problems setting it up. This situation should improve for SharpMap v2.0.


Hi,

interested me too :)
Is some prgress with this situation? It's possible to add a picture in "georeferenced" manner, means, one corner is georeferenced and dimensions are also known?

Many thanks.
Mar 31, 2008 at 10:06 PM
Is there a possibility of getting an example code snippet in C# as to how to use the OGR data provider, together with any pre-requisites for this. Would be much appreciated.

Apr 1, 2008 at 1:09 PM


Alex74 wrote:
Hi,

interested me too :)
Is some prgress with this situation? It's possible to add a picture in "georeferenced" manner, means, one corner is georeferenced and dimensions are also known?

Many thanks.


With the current source code, this is not possible. However, I created a patch that handles georeferenced rasters. It can be found here:

http://www.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=SharpMap&DownloadId=24213

This 'patch' is a stand alone version of sharpmap. If you run the DemoWinForm it has some sample data with georeferencing and has a menu option to load your own rasters for display (so you can look at how this is done in code).
Apr 2, 2008 at 10:52 PM
dan3258

Many thanks for your prompt reply.

I was able to render the jpg image using your example. A few questions:

1) will this capability be standard in the next version of SharpMap?
2) I presume that your 'patch' is really an extended version of the standard release, with this additional capability.
3) Can you point me in the direction of how to geo-reference my rendered jpg, if I know the planar coordinates (or lat/lon) of each corner of my image? When I bring it in in your version of DemoWinForm, it seems to come in just in pixels, but I notice that your TIF examples are in some planar coordinate system.

as you no doubt can tell, I am a beginner with SharpMap, so all examples and pointers very much appreciated, but will try to avoid bothering the discussion list as much as I can.

Thanks in advance
Apr 3, 2008 at 1:19 PM


rbmales wrote:
dan3258

Many thanks for your prompt reply.

I was able to render the jpg image using your example. A few questions:

1) will this capability be standard in the next version of SharpMap?
2) I presume that your 'patch' is really an extended version of the standard release, with this additional capability.
3) Can you point me in the direction of how to geo-reference my rendered jpg, if I know the planar coordinates (or lat/lon) of each corner of my image? When I bring it in in your version of DemoWinForm, it seems to come in just in pixels, but I notice that your TIF examples are in some planar coordinate system.

as you no doubt can tell, I am a beginner with SharpMap, so all examples and pointers very much appreciated, but will try to avoid bothering the discussion list as much as I can.

Thanks in advance


1. I believe one of the devs (pauldendulk) is planning on adding a lot of the raster stuff from my patch sometime soon.
2. Yes, it is not really a patch...more of a customized version.
3. To georeference a jpeg, you must use a .jgw file, which contains a 6 parameter transform. More on this can be found in this discussion:

http://lists.osgeo.org/pipermail/mapserver-users/2005-November/012166.html
Developer
Apr 4, 2008 at 2:44 PM
I committed the GdalRasterLayer version of Dan and Joel. It is in the latest version of the trunk. For it to work you need to copy the unmanaged dll's of fwtools 2.2.0 to your bin directory. There is also a Gdal sample included in WinFormSamples. I like to hear if you manage to render the jpg's with it.

I did also commit their changes to BoundingBox.cs en GeoTranform.cs but not the other parts yet.
Apr 4, 2008 at 2:56 PM
Thank you for the information, everything is working well, I appreciate the assistance. Solution for the record:

By including a "world file" with the same name as the .jpg file, but with the "jgw" extension, in the same directory as the jpg file, the following code (derived from the code in the patch provided by Dan) automatically geo-references to the coordinate of the world file. That is, it is NOT necessary to separately specify the name of the world file in code.


SharpMap.Layers.GdalRasterLayer layer;
OpenFileDialog dialog = new OpenFileDialog();

if (dialog.ShowDialog() != DialogResult.OK)
return;

try
{
layer = new SharpMap.Layers.GdalRasterLayer(dialog.FileName, dialog.FileName);

myMap.Layers.Add(layer);
myMap.ZoomToExtents();
//Render the map
System.Drawing.Image imgMap = myMap.GetMap();
pictureBox1.Image = imgMap;
pictureBox1.Refresh();
}
catch (Exception error)
{
MessageBox.Show("There was a problem loading raster.\n\n" + error.Message);
}

On the issue of testing the latest version of the trunk (post by Paul), forgive me because I am somewhat new in this world, but is this reflected in the 0.9 binaries, or only in the source code tree, and is that build for VS 2005 or VS 2008? If not in the binaries, then is a release in the binary form expected at some point in the future?

Thanks for all the help, this stuff is great.

Dick
Developer
Apr 4, 2008 at 3:09 PM
Thanks for your feedback.

The new commit is only code, no binaries. There will probably be a binary release before we all switch to v2. Both the vs2005 en vs2008 solutions have been updated.
Apr 8, 2008 at 9:55 PM
if you will permit me another question:

I have added the map toolbar to my application, trying to follow the example in DemoWinForm. Most of the toolbar buttons work fine directly, but SelectedZooming (zoom rectangle) throws an "out of memory". Zoom In, Zoom Out, Zoom Extents, One to One, Previous Extent, Next Extent, Pan all work.

SelectedZooming throws the error in MapBox.cs at line:
Bitmap patch = m_DragImage.Clone(pe.ClipRectangle, System.Drawing.Imaging.PixelFormat.DontCare);
in OnPaint. My system has 2 GB of ram.

not critical, but I would like to have this feature if possible.

thanks in advance.

Dick