/// <summary> /// Ogr to DotSpatial at feature field level /// </summary> /// <param name="fieldDefn">Ogr FieldDefn</param> /// <returns>DataColumn of DataTable of FeatureSet</returns> public static DataColumn Ogr2DSField(OSGeo.OGR.FieldDefn fieldDefn) { DataColumn dataColumn = new DataColumn(); dataColumn.ColumnName = fieldDefn.GetName(); dataColumn.Caption = fieldDefn.GetName();//column title OSGeo.OGR.FieldType fieldType = fieldDefn.GetFieldType(); switch (fieldType) { case OSGeo.OGR.FieldType.OFTInteger: dataColumn.DataType = typeof(int); break; case OSGeo.OGR.FieldType.OFTString: dataColumn.DataType = typeof(string); break; case OSGeo.OGR.FieldType.OFTDate: dataColumn.DataType = typeof(DateTime); break; case OSGeo.OGR.FieldType.OFTReal: dataColumn.DataType = typeof(double); break; default: dataColumn.DataType = typeof(string); break; } return(dataColumn); }
private void dataGrid_CellEditEnding(Object sender, DataGridCellEditEndingEventArgs e) { try { int rowid = e.Row.GetIndex(); int colid = e.Column.DisplayIndex; //get fid int fid = Convert.ToInt32(dt.Rows[rowid][0]); //get fieldname String fn = (String)e.Column.Header; OSGeo.OGR.FieldDefn fdn = null; if (fn != null && featuresource.schema.fields.TryGetValue(fn, out fdn)) { if (fdn != null) { Object value = null; switch (fdn.GetFieldType()) { case OSGeo.OGR.FieldType.OFTInteger: value = Convert.ToInt32(((TextBox)e.EditingElement).Text); break; case OSGeo.OGR.FieldType.OFTReal: value = Convert.ToDouble(((TextBox)e.EditingElement).Text); break; case OSGeo.OGR.FieldType.OFTString: value = ((TextBox)e.EditingElement).Text; break; case OSGeo.OGR.FieldType.OFTWideString: value = ((TextBox)e.EditingElement).Text; break; } if (value != null) { featuresource.features.GetFeatureByID(fid).ReviseAttribute(fn, value); } } } } catch (InvalidCastException) { MessageBox.Show("新属性值必须与原属性值具有相同类型!"); } }
/// <summary> /// Reads the field types from the OgrFeatureDefinition -> OgrFieldDefinition /// </summary> /// <param name="fdt">FeatureDatatTable</param> /// <param name="oLayer">OgrLayer</param> private static void ReadColumnDefinition(FeatureDataTable fdt, OgrLayer oLayer) { using (OgrFeatureDefn ogrFeatureDefn = oLayer.GetLayerDefn()) { int iField; for (iField = 0; iField < ogrFeatureDefn.GetFieldCount(); iField++) { using (OgrFieldDefn ogrFldDef = ogrFeatureDefn.GetFieldDefn(iField)) { OgrFieldType type = ogrFldDef.GetFieldType(); switch (type) { case OgrFieldType.OFTInteger: fdt.Columns.Add(ogrFldDef.GetName(), typeof(Int32)); break; case OgrFieldType.OFTReal: fdt.Columns.Add(ogrFldDef.GetName(), typeof(Double)); break; case OgrFieldType.OFTString: fdt.Columns.Add(ogrFldDef.GetName(), typeof(String)); break; case OgrFieldType.OFTWideString: fdt.Columns.Add(ogrFldDef.GetName(), typeof(String)); break; case OgrFieldType.OFTDate: case OgrFieldType.OFTTime: case OgrFieldType.OFTDateTime: fdt.Columns.Add(ogrFldDef.GetName(), typeof(DateTime)); break; default: { //fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.String")); Debug.WriteLine("Not supported type: " + type + " [" + ogrFldDef.GetName() + "]"); break; } } } } } }
public static void SetField(this OSGeo.OGR.Feature feature, OSGeo.OGR.FieldDefn fieldDefn, object value) { if (feature == null || fieldDefn == null) { return; } var fieldName = fieldDefn.GetName(); var fieldType = fieldDefn.GetFieldType(); if (!DBNull.Value.Equals(value)) { switch (fieldType) { case OSGeo.OGR.FieldType.OFTString: feature.SetField(fieldName, value.ToString()); break; case OSGeo.OGR.FieldType.OFTInteger: if (value is int intValue) { feature.SetField(fieldName, intValue); } break; case OSGeo.OGR.FieldType.OFTReal: if (value is double doubleValue) { feature.SetField(fieldName, doubleValue); } break; default: throw new NotImplementedException(); } } }
public static void map_fields(OSGeo.OGR.Layer ogr_layer, out System.Collections.Hashtable outFieldMap, out ESRI.ArcGIS.Geodatabase.IFields outFields, out ESRI.ArcGIS.Geodatabase.esriDatasetType outDatasetType, out ESRI.ArcGIS.Geometry.esriGeometryType outGeometryType, out int outShapeIndex, out int outOIDFieldIndex, out ISpatialReference outSpatialReference) { outSpatialReference = null; outFields = null; outDatasetType = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable; // start assuming it is a table outGeometryType = esriGeometryType.esriGeometryNull; //don't know what it is outOIDFieldIndex = -1; outShapeIndex = -1; outFieldMap = new System.Collections.Hashtable(); System.Collections.ArrayList fieldArray = new System.Collections.ArrayList(); OSGeo.OGR.FeatureDefn featDef = ogr_layer.GetLayerDefn(); int fieldsInserted = 0; // OIDs and Geometries can be pseudo fields in GDAL and are thus *may* not included in the OGR FieldDef // To account for that add those first (if they exist) and keep a mapping of fields using // fieldsInserted ////////////////////////////// // // handle oid field pseudo column // ESRI.ArcGIS.Geodatabase.IFieldEdit2 oidFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); if (ogr_layer.GetFIDColumn().Length > 0) { oidFieldEdit.Name_2 = ogr_layer.GetFIDColumn(); oidFieldEdit.AliasName_2 = ogr_layer.GetFIDColumn(); } else { oidFieldEdit.Name_2 = "FID"; oidFieldEdit.AliasName_2 = "FID"; } oidFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID; fieldArray.Add(oidFieldEdit); outOIDFieldIndex = fieldsInserted; fieldsInserted++; ////////////////////////////////////// // // handle (optional) geometry field pseudo column // if (!(ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbNone || ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbUnknown)) { ESRI.ArcGIS.Geodatabase.IFieldEdit2 geomFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); if (ogr_layer.GetGeometryColumn().Length > 0) { geomFieldEdit.Name_2 = ogr_layer.GetGeometryColumn(); geomFieldEdit.AliasName_2 = ogr_layer.GetGeometryColumn(); } else { geomFieldEdit.Name_2 = "Shape"; geomFieldEdit.AliasName_2 = "Shape"; } geomFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry; // add geometry def ESRI.ArcGIS.Geometry.esriGeometryType gdbType; bool hasZ; ogr_geo_type_to_esri_geo_type(ogr_layer.GetGeomType(), out gdbType, out hasZ); ESRI.ArcGIS.Geodatabase.IGeometryDefEdit geomDef = new ESRI.ArcGIS.Geodatabase.GeometryDefClass(); geomDef.GeometryType_2 = gdbType; geomDef.HasM_2 = false; //no M support on OGR geomDef.HasZ_2 = hasZ; geomDef.SpatialReference_2 = outSpatialReference = ogr_utils.get_spatialReference(ogr_layer.GetSpatialRef()); geomFieldEdit.GeometryDef_2 = geomDef; fieldArray.Add(geomFieldEdit); outDatasetType = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass; // upgrade to featureclass outGeometryType = gdbType; outShapeIndex = fieldsInserted; fieldsInserted++; } int fieldCount = featDef.GetFieldCount(); for (int i = 0; i < fieldCount; i++) { // map OGR field to ArcObjects OSGeo.OGR.FieldDefn fieldDef = featDef.GetFieldDefn(i); ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); fieldEdit.Name_2 = fieldDef.GetName(); fieldEdit.AliasName_2 = fieldDef.GetName(); // map type OSGeo.OGR.FieldType ogrFieldType = fieldDef.GetFieldType(); ESRI.ArcGIS.Geodatabase.esriFieldType mappedType; switch (ogrFieldType) { case OSGeo.OGR.FieldType.OFTInteger: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger; break; case OSGeo.OGR.FieldType.OFTReal: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDouble; break; case OSGeo.OGR.FieldType.OFTString: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString; break; case OSGeo.OGR.FieldType.OFTBinary: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob; break; case OSGeo.OGR.FieldType.OFTDateTime: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDate; break; default: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString; break; } fieldEdit.Type_2 = mappedType; outFieldMap.Add(fieldsInserted, i); fieldArray.Add(fieldEdit); fieldsInserted++; } // Add all the fields from the array to an ESRI fields class object. The reason that we do that // here is that we need to know the count in advance ESRI.ArcGIS.Geodatabase.IFieldsEdit fields = new ESRI.ArcGIS.Geodatabase.FieldsClass(); fields.FieldCount_2 = fieldArray.Count; for (int i = 0; i < fieldArray.Count; i++) { fields.set_Field(i, fieldArray[i] as ESRI.ArcGIS.Geodatabase.IField); } outFields = fields; }
public FeatureClass(Dataset dataset, OSGeo.OGR.Layer layer) { _dataset = dataset; _ogrLayer = layer; OSGeo.OGR.FeatureDefn defn = layer.GetLayerDefn(); _name = defn.GetName(); if (dataset.ConnectionString.ToLower().EndsWith(".dxf")) { try { System.IO.FileInfo fi = new System.IO.FileInfo(dataset.ConnectionString); _name = fi.Name; } catch { } } _fields = new Fields(); for (int i = 0; i < defn.GetFieldCount(); i++) { OSGeo.OGR.FieldDefn fdefn = defn.GetFieldDefn(i); Field field = new Field(fdefn.GetName()); switch (fdefn.GetFieldTypeName(fdefn.GetFieldType()).ToLower()) { case "integer": if (_idFieldName == String.Empty) { _idFieldName = field.name; } field.type = FieldType.integer; break; case "real": field.type = FieldType.Double; break; case "string": field.type = FieldType.String; field.size = fdefn.GetWidth(); break; } _fields.Add(field); } _countFeatures = layer.GetFeatureCount(1); OSGeo.OGR.Envelope env = new OSGeo.OGR.Envelope(); layer.GetExtent(env, 1); _envelope = new Envelope(env.MinX, env.MinY, env.MaxX, env.MaxY); switch (defn.GetGeomType()) { case OSGeo.OGR.wkbGeometryType.wkbPoint: _geomType = geometryType.Point; break; case OSGeo.OGR.wkbGeometryType.wkbLineString: case OSGeo.OGR.wkbGeometryType.wkbMultiLineString: _geomType = geometryType.Polyline; break; case OSGeo.OGR.wkbGeometryType.wkbPolygon: case OSGeo.OGR.wkbGeometryType.wkbMultiPolygon: _geomType = geometryType.Polygon; break; } }