private string sql_CreateTable(MG_FieldSet fieldSet, MG_GeometryType gt) {// oid, field1, field2, field3, field4,...fieldN, geom //CREATE TABLE point2 ( oid SERIAL PRIMARY KEY, name1 VARCHAR, name2 VARCHAR); SELECT AddGeometryColumn('public','point2','geom',0,'POINT',2); // table name, column name must be lowercase, e.g. pointTABLE --->pointtable string front = "CREATE TABLE {0} ( oid SERIAL PRIMARY KEY"; string table = fieldSet.GetName().ToLower(); front = String.Format(front, table); string m = ", {0} {1}"; string mid = ""; for (int i = 0; i < fieldSet.Count(); i++) { MG_Field field = fieldSet.GetAt(i); mid += String.Format(m, field.Name.ToLower(), field.Type.ToString()); } string end = "); SELECT AddGeometryColumn('public','{0}','geom',0,'{1}',2);"; end = String.Format(end, table, gt.ToString()); string strSQL = front + mid + end; return(strSQL); }
public static FieldDefn ToFieldDefn(MG_Field field) { FieldType type = ToFieldType(field.Type); FieldDefn def = new FieldDefn(field.Name, type); def.SetWidth(field.Width); def.SetPrecision(field.Precision); return(def); }
public static MG_Field AsField(FieldDefn f) { MG_Field mgField = new MG_Field(); mgField.Name = f.GetNameRef(); //collect data mgField.Type = AsFieldDBType(f.GetFieldType()); //collect data mgField.Width = f.GetWidth(); //collect data mgField.Precision = f.GetPrecision(); //collect data return(mgField); }
public static FeatureDefn ToFeatureDefn(MG_FieldSet fieldSet) { FeatureDefn def = new FeatureDefn(null); for (int i = 0; i < fieldSet.Count(); i++) { MG_Field field = fieldSet.GetAt(i); FieldDefn fdef = ToFieldDefn(field); def.AddFieldDefn(fdef); } return(def); }
public static MG_FieldSet AsFieldSet(FeatureDefn f) { MG_FieldSet fieldSet = new MG_FieldSet(f.GetName());// name int count = f.GetFieldCount(); for (int i = 0; i < count; i++) { MG_Field field = AsField(f.GetFieldDefn(i)); fieldSet.Add(field); } return(fieldSet); }
public static void CreateShapeFile(MG_Layer mgLayer, string filePath) { string dataSourceName = filePath; string layerName = Path.GetFileNameWithoutExtension(filePath); mgLayer.SetLayerPath(filePath); try { Ogr.RegisterAll(); string driverName = "ESRI Shapefile"; Driver driver = Ogr.GetDriverByName(driverName); if (File.Exists(dataSourceName)) { //System.IO.File.Delete(dataSourceName);// only remove xxx.shp driver.DeleteDataSource(dataSourceName);// reomve xxx.shp dbf shx } DataSource ds = driver.CreateDataSource(dataSourceName, new string[] { }); wkbGeometryType gt = TowkbGeometryType(mgLayer.Type);// retrieve data Layer layer = ds.CreateLayer(layerName, null, gt, new string[] { }); // create three files xxx.shp xxx.dbf xxx.shx // fieldset int fieldCount = mgLayer.FieldSet.Count(); for (int i = 0; i < fieldCount; i++) { MG_Field field = mgLayer.FieldSet.GetAt(i); FieldDefn def = ToFieldDefn(field); layer.CreateField(def, 1); } // feature int featureCount = mgLayer.GetFeatureCount(); for (int i = 0; i < featureCount; i++) { Feature f = ToFeature(mgLayer.GetFeature(i)); layer.CreateFeature(f); f.Dispose(); } // flush data to file layer.Dispose(); ds.Dispose(); driver.Dispose(); } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } }
protected MG_FieldSet GetFieldSet(string table) { ArrayList columns = this.GetColumnNames(table); ArrayList types = this.GetColumnTypes(table); if (columns == null || types == null) { return(null); } int columnCount = columns.Count; int typeCount = types.Count; if (columnCount != typeCount) { return(null); } MG_FieldSet fieldSet = new MG_FieldSet(table); // oid .... geom for (int i = 0; i < columnCount; i++) { string column = columns[i].ToString(); string type = types[i].ToString(); if (!column.Equals("oid") && !column.Equals("geom")) { // oid name geom no length // integer character varying USER-DEFINED integer double precision MG_FieldDBType dbType = MG_FieldDBType.VARCHAR; if (type.Equals("integer")) { dbType = MG_FieldDBType.INTEGER; } else if (type.Equals("character varying")) { dbType = MG_FieldDBType.VARCHAR; } else if (type.Equals("double precision")) { dbType = MG_FieldDBType.FLOAT8; } MG_Field field = new MG_Field(column, dbType); fieldSet.Add(field); } } return(fieldSet); }
private DataTable GetDataTable(MG_Layer layer) { // Create a new DataTable. DataTable table = new DataTable(layer.GetLayerName()); // add oid DataColumn oid_Column = new DataColumn("OID", Type.GetType("System.Int32")); table.Columns.Add(oid_Column); int i, j; // Add the column for (i = 0; i < layer.GetFieldSet().Count(); i++) { MG_Field field = layer.GetFieldSet().GetAt(i); DataColumn column = new DataColumn(field.Name); column.DataType = Type.GetType("System.String"); // Add the Column to the DataColumnCollection. table.Columns.Add(column); } // Add the row for (i = 0; i < layer.GetFeatureCount(); i++) { MG_Feature f = layer.GetFeature(i); DataRow row = table.NewRow(); // add oid row[0] = i + 1; for (j = 0; j < f.GetFieldCount(); j++) { object value = f.GetValue(j).Value; if (value != null) { row[j + 1] = value.ToString(); } } // Add the row to the DataRowCollection. table.Rows.Add(row); } return(table); }