ThinkGeo.com    |     Blog    |     Wiki    |     Support

Query KMLFeatureLayer on Lat/Lon point

Hello,

I have a KMLFeatureLayer on my map. I want to be able to determine what polygon a specific Lat/Lon point resides in, and then get some info about that polygon.

If I do a GetAllFeatures on the layer, I get a collection of Geometrycollection(Polygon(…
I can then see the ColumnValues of each feature, one of which is the info I need.

What I tried to do is call GetFeaturesContaining(New PointShape(Lat, Lon), ReturningColumnsType.AllColumns), but that is returning the error: ‘The method or operation is not implemented.’

Am I calling the wrong routine? Is there an easier way to do this?

Edit:
It looks like I can use GetFeaturesNearestTo, but I’m not sure if that gets features (polygons) that contain the lat/lon point in question. Does it?

Thanks,
Dib

Hi Dib,

You can did that like this:

 PointShape pointShape = ExtentHelper.ToWorldCoordinate(winformsMap1.CurrentExtent, new ScreenPointF(e.X, e.Y), winformsMap1.Width, winformsMap1.Height);

        KmlFeatureLayer kmlFeatureLayer = (winformsMap1.Overlays[1] as LayerOverlay).Layers[0] as KmlFeatureLayer;

        Collection<Feature> fs = kmlFeatureLayer.QueryTools.GetFeaturesNearestTo(pointShape, GeographyUnit.Meter, 1000, ReturningColumnsType.AllColumns);

        Collection<Feature> polygonFeatureContaining = new Collection<Feature>();

        foreach (Feature f in fs)
        {
            var shape = f.GetShape();
            if (shape is PolygonShape)
            {
                if (shape.Contains(pointShape))
                {
                    polygonFeatureContaining.Add(f);
                }
            }
        }

And here is a sample about it, wish that’s helpful.
KmlSample-ForWinForms.zip (117.8 KB)

Regards,

Ethan

Ethan,

That does work, but it is slow. It is taken 7-10 seconds to run, which is a bit long. Any way it can speed up?

Hi Dib,

Have you tried to make sure which API make it slow? If the slow is because GetFeaturesNearestTo, you can modify the parameter, in my sample code it’s 1000 meters, you can reduce the radius to get smaller shape collection and it can saved your time.

And if you hadn’t create the index file, you can try this API KmlFeatureLayer.BuildIndexFile to see whether the speed get faster.

Wish that’s helpful.

Regards,

Ethan

Ethan,

This line was the bottleneck:

cols2 = kmlLayer.QueryTools.GetFeaturesNearestTo(thePoint, GeographyUnit.Meter, 100, ReturningColumnsType.AllColumns)

It was getting called for each different point, and slowed things down considerably. I ended up calling this:

cols2 = kmlLayer.QueryTools.GetAllFeatures(retCols)

just once, outside of the points loop, and it sped up things greatly.

Hi Dib,

Your solution is cache all feature into memory and it should make speed up, I don’t know you need to call this logic for many times so my code use get feature near to, and I am glad to hear you find the best solution for your scenario.

Any question please let us know.

Regards,

Ethan