I have been getting a System.InvalidOperationException seemingly randomly while I am running my program. The additional information says: The FeatureSource is not open. Please call the Open Method before calling this method.
I am including my code below. It should be comprehensive enough to give you an idea of what I am doing or at least trying to do. Basically, I have a point layer displayed on the map, and I am drawing in a polygon layer. When the polygon layer finishes drawing, it looks for the nearest point and creates a highlight layer for it (inmemoryfeaturelayer). I am pulling attribute data from the point and joining it to the polygon. I am letting the user see which point is closest to their polygon shape by using the highlight layer. The highlight layer copies the point layer’s closest feature, and it creates a larger different color symbol to place over top of the closest feature. This part is done automatically.
I am trying to let me user select a different point if they decide they want to use a different feature. So, I have it set so they click on the visible dialog to change things around. I remove the normal map_click event that I had been using and add another one to route to my code below. I have been playing around with this and could not get it to crash the same way twice. I keep getting that exception; sometimes at 2 clicks on new features, other times at 30.
I saw something on your web application forms about a concurrency issue, but I would not know how to tackle that with WPF. Especially since I denote open and close methods anytime I am accessing a layer. I have been trying to trace it down using console messages, but it does not seem consistent when crashing. The one thing I did notice happens frequently even though I am not refreshing the map is Map Suite Trace: GoogleMapsLayer Request Image Count: * appearing in my console messages.
Any advice would be great. I am trying to provide you enough information without overwhelming you in my process here. I left most of it tact so it might have some other functions that either return shapefile names or something else. Those have been working for the most part. I am just confused on what is causing the exception since I haven’t been able to track it down. Thanks in advance!
001.PrivateSubReselectJoinFeature(ByValsenderAsObject,ByValeAsThinkGeo.MapSuite.WpfDesktopEdition.MapClickWpfMapEventArgs)002.DimnewJoinFeatureAsFeature003.newJoinFeature =NewFeature004.DimtransferResults()AsObject005.006.'select the new feature, change the highlight layer007.newJoinFeature = SetNewJoinFeatureValue(e)008.009.IfnewJoinFeature IsNotNothingThen010.'this next function just queries a database looking for fields/layer names that need to move attribute data011.'to/from012.transferResults = CheckForTransferValues(activeLayer.Name, valueTransferCollection(0).Name, newJoinFeature)013.014.IftransferResults(4) <>“”Then015.valueTransferResult = transferResults(4)016.Else017.valueTransferResult =""018.EndIf019.Else020.Console.WriteLine(“reselectjoinfeature is nothing”)021.EndIf022.EndSub023.024.025.PrivateFunctionSetNewJoinFeatureValue(ByValeAsThinkGeo.MapSuite.WpfDesktopEdition.MapClickWpfMapEventArgs)AsFeature026.'I remove the original map click handler and add in a handler for this one. At this point, there is027.'already a point feature in the highlightoverlay that is shown on screen028.'the idea behind this is to click another point on the map to change the selection and also029.'change which feature is 'highlighted’030.'I am just overlapping the original feature with the highlightoverlay031.DimfeatureCollectionAsCollection(Of Feature)032.DimjoiningLayerNameAsString033.DimtempAttributeLayer()AsString034.DimnewFeatureAsFeature035.DimjoinLayerAsShapeFileFeatureLayer036.DimshapeTypeAsShapeFileType037.038.'these next four lines are just there to grab a name for the target shapefile for the highlight overlay039.'they run into some functions that just adjust what name we are referring to from our database040.tempAttributeLayer = Split(txtSpJoinLayer.Text,“:”)041.joiningLayerName = tempAttributeLayer(1).Trim042.joiningLayerName = ReturnInternalNameFromUserName(joiningLayerName)043.joinLayer = ReturnShapefileFromLayerName(joiningLayerName)044.045.joinLayer.Open()046.joinLayer.FeatureSource.Open()047.048.shapeType = joinLayer.GetShapeFileType049.050.joinLayer.FeatureSource.Close()051.joinLayer.Close()052.053.'this function should return a collection (of 1 feature)054.'pass in the mouse click location and the name of the layer we are looking for055.featureCollection = SelectFeatures(e, joiningLayerName)056.057.''assign the feature to a feature variable058.IffeatureCollection IsNotNothingThen059.IffeatureCollection.Count > 0Then060.newFeature =NewFeature061.newFeature = featureCollection.Item(0)062.'if we were able to select a feature, it will be passed into the sub to add it to the highlight overlay063.CreateHighlightFeature(shapeType, newFeature)064.065.'select and return something066.ReturnnewFeature067.EndIf068.EndIf069.ReturnNothing070.071.EndFunction072.073.074.PrivateFunctionSelectFeatures(ByValeAsThinkGeo.MapSuite.WpfDesktopEdition.MapClickWpfMapEventArgs,OptionalByValtargetLayerAsString=Nothing)AsCollection(Of Feature)075.076.DimselectedfeaturesAsCollection(Of Feature)077.DimboxAsRectangleShape078.DimxAdjustAsDouble079.DimxOverlayAsNewLayerOverlay()080.DimxFeature1AsNewFeature081.082.xlayer = ReturnShapefileFromLayerName(targetLayer)083.084.'we are using a function here to try to ascertain the current zoom level in order to make a size appropriate085.'box to grab a feature086.SelectCaseGetZoomLevel(WpfMap_Main)087.Case1To5088.xAdjust = 10000089.Case6To7090.xAdjust = 5000091.Case8092.xAdjust = 4000093.Case9094.xAdjust = 3000095.Case10096.xAdjust = 2000097.Case11098.xAdjust = 450099.Case12100.xAdjust = 300101.Case13102.xAdjust = 100103.Case14104.xAdjust = 60105.Case15106.xAdjust = 30107.Case16108.xAdjust = 20109.Case17110.xAdjust = 10111.Case18112.xAdjust = 5113.Case19114.xAdjust = 2115.Case20116.xAdjust = 1117.EndSelect118.119.'here we create that box120.box =NewRectangleShape(e.WorldX - xAdjust, e.WorldY + xAdjust, e.WorldX + xAdjust, e.WorldY - xAdjust)121.122.xlayer.Open()123.xlayer.FeatureSource.Open()124.Ifxlayer.GetShapeFileType = ShapeFileType.PointThen125.selectedfeatures = xlayer.QueryTools.GetFeaturesWithin(box, ReturningColumnsType.AllColumns)126.Else'xlayer.GetShapeFileType = ShapeFileType.Polygon in this case - need to add functionality for lines, etc depending on what kind of selection method we want127.selectedfeatures = xlayer.QueryTools.GetFeaturesIntersecting(box, ReturningColumnsType.AllColumns)128.EndIf129.xlayer.FeatureSource.Close()130.xlayer.Close()131.132.Ifselectedfeatures.Count > 0Then133.Returnselectedfeatures134.Else135.ReturnNothing136.EndIf137.138.139.140.EndFunction141.142.PrivateSubCreateHighlightFeature(ByValshapeTypeAsShapeFileType,ByValnewFeatureAsFeature)143.144.'since I am running this sub repeatedly and I only want one feature shown in the highlight overlay145.'I first check the overlay’s featurelayer (highlightfeaturelayer) for content and clear it146.'remove inmemoryfeaturelayer features147.highlightFeatureLayer.Open()148.highlightFeatureLayer.FeatureSource.Open()149.150.IfhighlightFeatureLayer.InternalFeatures.Count > 0Then151.highlightFeatureLayer.InternalFeatures.Clear()152.EndIf153.154.highlightFeatureLayer.FeatureSource.Close()155.highlightFeatureLayer.Close()156.157.'here I take the shapetype that was passed in from newFeature’s shapefile to determine symbology158.SelectCase(shapeType)159.CaseShapeFileType.Point160.highlightFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.StandardColors.GhostWhite, 8, GeoColor.StandardColors.Black)161.CaseShapeFileType.Polygon162.highlightFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.GhostWhite, GeoColor.StandardColors.Black, 3)163.CaseShapeFileType.Polyline164.highlightFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.StandardColors.GhostWhite, 3,True)165.166.EndSelect167.168.'the rest of this should be generic enough to have here instead of in the select case169.highlightFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20170.highlightFeatureLayer.Open()171.highlightFeatureLayer.FeatureSource.Open()172.highlightFeatureLayer.FeatureSource.BeginTransaction()173.highlightFeatureLayer.FeatureSource.AddFeature(newFeature)174.highlightFeatureLayer.FeatureSource.CommitTransaction()175.highlightFeatureLayer.FeatureSource.Close()176.highlightFeatureLayer.Close()177.178.highlightOverlay.Layers.Add(highlightFeatureLayer)179.180.'the highlightOverlay was added to the map during initialization so I am only adjusting its contents181.'with this sub182.183.WpfMap_Main.Refresh()184.185.originalTransferValueFeature(2) = shapeType186.187.EndSub188.189.PrivateFunctionReturnShapefileFromLayerName(ByVallayerNameAsString)AsShapeFileFeatureLayer190.IflayerName <>“”Then191.192.193.DimoverlayCollectionAsNewCollection(Of LayerOverlay)194.DimlayerCollectionAsNewCollection(Of ShapeFileFeatureLayer)195.196.overlayCollection.Clear()197.layerCollection.Clear()198.199.'need to build a collection of work layers and basemap layers200.201.ForiAsInteger= 0ToWpfMap_Main.Overlays.Count - 1Step1202.'Console.WriteLine(WpfMap_Main.Overlays.Item(i).ToString)203.IfWpfMap_Main.Overlays(i).GetType.ToString =“ThinkGeo.MapSuite.WpfDesktopEdition.LayerOverlay”Then204.205.overlayCollection.Add(WpfMap_Main.Overlays(i))206.207.EndIf208.Next209.210.ForiAsInteger= 0TooverlayCollection.Count - 1Step1211.212.ForjAsInteger= 0TooverlayCollection.Item(i).Layers.Count - 1Step1213.214.'Console.WriteLine(overlayCollection.Item(i).Layers(j).GetType().ToString)215.overlayCollection.Item(i).Layers(j).Open()216.IfoverlayCollection.Item(i).Layers(j).GetType.ToString =“ThinkGeo.MapSuite.Core.ShapeFileFeatureLayer”Then217.overlayCollection.Item(i).Layers(j).Close()218.layerCollection.Add(overlayCollection.Item(i).Layers(j))219.EndIf220.Next221.222.Next223.224.ForiAsInteger= 0TolayerCollection.Count - 1Step1225.IflayerCollection.Item(i).Name = layerNameThen226.ReturnlayerCollection.Item(i)227.228.EndIf229.230.Next231.ReturnNothing232.233.Else234.ReturnNothing235.EndIf236.EndFunction