Hello,
I have created a polygon feature, and added it an InMemoryFeatureLayer’s .InternalFeatures. Then I create a PointShape feature, and add it to the .InternalFeatures. I want to find out if this point is within the polygon. I’ve tried Overlap, Contains, Intersects, Crosses, and Touches, and they always come back false, even when it is clear that the lat/lon of the point is within the polygon.
What am I doing wrong? What is the best way to find out if a lat/lon is within the boundaries of a polygon?
Thanks,
Dib
Is a point within a polygon
Hi Dib,
In order to check if a point is within a polygon, we can simply use the build-in methods in Feature Or BaseShape class to do it, the codes like the below:
Feature addingPolygon =
new
Feature(
“POLYGON((-31.497802734375 35.3759765625, -32.200927734375 -30.0146484375, 63.424072265625 -33.5302734375, 60.611572265625 31.8603515625, -31.497802734375 35.3759765625))”
);
Feature addingPoint =
new
Feature(
new
PointShape(0, 0));
bool
isContainedByPolygon = addingPolygon.Contains(addingPoint);
The Codes works fine in our end. I am wondering If you are using the InMemoryFeatureLayer methods in QueryTools to do this? Would you please attached your codes here?
It’s better we can have the point and polygon’s wkt. We can get the wkt by method feature.GetWellKnownText().
Thanks,
Troy
Troy,
Thanks for the code. I found another way to find this out, based on our data, not map features.
Hi Dib,
That’s Great! If more queries, don’t hesitate to let us know.
Thanks,
Troy
Dib,
One quick note on using InMemeoryFeatureLayers and InMemoryFeatureSources, when you add or remove items from them using the internalfeatures property make sure that if you add many to call the .BuildIndex on them. This builds a in memory r-tree spatial index which speed up query from the featuresource. If you use the .QueryTools.BeginTransaction and then use the .QeuryTools.Add and then QeuryTools.Commit then you dont need to build the index as it is built automatically.
David