/// <summary> /// Creates shapefile from specified database table. /// Geometry or GeometryText columns are expected /// </summary> private MapWinGIS.Shapefile LoadShapefile(string sql) { this.CheckConnection(); MapWinGIS.Shapefile sf = null; if (m_callback != null) { m_callback.Progress("", 0, "Extracting data..."); } System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); DbDataAdapter adapter = this.CreateAdapter(); adapter.SelectCommand = this.CreateCommand(); adapter.SelectCommand.Connection = m_connection; adapter.SelectCommand.CommandText = sql; DataTable dt = new DataTable(); adapter.Fill(dt); int index = dt.Columns.IndexOf("Geometry"); if (index >= 0 && dt.Rows.Count > 0) { // get shapefile type by the first shape MapWinGIS.ShpfileType shpType; MapWinGIS.Shape shape = new MapWinGIS.Shape(); if (shape.ImportFromBinary(dt.Rows[0][index])) { shpType = shape.ShapeType; } else { return(null); } sf = new MapWinGIS.Shapefile(); sf.CreateNew("", shpType); foreach (DataColumn column in dt.Columns) { MapWinGIS.Field field = new MapWinGIS.Field(); field.Name = column.ColumnName; switch (column.DataType.Name.ToLower()) { case "int32": case "int64": field.Type = MapWinGIS.FieldType.INTEGER_FIELD; break; case "double": field.Type = MapWinGIS.FieldType.DOUBLE_FIELD; break; case "string": field.Type = MapWinGIS.FieldType.STRING_FIELD; break; case "byte[]": continue; default: continue; } int fieldIndex = sf.NumFields; bool result = sf.EditInsertField(field, ref fieldIndex, null); System.Diagnostics.Debug.Print(result.ToString()); } int shapeIndex = dt.Rows.Count; shape = null; int percent = 0; for (int i = 0; i < dt.Rows.Count; i++) { if (m_callback != null && dt.Rows.Count > 1) { int newPercent = Convert.ToInt32((double)i / (double)(dt.Rows.Count - 1) * 100.0); if (newPercent > percent) { m_callback.Progress("", newPercent, "Creating shapefile..."); percent = newPercent; } } DataRow row = dt.Rows[i]; shape = new MapWinGIS.Shape(); if (shape.ImportFromBinary(row[index])) { shapeIndex = sf.NumShapes; if (sf.EditInsertShape(shape, ref shapeIndex)) { for (int j = 0; j < sf.NumFields; j++) { sf.EditCellValue(j, shapeIndex, row[j + 1]); } } } } if (m_callback != null) { m_callback.Progress("", 100, ""); } } watch.Stop(); Debug.Print("Finished: " + watch.Elapsed.ToString()); return(sf); }