ThinkGeo.com    |     Documentation    |     Premium Support

IndexOutOfRangeException with shape file commit

I'm wondering if you can tell me what the cause of this exception is.  I attached a screen shot of the error.


I'm trying to write an export routine to take a SQLServer table reprsented by a MsSql2008FeatureLayer 


and load those features into a shape file represented by a ShapeFileFeatureLayer.


Here is a small sample of what I'm trying to do so far:


 


MsSql2008FeatureLayer sql2008Layer = new MsSql2008FeatureLayer(connectString, "CurrentField_TEST", "GEO_ID"); ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Polygon, @"C:\WKT.shp", shapeFileColumns, System.Text.Encoding.UTF8, OverwriteMode.Overwrite); //ShapeFileFeatureLayer.BuildRecordIdColumn(@"C:\WKT.shp", "GEO_ID", BuildRecordIdMode.Rebuild); ShapeFileFeatureLayer shapes = new ShapeFileFeatureLayer(@"C:\WKT.shp", ShapeFileReadWriteMode.ReadWrite); //ShapeFileFeatureLayer.BuildIndexFile(@"C:\WKT.shp"); shapes.Open(); sql2008Layer.Open(); sql2008Layer.MakeAllGeometriesValid(); Collection<feature> sqlServerFeatures = sql2008Layer.FeatureSource.GetAllFeatures(ReturningColumnsType.AllColumns); foreach (Feature item in sqlServerFeatures) { //filter columns to matching columns only Dictionary tempCollection = (from x in shapes.FeatureSource.GetColumns().AsQueryable() from y in item.ColumnValues.AsQueryable() where x.ColumnName == y.Key select y).ToDictionary(x =>x.Key, x=>x.Value); Console.WriteLine(item.GetWellKnownType().ToString()); if (item.GetWellKnownType() == WellKnownType.Multipolygon || item.GetWellKnownType() == WellKnownType.Polygon) { BaseShape shape = item.GetShape().CloneDeep(); //Do some eidit on the shape //shape.Id = item.Id; Feature newFeature = new Feature(shape, tempCollection); System.Windows.Forms.MessageBox.Show(newFeature.IsValid().ToString()); shapes.EditTools.BeginTransaction(); shapes.FeatureSource.AddFeature(newFeature); TransactionResult result = shapes.EditTools.CommitTransaction(); foreach (var item2 in result.FailureReasons.AsQueryable()) { System.Windows.Forms.MessageBox.Show(item2.Value); } } } sql2008Layer.Close(); shapes.Close(); 


The error occurs on the CommitTransaction method call.  Any ideas of what my problem is or do you have a better routine to accomplish an export of a SQL Server 2008 table to a shape file that represents that SQL Server data?



1538-Exception.JPG (27 KB)

Peter,


Thanks for your post and question.
 
Following code shows how to transfer the data from SQL Server 2008 to shape file, while one thing you have to pay attention to is when you create the dbfColumns, for simple reason, I set all the type is String, you can change whatever if you want.

string connectString = "Data Source=***.***.***.***;Initial Catalog=InternalDB;Persist Security Info=True;User ID=sa;Password=***";
MsSql2008FeatureLayer sql2008Layer = new MsSql2008FeatureLayer(connectString, "states", "recid");
 
sql2008Layer.Open();
Collection<FeatureSourceColumn> allColumns = sql2008Layer.QueryTools.GetColumns();
Collection<Feature> allFeatures = sql2008Layer.QueryTools.GetAllFeatures(ReturningColumnsType.AllColumns);
sql2008Layer.Close();
 
Collection<DbfColumn> dbfColumns = new Collection<DbfColumn>();
foreach (FeatureSourceColumn featureSourceColumn in allColumns)
{
    //Here , should be paid some attention.
    DbfColumn dbfColumn = new DbfColumn(featureSourceColumn.ColumnName, DbfColumnType.String, 50, 0);
    dbfColumns.Add(dbfColumn);
}
 
string targetShapeFilePath =@"C:\tmp\post6761testdata.shp";
ShapeFileFeatureLayer.CreateShapeFile(ShapeFileType.Polygon, targetShapeFilePath, dbfColumns, System.Text.Encoding.UTF8, OverwriteMode.Overwrite);
ShapeFileFeatureLayer shapes = new ShapeFileFeatureLayer(targetShapeFilePath, ShapeFileReadWriteMode.ReadWrite);
shapes.Open();
 
shapes.EditTools.BeginTransaction();
 
foreach (Feature feature in allFeatures)
{
    shapes.EditTools.Add(feature);
}
 
TransactionResult result = shapes.EditTools.CommitTransaction(); 
shapes.Close();
 
if (result.TotalFailureCount > 0)
{
    MessageBox.Show(string.Format("{0} failed", result.TotalFailureCount));
}

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