I am running into an issue when I try to create shapefiles from a table. We have tables set up that describe shapefile type, field names/types, and we are hard coding some default field lengths for those fields.
I am running into problems with the end result. The process below ends up creating .dbc, .dbf, .ids, .idx, .prj (copied through different means), .shp, and .shx files. The shapefile opens up in our ThinkGeo application, but it doesn’t not open up in ArcMap. Errors vary from saying that the shapes don’t match the records in the DBF to just a general cannot open the file error. When I open the created shapefiles in qGIS, It seems to work, but there are extra fields appended to the attribute table.
Here is the code for this part:
01.PrivateSharedSubProcessShapefiles()02.'explanation of the items in the m_LayerFieldsToBeCreated dictionary03.'the keys are the names of the shapefiles… in my case, they are generic being layer1, layer2, layer3, etc…04.'the inner dictionary is as follows05.'–key = field/column name… ie. UNIQUEID, ID, GEOMETRY, Field1, Field2, etc… there may be 20-30 columns +/-06.'in the object(), there are 2 items… (0) = DbfColumnType…(1) = optional field length for character types07.08.'need to go through the dictionary to create new layer files09.ForEachitemAsKeyValuePair(OfString, Dictionary(OfString,Object()))Inm_LayerFieldsToBeCreated10.11.'create collection to hold the dbf columns12.DimdbfColumnsAsNewList(Of DbfColumn)13.14.'create the DBF columns15.ForEachpieceAsKeyValuePair(OfString,Object())Initem.Value16.'create the new column17.DimnewColumnAsDbfColumn =Nothing18.19.'set the properties of the new column20.SelectCaseDirectCast(piece.Value(0), DbfColumnType)21.CaseDbfColumnType.Character22.newColumn =NewDbfColumn(piece.Key, piece.Value(0), piece.Value(1), 0)'in this case, piece.Value(0) = DbfColumnType.Character, piece.value(1) = a length (only as high as 254), no decimals for character23.CaseDbfColumnType.Float24.newColumn =NewDbfColumn(piece.Key, piece.Value(0), 13, 6)25.CaseDbfColumnType.Numeric26.newColumn =NewDbfColumn(piece.Key, piece.Value(0), 9, 0)27.EndSelect28.29.'making sure that something was created during the Select Case operation30.IfnewColumn IsNotNothingThen31.Debug.WriteLine(newColumn.ColumnName &" "& newColumn.ColumnType.ToString)32.'populating a list of the new columns33.dbfColumns.Add(newColumn)34.EndIf35.Next36.37.Try38.'create the shapefile39.'m_LayersToBeCreated is a dictionary where the key is the layer name and the value is the a ShapeFileType40.Debug.WriteLine(m_LayersToBeCreated.Item(item.Key).ToString &" "&"." & item.Key & “.shp”)41.42.'create the shapefile - I tried this as a standard in case there were issues in my iteration, but it doesn’t work either43.'ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Point, "." & item.Key & “.shp”, dbfColumns, System.Text.Encoding.UTF8, OverwriteMode.Overwrite)44.45.'I have tried using iteration to generate a new shapefile that adjusts for what was in my table46.SelectCasem_LayersToBeCreated.Item(item.Key)47.CaseShapeFileType.Point48.ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Point,"." & item.Key & “.shp”, dbfColumns, System.Text.Encoding.UTF8, OverwriteMode.Overwrite)49.CaseShapeFileType.Polygon50.ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Polygon,“." & item.Key & “.shp”, dbfColumns, System.Text.Encoding.UTF8, OverwriteMode.Overwrite)51.CaseShapeFileType.Polyline52.ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Polyline,“." & item.Key & “.shp”, dbfColumns, System.Text.Encoding.UTF8, OverwriteMode.Overwrite)53.EndSelect54.55.'thinking there might be an issue with the shapefile, I tried adding in an option to rebuild… still didn’t work56.'ShapeFileFeatureSource.Rebuild(item.Key & “.shp”)57.58.59.'assign the projection file— all the shapefiles to be created use the same projection that is assigned as a generic projection.prj file60.System.IO.File.Copy(”.\Projection.prj”, item.Key &“.prj”)61.62.CatchexAsException63.'need to specify down later64.EndTry65.Next66.67.68.69.EndSub
For some reason, I am able to pull data from a spreadsheet and create a new shapefile from that. I am including that code in here as well. I don’t specifically see a big difference between the two methodologies so I don’t know where things are going wrong when reading the other way. This one is doing pretty much the same thing except type checking the spreadsheet’s fields in order to determine the field type for the DbfColumn.
Here is the code for that part:
01.PrivateSubExportSpreadsheetToShapefile(ByValsenderAsObject,ByValeAsDoWorkEventArgs)02.03.Dimparams()AsObject= e.Argument04.05.DimxColumnNameAsString= params(0)06.DimyColumnNameAsString= params(1)07.DimcolumnListAsList(OfString) = params(2)08.DimtableNameAsString= params(3)09.DimshapefileTextAsString= params(4)10.11.'query the database to get the information12.ExportToShapefileSpreadsheet.GetSpreadsheetData(tableName, columnList)13.14.'construct the shapefile15.'create collection of datacolumns16.DimdbfCollectionAsNewList(Of DbfColumn)17.18.ForiAsInteger= 0ToExportToShapefileSpreadsheet.m_SelectedSpreadsheetData.Tables(“Table”).Columns.Count - 1Step119.DimitemAsDataColumn = ExportToShapefileSpreadsheet.m_SelectedSpreadsheetData.Tables(“Table”).Columns(i)20.’ Dim newColumn As New DbfColumn(item.ColumnName, item.DataType.ToString, item.MaxLength)21.DimnewColumnAsDbfColumn =Nothing22.Console.WriteLine(item.ColumnName &" "& item.DataType.ToString &" "& item.ColumnName.Length)23.24.'select the datatype based off the spreadsheet25.SelectCaseitem.DataType26.27.CaseGetType(Char),GetType(String)28.newColumn =NewDbfColumn(item.ColumnName.ToString.Trim(), DbfColumnType.Character, 254, 0)29.CaseGetType(Date),GetType(DateTime)30.newColumn =NewDbfColumn(item.ColumnName.ToString.Trim, DbfColumnType.Date, 8, 0)31.CaseGetType(Double),GetType(Single)32.newColumn =NewDbfColumn(item.ColumnName.ToString.Trim, DbfColumnType.Float, 13, 5)33.CaseGetType(Integer)34.newColumn =NewDbfColumn(item.ColumnName.ToString.Trim, DbfColumnType.Numeric, 9, 0)35.EndSelect36.37.'add to the collection38.IfnewColumn IsNotNothingThen39.dbfCollection.Add(newColumn)40.EndIf41.Next42.43.'create the shapefile----here this is only points though and I set the encoding-- in the previous example, I also tried setting the same encoding and overwriting but that didn’t work either.44.ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Point, shapefileText, dbfCollection, System.Text.Encoding.UTF8, OverwriteMode.Overwrite)45.EndSub
My biggest issue is that the top code block ends up creating .dbc files and my .dbf isn’t configured properly. When I try to open the .dbf in Excel, it is all jumbled and not in a tabular format. I am not exactly sure why the second process works for this, but the top one doesn’t.
I have tried this on version 8.0 and 8.0.115.0. Both do the same things with the top code.
Any advice would be great. Thanks!


