How can I buffer selected pros?

993
6
Jump to solution
10-13-2021 10:49 PM
DavidMrázek
Occasional Contributor II

Good day,
how can I trim the buffer layer 200 meters around, then save it in the auxiliary layer. My code attempt:

protected override async void OnClick()
{
await OnSketchCompleteAsync();
}
protected async Task<bool> OnSketchCompleteAsync()
{
var valueArray = await QueuedTask.Run(() =>
{
FeatureLayer fl = MapView.Active.Map.FindLayers("O_Klad_ZTM10").First() as FeatureLayer;
QueryFilter qf = new QueryFilter();
string whereClause = "ZTM10_nom LIKE '0602%'";
qf.WhereClause = whereClause;
var g = new List<object>();
using (RowCursor rowCursor = fl.Search(qf))
{
while (rowCursor.MoveNext())
{
g = new List<object>() { fl.Select(qf) };
}

}

return Geoprocessing.MakeValueArray(g, null, @"200 Meters");// HERE CODE SHOWS ERROR
});
await Geoprocessing.ExecuteToolAsync("analysis.Buffer", valueArray);
return true;
}

 

 If I haven't explained it enough, ask me.

 

Thank you very much for the advice

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

As I understand from your first question  you need code like this:

 

protected override async void OnClick()
{
	FeatureLayer fl = MapView.Active.Map.FindLayers("O_Klad_ZTM10").First() as FeatureLayer;	
	if (fl == null)
		return;

	QueuedTask.Run(() = >
	{
		QueryFilter qf = new QueryFilter();
		string whereClause = "ZTM10_nom LIKE '0602%'";
		qf.WhereClause = whereClause;
		fl.Select(qf)
	});

	var rozsah = 200;
	CalculateBuffer(fl, @"C:\Users\mrazekd\Desktop\ukoly\ProAppModule1\buffers.shp", rozsah, "Meters");
}

You need to check asynchronous things. of that code

 

 

View solution in original post

6 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

MakeValueArray first parameter must be FeatureLayer, second - output path.

It's enough to make selection on featurelayer (fl.Select(qf)) and pass it to MakeValueArray. Geoprocessing checks is selection empty. Dependent on it geoprocessing tool calculates on all featurelayer or feature layer selection

Below working function code to make buffer layer:

        public static bool CalculateBuffer(FeatureLayer featureLayer, string sOutputPath, double dDistance, string sUnits)
        {
            try
            {
                var parameters = Geoprocessing.MakeValueArray(featureLayer, sOutputPath,
                    string.Format("{0} {1}", Convert.ToString(dDistance, System.Globalization.CultureInfo.InvariantCulture), sUnits));
                var gpResult = Geoprocessing.ExecuteToolAsync("Buffer_analysis", parameters, null, CancelableProgressor.None,
                        GPExecuteToolFlags.None);
                gpResult.Wait();
                return !gpResult.Result.IsFailed;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString(), System.Reflection.Assembly.GetExecutingAssembly().FullName);
                throw;
            }
        }

After layer selection you can call that function. And specify full path for output. For example full path to shapefile (c:\Temp\buffers.shp)

DavidMrázek
Occasional Contributor II

Hi,
I tried to redo it according to your advice and the whole code will work, but nothing in ARCGIS PRO did it.
I'm probably making an obvious mistake somewhere, but I don't know where ...

protected override async void OnClick()
{
var polygonFeatureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault(lyr => lyr.ShapeType == esriGeometryType.esriGeometryPolygon);
if (polygonFeatureLayer == null)
return;
var rozsah = 200;
CalculateBuffer(polygonFeatureLayer, @"C:\Users\mrazekd\Desktop\ukoly\ProAppModule1\buffers.shp", rozsah,"meteres");
await OnSketchCompleteAsync();
}
protected async Task<bool> OnSketchCompleteAsync()
{
var valueArray = await QueuedTask.Run(() =>
{
FeatureLayer fl = MapView.Active.Map.FindLayers("O_Klad_ZTM10").First() as FeatureLayer;
QueryFilter qf = new QueryFilter();
string whereClause = "ZTM10_nom LIKE '0602%'";
qf.WhereClause = whereClause;
var g = new List<object>();
using (RowCursor rowCursor = fl.Search(qf))
{
while (rowCursor.MoveNext())
{
Feature feature = rowCursor.Current as Feature;
Polygon polygon = feature.GetShape() as Polygon;
g = new List<object>() {polygon};
}
}
return Geoprocessing.MakeValueArray(g, null, @"C:\Users\mrazekd\Desktop\ukoly\ProAppModule1\buffers.shp");
});
await Geoprocessing.ExecuteToolAsync("PomocnaVrstva", valueArray);
return true;
}
public static bool CalculateBuffer(FeatureLayer featureLayer, string sOutputPath, double dDistance, string sUnits)
{
try
{
var parameters = Geoprocessing.MakeValueArray(featureLayer, sOutputPath,
string.Format("{0} {1}", Convert.ToString(dDistance, System.Globalization.CultureInfo.InvariantCulture), sUnits));
var gpResult = Geoprocessing.ExecuteToolAsync("Buffer_analysis", parameters, null, CancelableProgressor.None,
GPExecuteToolFlags.None);
gpResult.Wait();
return !gpResult.Result.IsFailed;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString(), System.Reflection.Assembly.GetExecutingAssembly().FullName);
throw;
}
}
0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

As I understand from your first question  you need code like this:

 

protected override async void OnClick()
{
	FeatureLayer fl = MapView.Active.Map.FindLayers("O_Klad_ZTM10").First() as FeatureLayer;	
	if (fl == null)
		return;

	QueuedTask.Run(() = >
	{
		QueryFilter qf = new QueryFilter();
		string whereClause = "ZTM10_nom LIKE '0602%'";
		qf.WhereClause = whereClause;
		fl.Select(qf)
	});

	var rozsah = 200;
	CalculateBuffer(fl, @"C:\Users\mrazekd\Desktop\ukoly\ProAppModule1\buffers.shp", rozsah, "Meters");
}

You need to check asynchronous things. of that code

 

 

DavidMrázek
Occasional Contributor II

After a few modifications, I finally managed to start, but now the whole application crashes: D

0 Kudos
GKmieliauskas
Esri Regular Contributor

Share your final code please

DavidMrázek
Occasional Contributor II

Sorry it works, I was looking for the final shapefile elsewhere, and I didn't write await.

 

Thank you very much!!!

0 Kudos