ThinkGeo.com    |     Documentation    |     Premium Support

Trying to write a shapefile

I am trying to export an empty shapefile so that I can then add a WKT feature to it for viewing. Please assume the WKT string is correct as it passes validation. When trying to create the file I receive the following error:


 


  Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim feature As New Feature(TextBox1.Text)

        Dim s As New DbfColumn("filler", DbfColumnType.String, 1, 1)
        Dim colms As New System.Collections.ObjectModel.Collection(Of DbfColumn) 
        colms.Add(s)

        ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Polygon, "C:\zzz\WKT.shp", colms, System.Text.Encoding.Default, OverwriteMode.Overwrite)

        Dim fL As New ShapeFileFeatureLayer("C:\zzz\WKT.shp", ShapeFileReadWriteMode.ReadWrite)
        fL.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(180, 230, 0, 0), GeoColor.SimpleColors.Black)
        fL.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20

        ShapeFileFeatureLayer.BuildIndexFile("C:\zzz\empty.shp")

        WinformsMap1.MapUnit = GeographyUnit.Meter
        fL.Open()
        fL.EditTools.BeginTransaction()
        fL.FeatureSource.AddFeature(feature)
        fL.EditTools.CommitTransaction()
        Dim extent As RectangleShape = fL.GetBoundingBox
        fL.Close()

        WinformsMap1.CurrentExtent = extent
        WinformsMap1.StaticOverlay.Layers.Add("emerald", fL)
        WinformsMap1.Refresh()

    End Sub


When trying to do this I receive the following error:


The process cannot access the file 'C:\zzz\TMPWKT.dbf' because it is being used by another process.


What am I doing wrong?



Nelson, 
  
   What line is throwing the exception?   
  
   On thing you can consider if that if you do not need keep the results around you do not need to use the ShapeFileFeatureLayer.  You could use instead the InMemoryFeatureLayer.  This keeps things in memory and if you don’t have thousands of shapes it does pretty good.  On advantage is that it is all in memory.  You can also bypass the transaction system by putting the features directly in by using the .InternalFeatures dictionary.  Just offering that up, not sure if it fits your scenario.   
  
 Also just to make sure are you using the latest version from a week ago? 
  
 I also noticed that on the line where you build the spatial index you are specifing a different shape file from the one you just created.  The one you created was called WKT.shp and the one you use to build the index is empty.shp.  I think when you create the shape file it will create an index and I don’t think you need to build it but I could be wrong.  After you call the create shape file then look if it has an idx and ids files.  If they exist then you don’t need to build it. 
  
 David

I had corrected the index building file ,sorry about that. The line that threw the exception was the ShapeFileFeatureLayer.CreateShapeFile line. I worked aroudn this by creating an empty shapefile in ArcMap. Yes, it makes more sense to use the InMemoryFeatureLayer, I hadn’t thought of it. It actually makes a lot more sense.  
  
 I am not 100% positive I am using the most new version but I think I am. What is a good way to determine this? 
  
 Thanks.

Nelson, 
  
 The best way is to call the GetVersion api usually on the map control or in the case of the Service Edition it is on the MapEngine class.  I am going to try and re-create that error, it is strange.  We have test cases to test the create shape file and I know we had an issue with this a ways back. 
  
 David

My map reports version 3.1.4 but the website says the latest version is 3.0.184. I noticed the web edition version is 3.1.4 though so is it possible I’ve mixed dlls somehow?

Also, I noticed that there are some issues regarding threading in this latest release. I am running a Quad Core so perhaps this may be related?

Nelson, 



The following code 


Dim s As New DbfColumn("filler", DbfColumnType.String, 1, 1)

Should be 


Dim s As New DbfColumn("filler", DbfColumnType.String, 1, 0)



The last parameter for DbfColumn is Decimal Length, which only makes sense when the column type is double. For any other types, this should be 0. We will throw some exception here so you will easily see that. 

Desktop Edition includes 2 main assemblies, MapSuiteCore.dll is with the version 3.1.4 but the DesktopEdition.dll is in 3.0.184. That’s because MapSuiteCore.dll is final released while Desktop is still in Beta. So you are right and the website says also right :) 

There is some cross threading issue introduced in the latest version, which should not be your case though as we don’t have multi threading there on creating a new shape file. If you still have the problem, can you just replace the code by “File.Delete(“C:\zzz\WKT.shp”) to make sure it is not used by another process outside the application? 



Thanks, 

Ben 

 



I got the same "is being used by another process." message when i tried to delete the shape file in the application. I had to make sure all the layers using that file are closed. Hope it helps. 


Thanks for letting us know, Alta.