Select to view content in your preferred language

Why can't I search for elements?

981
7
09-23-2022 12:25 AM
DavidMrázek
Frequent Contributor

Hi,

I'm trying to find lines on which there are points that I would then connect into a line. The problem is that as soon as I look for points intersecting the line, it doesn't find any even though I know they must be there.

průnik.PNG

 

One line example

my code:

 if (!(MapView.Active.Map.FindLayers("Z_Voda_L ").First() is FeatureLayer vodaLayer))
                return;
   FeatureClass fcVoda = vodaLayer.GetTable() as FeatureClass;
 if (!(MapView.Active.Map.FindLayers("featureToVer").First() is FeatureLayer featureToVer))
                    return;
                FeatureClass fcFeatureToVer = featureToVer.GetTable() as FeatureClass;
 var qfilter = new QueryFilter
                {
                    WhereClause = "OBJECTID >= 0"
                };
  var vodaPruchod = fcVoda.Search(qfilter);

 while (vodaPruchod.MoveNext())
                {
                    var vodaFeature = vodaPruchod.Current as Feature;
                    var body = vodaFeature.GetShape() as Polyline;
                    var spatialQueryFilter = new SpatialQueryFilter()
                    { FilterGeometry = body, SpatialRelationship = SpatialRelationship.Intersects};
                    var polyCursor = fcFeatureToVer.Search(spatialQueryFilter, false);
                    var mapoveBody = new List<MapPoint>();
                    while (polyCursor.MoveNext())
                    {
                        var bodAktual = polyCursor.Current as Feature;
                        var mapPoint = bodAktual.GetShape() as MapPoint;
                        mapoveBody.Add(mapPoint);
                    }
}
}

The polyline is always selected because I see it always has a length, but I don't know why it doesn't find any point.

 

I will be glad for any advice and help

David

0 Kudos
7 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

 Somethimes spatial filter doesn't work if spatial references of FilterGeometry and FeatureClass differs. Then you need to reproject FilterGeometry

DavidMrázek
Frequent Contributor

I'm probably asking a stupid question, but how is it done?

0 Kudos
GKmieliauskas
Esri Regular Contributor

Project or check spatial references?

To project use code like this:

var sr = fcFeatureToVer.GetDefinition().GetSpatialReference();
...
FilterGeometry = GeometryEngine.Instance.Project(body, sr);

To check spatial references place breakpoint near spatialQueryFilter add body and fcFeatureToVer to VS watch list and investigate

0 Kudos
DavidMrázek
Frequent Contributor

Now it gives me this error: 'Invalid URI: The format of the URI could not be determined.'

Is it possible that this is the wrong path? Where did I save it...

0 Kudos
GKmieliauskas
Esri Regular Contributor

Which line generates the error? There is no code for saving in your pasted code.

0 Kudos
DavidMrázek
Frequent Contributor

var sr = fcFeatureToVer.GetDefinition().GetSpatialReference();

0 Kudos
DavidMrázek
Frequent Contributor

