I've created a multipolygon shape where some of the polygons overlap. There are two issues. The first is that the overlapping sections of the polygons do not paint (can see other layers behind them). The second is that when I do a shape.Contains() call, I get the following exception:
I can workaround the exception by calling Contains on the individual Polygons but can’t get by the lack of painting. Could I get a copy of the fix when it’s ready? Any estimates?
I’ll weigh in on this a little… we use NTS extensively for backend processing. I can tell you that the version of NTS that is being used simply does not like invalid geometries (and you have said yours is invalid). The NTS effort is an open source effort, and a version is being built that is more robust, with the performance of the less robust versions. I cannot speak to when this version will be available though.
Can you consider making your geometry valid? ie, union all of your individual polygons into a new geometry, so that you don’t have overlapping exterior rings?
Thanks for your post and sharing! I appreciate it very much.
I think there are 2 solutions for your requirements. 1) Use Polygons instead of MultiPolygons. I tried to add the polygons as following instead of adding one MultiPolygon, the overlapping intersections will be painted. And when you want to do Shape.Contains() call, just try to loop each Polygons(polygon1 ~ polygon4) to work around as you said.
//InMemoryFeatureLayer inMemoryLayer = new InMemoryFeatureLayer(); //inMemoryLayer.InternalFeatures.Add("Polygon1", new Feature(polygonShape1)); //inMemoryLayer.InternalFeatures.Add("Polygon2", new Feature(polygonShape2)); //inMemoryLayer.InternalFeatures.Add("Polygon3", new Feature(polygonShape3)); //inMemoryLayer.InternalFeatures.Add("Polygon4", new Feature(polygonShape4));
InMemoryFeatureLayer inMemoryLayer = new InMemoryFeatureLayer(); MultipolygonShape multiPolygonShape = new MultipolygonShape(); multiPolygonShape.Polygons.Add(polygonShape1); multiPolygonShape.Polygons.Add(polygonShape2); multiPolygonShape.Polygons.Add(polygonShape3); multiPolygonShape.Polygons.Add(polygonShape4); inMemoryLayer.InternalFeatures.Add(new Feature(multiPolygonShape));
2) Override your own InmemoryFeatureLayer and InmemoryFeatureSource to fix the problem of not painting when adding a MultiPolygonShape. Following is some codes you can take a reference.
For the problem of Contains, I think you still need to go around this by loop each PolygonShape.
// Create your own FeatureSource public class CustomInmemoryFeatureSource : InMemoryFeatureSource { private CustomInmemoryFeatureSource() : this(new FeatureSourceColumn[] { }, new Feature[] { }) { }
public CustomInmemoryFeatureSource(IEnumerable<FeatureSourceColumn> featureSourceColumns) : this(featureSourceColumns, new Feature[] { }) { }
public CustomInmemoryFeatureSource(IEnumerable<FeatureSourceColumn> featureSourceColumns, IEnumerable<Feature> features) : base(featureSourceColumns,features) {
}
protected override Collection<Feature> GetFeaturesForDrawingCore(RectangleShape boundingBox, double screenWidth, double screenHeight, IEnumerable<string> returningColumnNames) { Collection<Feature> features = base.GetFeaturesForDrawingCore(boundingBox, screenWidth, screenHeight, returningColumnNames); Collection<Feature> returnFeatures = new Collection<Feature>(); foreach (Feature feature in features) { BaseShape baseShape = feature.GetShape(); if (baseShape is MultipolygonShape) { MultipolygonShape multiPolygonShape = (MultipolygonShape)baseShape;
foreach (PolygonShape polygonShape in multiPolygonShape.Polygons) { Feature polygonFeature = new Feature(polygonShape, feature.ColumnValues); returnFeatures.Add(polygonFeature); } } else { returnFeatures.Add(feature); } }
return returnFeatures;
} }
//Create your own FeatureLayer based on the new FeatureSource public class CustomInmemoryFeatureLayer : InMemoryFeatureLayer { public CustomInmemoryFeatureLayer() : this(new FeatureSourceColumn[] { }, new Feature[] { }) { }
public CustomInmemoryFeatureLayer(IEnumerable<FeatureSourceColumn> featureSourceColumns, IEnumerable<Feature> features) : base() { FeatureSource = new CustomInmemoryFeatureSource(featureSourceColumns, features); }
public CustomInmemoryFeatureLayer(IEnumerable<FeatureSourceColumn> featureSourceColumns, IEnumerable<BaseShape> shapes) { Collection<Feature> features = new Collection<Feature>(); foreach (BaseShape shape in shapes) { features.Add(new Feature(shape)); }
FeatureSource = new CustomInmemoryFeatureSource(featureSourceColumns, features); } }
//Useage CustomInmemoryFeatureLayer inMemoryLayer = new CustomInmemoryFeatureLayer(); MultipolygonShape multiPolygonShape = new MultipolygonShape(); multiPolygonShape.Polygons.Add(polygonShape1); multiPolygonShape.Polygons.Add(polygonShape2); multiPolygonShape.Polygons.Add(polygonShape3); multiPolygonShape.Polygons.Add(polygonShape4); inMemoryLayer.InternalFeatures.Add(new Feature(multiPolygonShape));
David, I had to catach a plane and could not follow up. I think Yale is right about the contains stuff. Within our own application, I have abandoned using the NTS built-in geometry operations directly. So, I never use code like "Geometry.Contains(point)". Instead, we have written a geometry operations factory with a method Contains(IGeometry geometry, IPoint point). Within that factory, if we test for a multi-geometry, and if found, recurse the parts, breaking on the first True result. Same on buffering and other operations. The MultiGeometry object is problematic. So maybe you will have to do the same thing but operate on ThinkGeo shapes?
In a custom feture layer, another option (rather than creating a feature for each part) is to clean the topology on any multi-part features as discussed above… create a new feature by unioning all of the parts of the original feature. This would preserve the same number of features in the feature collection as your original data source, and if using search tools, etc, you would find the complete feature, instead of potentially just finding individual parts.
Thanks for the input. For my app I need to keep a rectangular shape and only one set of sides can be widened. Creating other shapes would be problematic for me since I need to keep track of the original vertices (and don't want to keep them in ColumnValues somewhere). Geometry always did make my mind blister.
Not sure which way I'm heading but at least I have a few more options ( a new class (shape as I mentioned or FeatureSource/Layer as Yale showed, or just adding the polygons one at a time). Just a bit worried about what else I'm going to need to handle after the paint and Contains.
Your suggestion worked for normal layers. My problem now is that when the shape is moved to the EditShapesLayer the paint problem again shows up. I’m using a custiom EditInteractiveOverlay and I tried creating a new EditShapesLayer property and setting that property to a CustomInMemoryFeatureLayer. It doesn’t seem to work. I can’t change the property directly since it is read-only. Any thoughts?
I am sorry I did not notice you are using the Feature for editing.
Yes, I think you are right! Currently, the EditShapesLayer in EditOverlay is READ-ONLY, we will consider to make it writable in later version. So you cannot accomplish by setting your created CustomInMemory layer to it now.But still we provide another way from scratch to go for this if you are really interested to Achieve this.
Following is the code you can take a reference from:
public class MyCustomEditOverlay : EditInteractiveOverlay { public MyCustomEditOverlay() : base() { }
protected override void DrawCore(GeoCanvas canvas) { // Draw 6 inmemoryLayers. Collection<SimpleCandidate> labelsInAllLayers = new Collection<SimpleCandidate>(); EditShapesLayer.Open(); Collection<Feature> backedUpFeatures = EditShapesLayer.QueryTools.GetAllFeatures(ReturningColumnsType.AllColumns); EditShapesLayer.InternalFeatures.Clear(); foreach (Feature feature in backedUpFeatures) { BaseShape baseShape = feature.GetShape(); if (baseShape is MultipolygonShape) { MultipolygonShape multiPolygonShape = (MultipolygonShape)baseShape;
I placed the code in my custom edit overlay and now my polygons don't paint at all. The control points paint. I have set the DefaultAreaStyle. Would that cause this? Should we be checking IsDirty or is that done before calling DrawCore?
I am sorry I cannot recreate your problem. I guess there are a couple of reasons probably cause the problem:
1) I did not see you add the Multipolygon Feature. Are you sure you added this multiPolygonFeature to the EditShapesLayer in the EditOverlay?
2) Can you try to remove the projection? Comment out the set of the Projection for the Overlay? And try to the MultiPolygon shape posted at the beginning of the post.
3) Can you try not to use another ResizeMode?
If you still cannot paint for the feature, I can upload my demo for you. Or you can upload your demo for us.
Sorry for the late response. In my attempt to modify my custom edit overlay (the read-only EditOverlay issue), I created a new EditShapesLayer property. The draw method evidently doesn’t use this property and to old layer got drawn instead (I assume). Working fine now after I backed that out.