/// <summary> /// Reads the complete feature set from the database /// </summary> /// <param name="connString">sqlite db connection string</param> /// <param name="featureSetInfo">information about the table</param> /// <param name="sql">the sql query</param> /// <returns>the resulting feature set</returns> public IFeatureSet ReadFeatureSet(string connString, GeometryColumnInfo featureSetInfo, string sql) { FeatureSet fs = new SpatiaLiteFeatureSet(featureSetInfo.GeometryType); fs.IndexMode = false; //setting the initial index mode.. using (var cmd = CreateCommand(connString, sql)) { cmd.Connection.Open(); RunInitialCommands(cmd.Connection); //DotSpatial.Topology.Utilities.WkbReader wkbr = new DotSpatial.Topology.Utilities.WkbReader(); var wkbr = new SpatiaLiteWkbReader(); var rdr = cmd.ExecuteReader(); var columnNames = PopulateTableSchema(fs, featureSetInfo.GeometryColumnName, rdr); while (rdr.Read()) { var wkb = rdr[featureSetInfo.GeometryColumnName] as byte[]; var geom = wkbr.Read(wkb); var newFeature = fs.AddFeature(geom); //populate the attributes foreach (var colName in columnNames) { newFeature.DataRow[colName] = rdr[colName]; } } cmd.Connection.Close(); fs.Name = featureSetInfo.TableName; //TODO: look into this more //HACK required for selection to work properly fs.IndexMode = true; //assign projection var proj = ProjectionInfo.FromEpsgCode(featureSetInfo.SRID); fs.Projection = proj; return(fs); } }
//finds out the geometry column information.. private GeometryColumnInfo FindGeometryColumnInfo(string connString, string sqlQuery) { GeometryColumnInfo result = null; using (var cmd = CreateCommand(connString, sqlQuery)) { cmd.Connection.Open(); RunInitialCommands(cmd.Connection); var wkbr = new SpatiaLiteWkbReader(); var rdr = cmd.ExecuteReader(CommandBehavior.SingleRow); var schemaTable = rdr.GetSchemaTable(); foreach (DataRow r in schemaTable.Rows) { var colName = Convert.ToString(r["ColumnName"]); var colDataType = Convert.ToString(r["DataType"]); //if BLOB, then assume geometry column if (Type.GetType(colDataType) == typeof(byte[])) { result = new GeometryColumnInfo(); result.GeometryColumnName = colName; break; } } if (result != null && rdr.HasRows) { rdr.Read(); var blob = rdr[result.GeometryColumnName] as byte[]; var geom = wkbr.Read(blob); result.GeometryType = GetGeometryType(Convert.ToString(rdr["type"])); } cmd.Connection.Close(); return(result); } }