This is my entire code:

 protected override async void OnClick()
        {
            if (!(MapView.Active.Map.FindLayers("Z_Voda_L ").First() is FeatureLayer vodaLayer))
                return;
            if (!(MapView.Active.Map.FindLayers("Z_KomSilnice_L ").First() is FeatureLayer komLayer))
                return;

            await QueuedTask.Run(async () =>
            {
                Map activeMap = MapView.Active.Map;
                string fileBufferVoda = @"D:\BufferVoda.shp";
                object[] bufferParaVoda =
                    {vodaLayer, fileBufferVoda, "1 Meters", "FULL", "ROUND", "LIST", "", "PLANAR"};
                await StartATask("analysis.Buffer", bufferParaVoda);
                string fileBufferSilnice = @"D:\BufferSilnice.shp"; ;
                object[] bufferParaSilnice =
                    {komLayer, fileBufferSilnice, "2 Meters", "FULL", "ROUND", "LIST", "", "PLANAR"};
                await StartATask("analysis.Buffer", bufferParaSilnice);
                string output = @"D:\PairwiseIntersect.shp";
                await CreateFcWithAttributesAsync("LinieKoncovychBodu", EnumFeatureClassType.Polyline);
                var polylineFeatureLayer = activeMap.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault(lyr => lyr.ShapeType == esriGeometryType.esriGeometryPolyline);
                string silnice = "BufferSilnice";
                string voda = "BufferVoda";
                var qfilter = new QueryFilter
                {
                    WhereClause = "OBJECTID >= 0"
                };
                FeatureClass fcVoda = vodaLayer.GetTable() as FeatureClass;
                var editace = new EditOperation
                {
                    Name = "Posun anotace",
                    SelectNewFeatures = false
                };
                var insp = new Inspector();
                FeatureLayer bufferSilnice = MapView.Active.Map.FindLayers(silnice).First() as FeatureLayer;
                FeatureLayer bufferVoda = MapView.Active.Map.FindLayers(voda).First() as FeatureLayer;
                object[] inputFields = { bufferVoda + ";" + bufferSilnice };
                object[] parametrPair =
                    {inputFields, output , "ALL", "", "INPUT"};
                await StartATask("analysis.PairwiseIntersect", parametrPair);
                var vodaPruchod = fcVoda.Search(qfilter);
                string outputVer = @"D:\featureToVer.shp";
                object[] parametrVer =
                    {vodaLayer, outputVer , "ALL"};
                await StartATask("management.FeatureVerticesToPoints", parametrVer);
                if (!(MapView.Active.Map.FindLayers("featureToVer").First() is FeatureLayer featureToVer))
                    return;
                FeatureClass fcFeatureToVer = featureToVer.GetTable() as FeatureClass;
                var sr = fcFeatureToVer.GetDefinition().GetSpatialReference();
                //SpatialReferenceBuilder srBuilder = new SpatialReferenceBuilder(3857);//zkusit dát tohle jako referenci

                if (!(MapView.Active.Map.FindLayers("PairwiseIntersect").First() is FeatureLayer pairwise))
                    return;
                FeatureClass fcPairwise = pairwise.GetTable() as FeatureClass;
                var createOperation = new EditOperation();
                var polylineFeatureClass = polylineFeatureLayer.GetTable() as FeatureClass;
                var polylineDefinition = polylineFeatureClass.GetDefinition();
                var linie = new List<Polyline>();
                while (vodaPruchod.MoveNext())
                {
                    var vodaFeature = vodaPruchod.Current as Feature;
                    var body = vodaFeature.GetShape() as Polyline;
                    var spatialQueryFilter = new SpatialQueryFilter()
                    { FilterGeometry = GeometryEngine.Instance.Project(body, sr), SpatialRelationship = SpatialRelationship.Intersects};
                    var polyCursor = fcFeatureToVer.Search(spatialQueryFilter, false);
                    var mapoveBody = new List<MapPoint>();
                    while (polyCursor.MoveNext())
                    {
                        var bodAktual = polyCursor.Current as Feature;
                        var mapPoint = bodAktual.GetShape() as MapPoint;
                        mapoveBody.Add(mapPoint);
                    }
                    var newPolyline = PolylineBuilder.CreatePolyline(mapoveBody, polylineDefinition.GetSpatialReference());
                    linie.Add(newPolyline);
                    var list = new Dictionary<long, string>();
                    var filter = new QueryFilter
                    {
                        WhereClause = "FID = 0"
                    };
                    var stranaHorni = fcPairwise.Search(filter);
                    while (stranaHorni.MoveNext())
                    {
                        var aktualniPrvek = stranaHorni.Current as Feature;
                        var hledanyBod = aktualniPrvek.GetShape() as Polygon;
                        var spatialQuery = new SpatialQueryFilter()
                        { FilterGeometry = hledanyBod, SpatialRelationship = SpatialRelationship.Contains };
                        var polyCursor3 = fcFeatureToVer.Search(spatialQuery, false);
                        while (polyCursor3.MoveNext())
                        {
                            var oid = polyCursor3.Current.GetObjectID();
                            var aktualni = polyCursor3.Current;
                            var jmeno = aktualni["JMENO"].ToString();
                            list.Add(oid, jmeno);
                        }
                    }
                    foreach (var item in list)
                    {
                        var oid = item.Key;
                        insp.Load(featureToVer, oid);
                        var shape = insp.Shape;
                        var posun = GeometryEngine.Instance.Move(shape, -10, -10) as MapPoint;
                        insp.Shape = posun;
                        editace.Modify(insp);
                    }
                    foreach (var line in linie)
                    {
                        createOperation.Create(polylineFeatureLayer, line);
                    }
                }
                await createOperation.ExecuteAsync();

                await editace.ExecuteAsync();
            });
        }
0 Kudos