Hi,
I have to find distinct values of particular column of inmemoryfeaturelayer. I have tried Executenquery() on shapefilefeaturelayer and inmemoryfeaturelayer. But I am not able to get it in inmemoryfeaturelayer. Please suggest how to get it.
Thanks,
Goral
Get Distinct Column Values from InmemoryfeatureLayer
Hi Goral,
Have you declare the column for inmemoryfeaturelayer like this?
layer.Columns.Add(new FeatureSourceColumn("Your Column"));
Regards,
Don
Hi,
Ya I have declared and I am adding values to it.
Thanks,
Goral
Hi Goral,
InMemoryFeatureLayer don’t support ExcuteQuery method as this kind of method is related with database sql statement. For InmemoryFeatureLayer, as all the operation on the layers are in memory, then we can get all the features and then do a filter with linq.
Thanks,
Troy
Hi,
Thanks for reply. Can you please suggest how to get distinct values of column from inmemoryfeaturelayer?
Thanks,
Goral
Goral,
Please try the below codes:
Collection<
Feature
> allFeatures = inMemoryFeatureLayer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
var filterFeatures = allFeatures.ToList().Distinct(new FeatureComparer());
public class FeatureComparer : IEqualityComparer<
Feature
>
{
public bool Equals(Feature obj1,Feature obj2)
{
if (obj1.ColumnValues["a"] == obj1.ColumnValues["a"])
return true;
else
return false;
}
public int GetHashCode(Feature obj)
{
return base.GetHashCode();
}
}
Here is the distinct document, hope it helps. msdn.microsoft.com/en-us/library/vstudio/bb348436(v=vs.100).aspx
Thanks,
Troy
Hi,
Thanks for reply. Its very useful. Two things I want to know from given code.
1) In code FeatureComparer if (obj1.ColumnValues["a"] == obj1.ColumnValues["a"]) is given. My doubt is it should be
if (obj1.ColumnValues["a"] == obj2.ColumnValues["a"]) because we are passing two objects.
2) After implementing code I am getting feature list with distinct column values. I want only column values in any form like datatable column or array.
Request you to guide.
Thanks,
Goral
Hi Goral,
For 1, you’re right, that’s should be obj1.ColumnValues[“a”] == obj2.ColumnValues[“a”], please modify it.
For 2, I think Troy mainly provided the way where to get value and compare them to a result. If you need a special format result, you can implement your logic easily based on the list result.
Regards,
Don
Hi,
Thanks for reply.
Hi Goral,
Any question please let us know.
Regards,
Don
Hi,
I have one more question regarding Intersection. Like Distinct can we write for Intersection.
Thanks,
Goral
Hi Goral,
I think Intersection should related with shape but not for value, if the "Intersection" here meant find same part of special value I think you can just use the same logic.
If you meant you want to get Intersection between shape, please call our spatial query API for it.
Regards,
Don
Hi,
Thanks for reply. Ya I want intersection of between shape. I have tried spatial query but in that we can pass only one feature at a time. So we have to run a loop. I have two layers and without running loop I want intersecting features. Any other way please suggest.
Thanks,
Goral
Hi Goral,
For your requirement, I think you have to loop all features unless you create a algorithm function to combine all feature to one shape.
In fact for our special shape like MultipolygonShape, it still need to loop all features and calculate result.
Regards,
Don
The FeatureComparer is not needed if you just want the distinct values of the column. You can just use:
Collection<Feature> allFeatures = inMemoryFeatureLayer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
var filteredValues = (from f in allFeatures select f.ColumnValues["a"]).Distinct();
Hi Pavlos,
Thanks for your share, Linq statement is looks simpler.
Regards,
Don