I know how to use the FeatureSource.GetFeaturesNearestTo() function to find the feature the user has clicked on, but some of my features contain a MultiLine shape. How can I best find which of the lines within the MultiLine shape the user clicked on?
Get Shape instead of feature Nearest To user's click
Hi,
I think there are two ways for you: One is to calculate the multiline shape again to find out the nearest single line, here are some codes for this one.The other way is we split all the multiline feature into the single line feature from the file level, which means we split those features and then save them back shape file with all the single line features.
void wpfMap1_MapClick(object sender, MapClickWpfMapEventArgs e)
{
highLightLayer.InternalFeatures.Clear();
var result = worldLayer.FeatureSource.GetFeaturesNearestTo(e.WorldLocation, wpfMap1.MapUnit, 1, ReturningColumnsType.AllColumns);
if (result.Count > 0)
{
double distance = -1;
LineShape resultLine = null;
MultilineShape multilineShape = result[0].GetShape() as MultilineShape;
foreach (var line in multilineShape.Lines)
{
double distance2 = line.GetDistanceTo(e.WorldLocation, wpfMap1.MapUnit, DistanceUnit.Meter);
if (distance == -1 || distance2 < distance)
{
distance = distance2;
resultLine = line;
}
}
highLightLayer.InternalFeatures.Add(new Feature(resultLine));//The resultLine is the shape User’s click on.
}
wpfMap1.Refresh();
}
Hope it helps.
Regards,
Troy
Perfect!
Hi,
So good to hear it’s fit for you.
Regards,
Troy
Ok, next issue:
Now that I know which shape the user clicked on, I want the user to be able to interactively edit this shape.
My plan to achieve this was as follows:
1. Remove the clicked on shape from its feature
2. Create a new temporary feature containing just this one shape
3. Add this temporary feature to the EditShapesLayer allowing the user to edit it
4. After user edit, get shape from the temporary feature(and throw away the temporary feature)
5. Add this editted shape to the original feature
I've implemented this, and it works partially, but I can't find out how to best remove (and later add) the shape from (to) the feature.
Using the same definitions as in the code you provided, I've tried:
multilineShape.Lines.Remove(resultLine);
This removes resultLine from multilineShape, but not from the feature where multilineShape was taken from. This is also true when I use Add instead of Remove.
How can I get the shape from a feature and make sure that when I edit it, by adding or removing parts from a multishape, the feature will contain this edited shape?
If that isn't possible, is there maybe a way to replace an existing feature's shape? I know I can create a new feature, but then I need to manually set the Id, Tag, and column values to be equal to the former feature, and this seems less than ideal.
Possibly there are better ways to achieve my goals, if so, please let me know!
Hi,
Not sure but if you means you want to make sure the edited feature include the modified part in the original source like in shape file, then we need to use some codes like the below:
source.BeginTransaction();
Feature feature = source.GetFeatureById(“123”, ReturningColumnsType.AllColumns);
RectangleShape newShape = new RectangleShape(-100, 20, -80, 0);
Feature resultFeature = new Feature(newShape, feature.Id, feature.ColumnValues);
source.UpdateFeature(resultFeature);
source.CommitTransaction();
Please make sure the resultfeature’s id is equal to the updated feature from source layer.
If there is anything I misunderstand, please feel free to correct me.
Hope it helps.
Troy
It sort of answers my question, although indirectly :-)
From your answer I understand that there is no UpdateShape kind of method for the feature, so when I change a feature’s shape I have to make a new feature and force it to have the same Id, and ColumnValues (and when I do the Tag is copied too).
By the way:
Feature resultFeature =
new
Feature(newShape, feature.Id, feature.ColumnValues);
gives an “The best overloaded method match has some invalid arguments” error, but
Feature resultFeature =
new
Feature(newShape, feature.ColumnValues);
resultFeature.Id = feature.Id;
works fine, so t’s no big deal.
Thanks!
Hi,
Maybe I am not using the latest version :), but good to hear it helps.
Regards,
Troy
Hi,
Maybe I an not using the latest version :), but good to hear it helps.
Regards,
Troy