Use ArcEngine to implement the Set datasource function of ArcMap and fix the layer position in mxd.

Source: Internet
Author: User

When opening an mxd File In ArcMap, it is possible that the path of the associated shp file is changed, so that the layer cannot be found. In this case, right-click and choose Properties, in the displayed dialog box, select the source tab,
Click the set data source button, as shown in figure

Select the position of the layer.

The layer configuration information is stored in mxd. If you import the layer data to sde and save it, the configuration information cannot be found during loading, to load layers from the database according to the mxd style,
You have to extract the configuration information of each layer and store it in the database, in this way, you need to consider the annotation, layer rendering, Layer Sequence, layer group, the visible range of the layer, whether the layer is optional, whether the layer is visible,
The projection system of a map. This introduces more complexity,
In fact, you can save the mxd separately. Each time you connect to the database, point each layer in the mxd file to the corresponding layer in sde, then open this mxd to save a lot of trouble.

Thoughts on implementation:
First, traverse the layers of mxd, use the IMapDocument interface to open the mxd file, use the get_Layers method of the IMap interface, and search by the UID of each layer, which is generally divided into three types: vector layer (IGeoFeatureLayer ),
Annotation layer (IFDOGraphicsLayer) and remote sensing image layer (IRasterLayer). The first two types can be converted to the IFeatureLayer interface type, and then the FeatureClass can be directed to the corresponding FeatureClass in SDE,
Use the OpenFeatureClass method of IFeatureWorkspace,
After obtaining FeatureClass, assign this object to IFeatureLayer,

[C-sharp]View plaincopy
  1. PFeatureLayer. FeatureClass = pNewFC;

The image layer can use the OpenRasterDataset method of IRasterWorkspaceEx to obtain its corresponding Dataset, and then use the CreateFromDataset method of IRasterLayer.[C-sharp]View plaincopy

  1. PRasterlyr. CreateFromDataset (pRasterDataset );

When using OpenFeatureClass or OpenRasterDataset, the layer names in the parameters used are all the names when the SDE is imported, that is, the real name of the layer file. In mxd, the name of a normal layer and its true
The shp or image file names are not the same (note layers are generally placed in mdb or gdb, and the name displayed in mxd is the same as its own, directly using lyr. you can only get the alias of this layer in mxd by using the name attribute of ILayer. To get the file name, you need to convert the layer to IDataLayer first to convert the DatasourceName attribute of this object.
Convert to the IDatasetName type and obtain the Name attribute of the IDatasetName. This is the Name of the real file of the layer. The sample code is as follows:

 

[C-sharp]View plaincopy
  1. IDataLayer2 pFd = pFeatureLayer as IDataLayer2;
  2. If (pFd! = Null)
  3. {
  4. If (pFd. DataSourceName is IDatasetName)
  5. {
  6. // Obtain the original name
  7. IDatasetName pDN = pFd. CENAME as IDatasetName;
  8. LyrFeatureClassName = pDN. Name;
  9. }
  10. }

 

Appendix: UID corresponding to each layer type

[C-sharp]View plaincopy
  1. // {6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer
  2. // {40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
  3. /{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
  4. // {34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
  5. // {5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
  6. // {0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
  7. // {EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer
  8. // {D02371C7-35F7-11D2-B1F2-00C04F8EDEFF} IRasterLayer

Reprinted from:

Http://blog.csdn.net/nickwar/article/details/4855684

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.