We want to allow users to use the drawing tools and make marks on a map and save that as a shape file.
The problem I am having is the TrackShapeLayer that the users mark up is on does not have any ColumnValues for the features and the shape file generation fails saying the database columns should have at least one value.
Below is the code I was using, I also tried creating dummy values for the columns but it did not work. I have verified that a polyline exists on the layer when testing.
privatevoidPrintToNewShapeFile(stringshapeName){InMemoryFeatureLayer layer = _mapControl.TrackOverlay.TrackShapeLayer;
WellKnownType shapeType = WellKnownType.Point;ShapeFileType shapeFileType = (ShapeFileType)shapeType;
layer.Open();InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)layer;Collection<Feature> allFeatures = inMemoryFeatureLayer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
//foreach (Feature feature in allFeatures)//{// feature.ColumnValues.Add(Guid.NewGuid().ToString(), "Key");//}Collection<FeatureSourceColumn> allColumns = inMemoryFeatureLayer.QueryTools.GetColumns();
//if (allColumns == null || !allColumns.Any())//{// foreach (Feature allFeature in allFeatures)// {// if (allColumns == null)// {// allColumns = new Collection<FeatureSourceColumn>();// }// var columnValue = new FeatureSourceColumn(allFeature.ColumnValues.First().Value, "String", 250);// allColumns.Add(columnValue);// }//}
Collection<DbfColumn> dbfColumns =newCollection<DbfColumn>();foreach(FeatureSourceColumn featureSourceColumninallColumns){DbfColumn col = GetDbfColumnFromFeatureSourceColumn(featureSourceColumn);if(col !=null){dbfColumns.Add(col);}}
ShapeFileFeatureSource.CreateShapeFile(shapeFileType,shapeName, dbfColumns, Encoding.ASCII, OverwriteMode.Overwrite);ShapeFileFeatureSource shapeFileLayer =newShapeFileFeatureSource(shapeName, ShapeFileReadWriteMode.ReadWrite);
shapeFileLayer.Open();
foreach(Feature featureinallFeatures){Dictionary<string,string> shapeColumnValues =newDictionary<string,string>();BaseShape baseShape = feature.GetShape();
// Loop through all columns and add their valuesforeach(FeatureSourceColumn columninallColumns){stringfeatureColumnValue = feature.ColumnValues[column.ColumnName].ToString();shapeColumnValues.Add(column.ColumnName, featureColumnValue);}
Feature newf =newFeature(baseShape, shapeColumnValues);shapeFileLayer.BeginTransaction();shapeFileLayer.AddFeature(newf);TransactionResult result = shapeFileLayer.CommitTransaction();Console.WriteLine(result.TransactionResultStatus);}
shapeFileLayer.Close();layer.Close();
}
privatestaticDbfColumn GetDbfColumnFromFeatureSourceColumn(FeatureSourceColumn featureSourceColumn){switch(featureSourceColumn.TypeName){case"String":returnnewDbfColumn(featureSourceColumn.ColumnName, DbfColumnType.String, featureSourceColumn.MaxLength, 0);case"Memo":returnnewDbfColumn(featureSourceColumn.ColumnName, DbfColumnType.Memo, featureSourceColumn.MaxLength, 0);case"Integer":returnnewDbfColumn(featureSourceColumn.ColumnName, DbfColumnType.Integer, featureSourceColumn.MaxLength, 0);case"Logical":returnnewDbfColumn(featureSourceColumn.ColumnName, DbfColumnType.Logical, featureSourceColumn.MaxLength, 0);case"Double":returnnewDbfColumn(featureSourceColumn.ColumnName, DbfColumnType.Double, 0, featureSourceColumn.MaxLength);case"Date":returnnewDbfColumn(featureSourceColumn.ColumnName, DbfColumnType.Date, featureSourceColumn.MaxLength, 0);default:returnnull;}}