How can I buffer selected pros?

155
6
Jump to solution
a week ago
DavidMrázek
New Contributor III

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
GintautasKmieliauskas
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
GintautasKmieliauskas
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
New Contributor III

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
GintautasKmieliauskas
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

DavidMrázek
New Contributor III

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

0 Kudos
GintautasKmieliauskas
Regular Contributor

Share your final code please

DavidMrázek
New Contributor III

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

 

Thank you very much!!!

0 Kudos