This project is read-only.

Map.LayerAdded Event and Layer Extents

Nov 18, 2010 at 6:16 PM
Edited Nov 18, 2010 at 7:19 PM


I am listening to the Map.LayerAdded event to detect new layers and zoom to them. When I add a layer that is different than the current map projection - the layer.dataset.projection is the same as the map's projection. But the layer extents still show the coordinates in the layers original coordinates. I need the coordinates to be in the map frame's coordinates. How do I handle this ?

I could reproject the coordinates to the map frame projection if I knew what the layer's original projection is, but that has been changed by the time I get a handle. Please advise.

This seems to be happening only for rasters. With vectors I get the warning that the projection is different.

Thank you,


Nov 18, 2010 at 7:55 PM
If you open the shapefile using:

FeatureSet fs = new FeatureSet("myFilename");
if(fs.Projection != map1.MapFrame.Projection) // Possibly map1.Projection), I forget which

How are you currently adding new layers in the Map? Through the add button on the SpatialToolStrip, or are you using your own button and calling map1.AddLayer() or whatever? Wherever that code is is where I'd check the dataset before sending it to the map, rather than trying to capture events that happen after data has already been added.

This assumes though, that your shapefile has an associated .prj file. When you say "that has been changed by the time I get a handle" it sounds like it is already trying to reproject it to the maps coordinate system, but is not successful. One thing that can cause this is if you have a projection that is incorrectly defined. If for instance, the coordinates are actually UTM, but the .prj file says World, then when the map tries to reproject it, you get completely abnormal results. The way to test this is if it prompts you to reproject during the add, choose "No". Then, check the coordinates. If for some reason it is reading your projection incorrectly, then the coordinates read in may not be on the correct scale for what the .prj file claims. If you have no .prj file at all, this can cause the same problem. You can use the "DefineProjection" Tool to fix this I think. (Haven't tested that lately, but I think that works.)


Nov 18, 2010 at 10:10 PM


Thanks for getting back to me.

I am also using the spatial tool strip in my form. I am adding the layers by clicking the "Add Data" button (looks like a plus sign). Once the layer has been added throught the "Add Data" button the LayerAdded event is fired and that is where I am doing my checks. I guess I could look at the coordinates and if they are out of whack then I could display a message to the user.

Also is there a way to get to the path of a shapefile once its been added to the map using the "Add Data" button?

Thank you very much,