Select to view content in your preferred language

add a shapefile to a layer package

1011
3
10-10-2011 06:32 AM
AndreaFlesca
Emerging Contributor
Hi everibody,
I need to add a shapefile to my map (in ArcEngine) by using the AddShapefile method.
But I'd like to have my personal thematic settings for this shapefile.
Maybe I could use a layer file or layer package with my favourite thematism pre-setted.
Well , so I have to load the shapefile and connect it in some way, to the desired layer file or layer package.
Is this possible? How? Could anyone give me an example?

Forgive my limited communication capabilities, I'm not a native english speaker.
Thanks.
0 Kudos
3 Replies
ScottKutz
Emerging Contributor
Andrea,

  If you are able to define the symbology for the feature layer loaded from your shapefile in ArcMap and save it in a layer file (*.lyr), then the *.lyr file itself will persist the thematic renderer you have set up.

  The *.lyr file also persists a reference to the shapefile itself. Depending on the options you set up in ArcMap (Document properties, if I recall correctly), the path data to your shapefile will be saved in the *.lyr file either as an absolute path or as a relative path. (If you open the *.lyr file with NotePad, it will be pretty garbled ... but usually you can at least make some sense out of the path to the shapefile ... at least well enough to determine if it is saved as an absolute or relative path.)

  To re-use that *.lyr file and load it into the MapControl ActiveX component (including restoring the layer's thematic renderer properties), your ArcEngine application will then shift its focus to restoring the feature layer from a *.lyr file (vs. the more traditional focus of loading a shapefile).

  Here is a documentation reference to the MapControl method that can be used to load the *.lyr file. Note that it is the AddLayerFromFile method that deciphers the information inside the *.lyr file. Your application just needs to pass it the full path name to the *.lyr file.

- AddLayerFromFile Method
Loads a layer file and adds it to the Map's collection of layers at the specified index position.
http://help.arcgis.com/en/sdk/10.0/arcobjects_cpp/componenthelp/index.html#//00060000020m000000

I don't have any experience with using Layer Packages from ArcEngine, but here is a documentation link.
Working with packages in ArcGIS Engine
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Sample_Working_with_pac...

The documentation indicates this capability to programmatically open Layer Packages was new with ArcGIS 10.0, SP1. It also notes that only the first layer in a Layer Package will be loaded.

My take on that was - it seems more straightforward to make use of a *.lyr file (if possible) because there it is clear which layer will be loaded.

  I hope this information is of use.

Scott
0 Kudos
AndreaFlesca
Emerging Contributor
Thank you scott.
Your suggestion is very helpful.
But I need another piece of information.
I open the layer properties (rightclick on the layer and then select Properties), I can see the shapefile referred (or linked) but I just cannot change the path.
Indeed, I create the layer file into a directory, but then I need to move it into another path.

In general, is there a way to manipulate the layer file, like a script or something different from ArcMAP?

Thenk you in advance,
Andrea
0 Kudos
ScottKutz
Emerging Contributor
Andrea,

  Sorry for the delay in responding. For some reason, the forums do not seem to send out an e-mail notification even though I subscribed to this item.

  I'm not sure if I understand your question correctly, so let me respond in two different contexts.

1. Programmatic access to a layer file

The ILayerFile interface is provided in ArcObjects to interact with a layer file. A link to the help topic is shown below.

ILayerFile Interface
http://help.arcgis.com/en/sdk/10.0/arcobjects_cpp/componenthelp/index.html#//000500000744000000

To the best I can tell, there really isn't a concept of updating just the path to the layer's underlying data source. The following method is used to perform a complete replace of the layer that is referenced by any given *.lyr file.

ILayerFile.ReplaceContents Method
Replace the contents of the layer file.

2. Capturing "relative path" information in a *.lyr file.

I'm guessing this may be the more meaningful approach for you. By storing a relative path in the *.lyr file, you are able to physically move the shapefile -and- the *.lyr file to any other directory, and the *.lyr file will still successfully point to the shapefile (even though the shapefile is in a different directory than it was at the time the *.lyr file was originally created).

To the best I can tell (again by peeking inside the *.lyr file with NotePad), this is achieved by ArcObjects storing the shapefile name with the string ".\" as the path name, which is a reference to the current directory.

How to achieve storing relative paths in a *.lyr file.

2.a From inside ArcMap 10.0 (a similar concept existed in 9.3, but the dialogs were slightly different).

File > Map Document Properties > Pathnames:
Check the checkbox labeled "Store relative pathnames to data sources"

Then, when you use the Save as Layer File RMC menu option on any given layer, the path to the underlying data source (such as a shapefile) will include ".\" as its path name in the *.lyr file.

2.b Programmatically in ArcObjects

The code fragment below (Visual C++) shows an approach for saving a *.lyr file with relative path names. (All error and NULL checking has been removed to make it easier to read.)

ILayerPtr          pLayer <-- provided as calling parameter
CString            csLyrFileName <-- provided as calling parameter

ILayerFilePtr                     pLayerFile = NULL;
IDataLayer2Ptr                    pDataLayer2 = NULL;

HRESULT                           hr = S_OK;

// Create the Layer File object class instance
pLayerFile.CreateInstance(CLSID_LayerFile);

// QI to the IDataLayer2 interface
pDataLayer2 = pLayer;

// Set the property that will result in a ".\" entry in the
// saved layer file to support relative path capabilities
// when re-loading the layer from the *.lyr file.
// Testing in conjunction with Esri Incident #928311 shows that  
// this approach of assigning the full *.lyr file name
// into the ->put_RelativeBase() is the technique to achieve the
// intended "relative path" behavior.
// Note: This whole concept of saving the *.lyr file with relative paths
//       is only applicable if the *.lyr file is saved into the same directory
//       as the actual data source (such as the shapefile or the personal geodatabase).
//       If the *.lyr file is saved into a different directory, then it is not
//       possible to save a relative path.
hr = pDataLayer2->put_RelativeBase(CComBSTR(csLyrFileName));

// Create a new layer file
hr = pLayerFile->New(CComBSTR(csLyrFileName));

// Attach the layer file with the actual layer
hr = pLayerFile->ReplaceContents(pLayer);

// Save the layer file
hr = pLayerFile->Save();



I believe that saving the *.lyr file with a relative path name to your shapefile is an approach that will let the *.lyr file continue to successfully load the layer, regardless of where you move the shapefile (remembering, of course, that the *.lyr file must always be moved to the same directory as the shapefile).

Scott
0 Kudos