How to load shapefile layer to current map?

925
2
Jump to solution
10-17-2021 10:20 PM
DavidMrázek
Occasional Contributor II

Hello,

does anyone have any c # code that can load the shapefile into the current map and add the same reference so that it fits well into the original map?

 

Thank for any advice

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

To add a data source to your current active map (or another map) you can use the snippet:

public Task<Layer> AddLayer(string uri)
{
    return QueuedTask.Run(() =>
    {
        Map map = MapView.Active.Map;
        return LayerFactory.Instance.CreateLayer(new Uri(uri), map);
    });
}

and you would call the method like this:

var pathToSource = @"c:\temp\test.shp";
Layer lyr = await AddLayer(pathToSource);

and some examples for pathToSource

  1.  FeatureClass inside a FileGeodatabase: C:\Data\MyFileGDB.gdb\Census
  2. A shape file inside a folder: \\Machine\SharedFolder\Census.shp
  3. Raster Dataset inside a FileGeodatabase: C:\Data\MyFileGDB.gdb\DEM
  4. An image file inside a folder: \\Machine\SharedFolder\Imagery.tif
  5. A map service layer: http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer
  6. A feature layer off a map or feature service: http://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0
  7. A .lprx or .lpkx file: \\Machine\SharedFolder\Fires.lyrx

View solution in original post

2 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

To add a data source to your current active map (or another map) you can use the snippet:

public Task<Layer> AddLayer(string uri)
{
    return QueuedTask.Run(() =>
    {
        Map map = MapView.Active.Map;
        return LayerFactory.Instance.CreateLayer(new Uri(uri), map);
    });
}

and you would call the method like this:

var pathToSource = @"c:\temp\test.shp";
Layer lyr = await AddLayer(pathToSource);

and some examples for pathToSource

  1.  FeatureClass inside a FileGeodatabase: C:\Data\MyFileGDB.gdb\Census
  2. A shape file inside a folder: \\Machine\SharedFolder\Census.shp
  3. Raster Dataset inside a FileGeodatabase: C:\Data\MyFileGDB.gdb\DEM
  4. An image file inside a folder: \\Machine\SharedFolder\Imagery.tif
  5. A map service layer: http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer
  6. A feature layer off a map or feature service: http://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0
  7. A .lprx or .lpkx file: \\Machine\SharedFolder\Fires.lyrx
DavidMrázek
Occasional Contributor II

Thank you!!!

0 Kudos