ThinkGeo.com    |     Documentation    |     Premium Support

Serialization of ShapeFileFeatureLayers

Hi,


I am in the process of developing the functionality to save, then restore the WinFormsMap and all the layers it contains.  I am loading MsSql2008FeatureLayers and ShapeFileFeatureLayers.  I have written classes to inherit from each of these so I can add a few additional properties.  In my earler attempts I only serialized MsSql2008FeatureLayers and everything worked fine.  However, when I try to serialize ShapeFileFeatureLayers I get an error when I try to deserialize.  Here is the code for serilization and deserialization:


 



      '***** SERIALIZE SNIPPET
      '***** Open a file stream to use for serialization
      oFormatter = New BinaryFormatter()
      oStream = New FileStream(sFileName, FileMode.Create, FileAccess.Write, FileShare.None)

      '***** Save some of the map's property
      oMapProps = New MapProperties
      oLayerOverLay = CType(oMap.Overlays("Static"), LayerOverlay)
      With oMapProps
         .BoundingBox = oMap.CurrentExtent
         .MapUnit = oMap.MapUnit
         .NumberOfLayers = oLayerOverLay.Layers.Count
         .CoordinateSystemInfo = oMap.CoordinateSystem
      End With

      oFormatter.Serialize(oStream, oMapProps)

      '***** Now save all the layers, save the bottom one first so the order will 
      '***** be correct when deserialized
      For i = 0 To oLayerOverLay.Layers.Count - 1
         oFormatter.Serialize(oStream, oLayerOverLay.Layers(i))
      Next i

      oStream.Close()

      '***** DESERIALIZE SNIPPET
         '***** Open a file stream to use for deserialization
         oFormatter = New BinaryFormatter()
         oStream = New FileStream(sWorksetFile, FileMode.Open, FileAccess.Read, FileShare.Read)

         For i = 0 To oMapProps.NumberOfLayers - 1
            oObjLayer = oFormatter.Deserialize(oStream)
               oShapefileFeatureLayer = CType(oObjLayer, CustomShapefileFeatureLayer)
               oLayerOverLay.Layers.Add(oShapefileFeatureLayer.Name, oShapefileFeatureLayer)
               If oShapefileFeatureLayer.IsProjected Then
                  Dim oProj4 As New ManagedProj4Projection
                  oProj4.InternalProjectionParameters = oShapefileFeatureLayer.InternalProjParams
                  oProj4.ExternalProjectionParameters = oShapefileFeatureLayer.ExternalProjParams
                  oProj4.Open()
                  oShapefileFeatureLayer.FeatureSource.Projection = oProj4
               End If
               SetBoundingBox(oShapefileFeatureLayer)
               'oShapefileFeatureLayer.BoundingBox = oShapefileFeatureLayer.GetBoundingBox()
            End If


The problem manifests when I try to use the GetBoundingBox Method on the ShapeFileFeatureLayer.  I get the error "Object reference not set to an instance of an object."  Here is the stack trace:


   ThinkGeo.MapSuite.Core.ShapeFileFeatureSource.GetBoundingBoxCore()

       GeoFrame.EXE: N 00048

   ThinkGeo.MapSuite.Core.FeatureSource.GetBoundingBox()

       GeoFrame.EXE: N 00243

   ThinkGeo.MapSuite.Core.FeatureLayer.GetBoundingBoxCore()

       GeoFrame.EXE: N 00079

   ThinkGeo.MapSuite.Core.Layer.GetBoundingBox()

       GeoFrame.EXE: N 00075

   MapUtilities.LayerUtils.SetBoundingBox(oFeatureLayer As FeatureLayer)

       GeoFrame.EXE: N 00107

   MapUtilities.LayerUtils.OpenWorkingSet(oMap As CustomWinformsMap, sWorksetFile As String)

       GeoFrame.EXE: N 01167


I have been searching the forums for any clues and I read that the CloneDeep method uses Serialize in its implementation, with the limitation that it does not inclued Encoding.  And, indeed, if I query the encoding of the ShapeFileFeatureLayer that has just been deserialized I get the error:


System.ArgumentNullException was unhandled

  Message=The parameter you supplied may not be null.

Parameter name: DBaseEngine

  Source=MapSuiteCore

  ParamName=DBaseEngine

  StackTrace:

       at ThinkGeo.MapSuite.Core.x6d719af406ea4c2c.xddc71efc9badd3c7(Object xf85c38ac7d1447ea, String x34decc57f0820440)

       at ThinkGeo.MapSuite.Core.ShapeFileFeatureSource.get_Encoding()

       at ThinkGeo.MapSuite.Core.ShapeFileFeatureLayer.get_Encoding()

  InnerException: 


When I try to set the encoding to System.Text.Encoding.Default (as a workaround) I get the same error.  Don't know if this is related to the error I get calling GetBoundingBox or not.


Any ideas?


Thanks,


Steve


 






 


 



Steven, 
  
 Thanks for your post and questions. 
  
 I do not think the exception caused by the GetBoundingBox is the same error with the Encoding problem. I doubt it is related with the Projection set to the layer cannot be sterilized. While, also I noticed the layer is customized one called CustomShapefileFeatureLayer, could you show me a complete example to recreate this problem? 
  
 Any more questions please feel free to let me know. 
  
 Thanks. 
  
 Yale

Yale, 
  
 Thanks for your response.  I will send a sample to you shortly. 
  
 Steve

Steven,


Try the following code when you deserialize the shape file feature layer, hope it can fix your problem.



oShapefileFeatureLayer = CType(oFormatter.Deserialize(oStream), ShapeFileFeatureLayer)
oShapefileFeatureLayer.Close()
oShapefileFeatureLayer.Open()

 
Any more questions please feel free to let me know.
 
Thanks.
 
Yale

Yale, 



Sorry to take so long to reply.  Thanks for your solution, it works perfectly! 



Steve



Steven, 
  
 Thanks for letting me know this. 
  
 Any more questions please feel free to let me know. 
  
 Thanks. 
  
 Yale