Hello,
is there a possibility to set that the line will have certain properties (size, color) when creating new layers (polyline)?
protected override async void OnClick()
{
await CreateMyLineAsync();
}
private static async Task CreateMyLineAsync()
{
var name = "Linie";
EnumFeatureClassType type = EnumFeatureClassType.POLYLINE;
await CreateLayer(name, type);
await QueuedTask.Run(async () =>
{
GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
{
SymbolTemplate = SymbolFactory.Instance
.ConstructLineSymbol(ColorFactory.Instance.GreenRGB, 20, SimpleLineStyle.Solid)
.MakeSymbolReference(),
};
var fcLayer = MapView.Active.Map.GetLayersAsFlattenedList().Where(l => l.Name == "Linie").FirstOrDefault() as FeatureLayer;
await SelectLayer(fcLayer, @"D:\featLToFeatC.shp");
LayerFactory.Instance.CreateFeatureLayer(new Uri(@"C:\Users\mrazekd\Desktop\Vyber.gdb"),
MapView.Active.Map, layerName:"Linie",rendererDefinition: gcDef);
});
}
private static async Task SelectLayer(FeatureLayer featureLayer, string Output)
{
var parameters = Geoprocessing.MakeValueArray(featureLayer, Output);
var selectLayer = "conversion.FeatureClassToFeatureClass";
await FunctionPart(selectLayer, parameters);
}
private static async Task FunctionPart(string stringFunc, IReadOnlyList<string> parameters)
{
try
{
await Geoprocessing.ExecuteToolAsync(stringFunc, parameters).ConfigureAwait(false);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString(), Assembly.GetExecutingAssembly().FullName);
throw;
}
}
private enum EnumFeatureClassType
{
POINT,
MULTIPOINT,
POLYGON,
POLYLINE
}
private static async Task CreateLayer(string fcName, EnumFeatureClassType fcType)
{
await CreateFeatureClass(fcName, fcType);
var fcLayer = MapView.Active.Map.GetLayersAsFlattenedList().Where(l => l.Name == fcName).FirstOrDefault() as BasicFeatureLayer;
if (fcLayer == null)
{
MessageBox.Show($@"Nelze najít {fcName} v této mapě");
return;
}
{
var dataSource = await GetDataSource(fcLayer);
Debug.WriteLine($@"{dataSource} bylo nalezeno ... přidej pole");
await ExecuteAddFieldToolAsync(fcLayer,
new KeyValuePair<string, string>("Description", "Desc."), "Text", 50);
}
}
private static async Task CreateFeatureClass(string featureclassName, EnumFeatureClassType featureclassType)
{
List<object> arguments = new List<object>
{
CoreModule.CurrentProject.DefaultGeodatabasePath,
featureclassName,
featureclassType.ToString(),
"",
"DISABLED",
"DISABLED"
};
await QueuedTask.Run(() =>
{
arguments.Add(SpatialReferenceBuilder.CreateSpatialReference(5514));
});
await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", Geoprocessing.MakeValueArray(arguments.ToArray()));
}
private static async Task<string> GetDataSource(BasicFeatureLayer theLayer)
{
try
{
return await QueuedTask.Run(() =>
{
var inTable = theLayer.Name;
var table = theLayer.GetTable();
var dataStore = table.GetDatastore();
var workspaceNameDef = dataStore.GetConnectionString();
var workspaceName = workspaceNameDef.Split('=')[1];
var fullSpec = Path.Combine(workspaceName, inTable);
return fullSpec;
});
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return string.Empty;
}
}
private static async Task<string> ExecuteAddFieldToolAsync(
BasicFeatureLayer theLayer,
KeyValuePair<string, string> field,
string fieldType, int? fieldLength = null,
bool isNullable = true)
{
return await QueuedTask.Run(() =>
{
try
{
var inTable = theLayer.Name;
var table = theLayer.GetTable();
var dataStore = table.GetDatastore();
var workspaceNameDef = dataStore.GetConnectionString();
var workspaceName = workspaceNameDef.Split('=')[1];
var fullSpec = Path.Combine(workspaceName, inTable);
Debug.WriteLine($@"Přidat {field.Key} z {fullSpec}");
var parameters = Geoprocessing.MakeValueArray(fullSpec, field.Key, fieldType.ToUpper(), null, null,
fieldLength, field.Value, isNullable ? "NULABLE" : "NON_NULLABLE");
var cts = new CancellationTokenSource();
var results = Geoprocessing.ExecuteToolAsync("management.AddField", parameters, null, cts.Token,
(eventName, o) =>
{
Debug.WriteLine($@"GP event: {eventName}");
});
var isFailure = results.Result.IsFailed || results.Result.IsCanceled;
return !isFailure ? "Chyba" : "Ok";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return ex.ToString();
}
});
}
Thank you for any advice or ideas
Thanks
You can create a feature layer using the feature class and the renderer as parameters of the CreateFeatureLayer function as shown here:
This is the sample snippet demonstrating the usage:
string colorBrewerSchemesName = "ColorBrewer Schemes (RGB)";
StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == colorBrewerSchemesName);
string colorRampName = "Greens (Continuous)";
IList<ColorRampStyleItem> colorRampList = await QueuedTask.Run(() =>
{
return style.SearchColorRamps(colorRampName);
});
ColorRampStyleItem colorRamp = colorRampList[0];
await QueuedTask.Run(() =>
{
GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
{
ClassificationField = "CROP_ACR07",
ClassificationMethod = ArcGIS.Core.CIM.ClassificationMethod.NaturalBreaks,
BreakCount = 6,
ColorRamp = colorRamp.ColorRamp,
SymbolTemplate = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.GreenRGB, SimpleFillStyle.Solid, null).MakeSymbolReference(),
ExclusionClause = "CROP_ACR07 = -99",
ExclusionSymbol = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.RedRGB, SimpleFillStyle.Solid, null).MakeSymbolReference(),
ExclusionLabel = "No yield",
};
LayerFactory.Instance.CreateFeatureLayer(new Uri(@"c:\Data\CountyData.gdb\Counties"),
MapView.Active.Map, layerName: "Crop", rendererDefinition: gcDef);
});
Please note that CreateFeatureLayer is overloaded allowing for different options.
Hi Wolf,
@Wolf wrote:You can create a feature layer using the feature class and the renderer as parameters of the CreateFeatureLayer function as shown here:
This is the sample snippet demonstrating the usage:
string colorBrewerSchemesName = "ColorBrewer Schemes (RGB)";
StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == colorBrewerSchemesName);
string colorRampName = "Greens (Continuous)";
IList<ColorRampStyleItem> colorRampList = await QueuedTask.Run(() =>
{
return style.SearchColorRamps(colorRampName);
});
ColorRampStyleItem colorRamp = colorRampList[0];
await QueuedTask.Run(() =>
{
GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition()
{
ClassificationField = "CROP_ACR07",
ClassificationMethod = ArcGIS.Core.CIM.ClassificationMethod.NaturalBreaks,
BreakCount = 6,
ColorRamp = colorRamp.ColorRamp,
SymbolTemplate = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.GreenRGB, SimpleFillStyle.Solid, null).MakeSymbolReference(),
ExclusionClause = "CROP_ACR07 = -99",
ExclusionSymbol = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.RedRGB, SimpleFillStyle.Solid, null).MakeSymbolReference(),
ExclusionLabel = "No yield",
};
LayerFactory.Instance.CreateFeatureLayer(new Uri(@"c:\Data\CountyData.gdb\Counties"),
MapView.Active.Map, layerName: "Crop", rendererDefinition: gcDef);
});
Please note that CreateFeatureLayer is overloaded allowing for different options.
In the section:
LayerFactory.Instance.CreateFeatureLayer (new Uri (@ "c: \ Data \ CountyData.gdb \ Counties"), MapView.Active.Map, layerName: "Lines", rendererDefinition: gcDef);
the program throws an error message:
'Failed to add data, unsupported data type. c: \ Data \ CountyData.gdb \ Counties'
Why? It's a completely new sobour, isn't it?