/// <summary> /// Reads the field types from the OgrFeatureDefinition -> OgrFieldDefinition /// </summary> /// <param name="fdt">FeatureDatatTable</param> /// <param name="oLayer">OgrLayer</param> private void ReadColumnDefinition(FeatureDataTable fdt, OSGeo.OGR.Layer oLayer) { using (OSGeo.OGR.FeatureDefn _OgrFeatureDefn = oLayer.GetLayerDefn()) { int iField; for (iField = 0; iField < _OgrFeatureDefn.GetFieldCount(); iField++) { using (OSGeo.OGR.FieldDefn _OgrFldDef = _OgrFeatureDefn.GetFieldDefn(iField)) { OSGeo.OGR.FieldType type; switch ((type = _OgrFldDef.GetFieldType())) { case OSGeo.OGR.FieldType.OFTInteger: fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.Int32")); break; case OSGeo.OGR.FieldType.OFTReal: fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.Double")); break; case OSGeo.OGR.FieldType.OFTString: fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.String")); break; case OSGeo.OGR.FieldType.OFTWideString: fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.String")); break; default: { //fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.String")); System.Diagnostics.Debug.WriteLine("Not supported type: " + type + " [" + _OgrFldDef.GetName() + "]"); break; } } } } } }
//从文件中读取数据 private static FeatureSource GetFeaturesource(SHPGeoDataBase thisdb, String path) { OSGeo.OGR.DataSource ds = GetOGRDataSource(thisdb, path); //init schema OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); OSGeo.OGR.FeatureDefn fd = layer.GetLayerDefn(); Int32 sIndex = path.LastIndexOf("\\"); string sname = path.Substring(sIndex + 1, path.Length - sIndex - 1 - 4); int fieldcount = fd.GetFieldCount(); Dictionary <String, FieldDefn> tmpField = new Dictionary <string, FieldDefn>(); for (int i = 0; i < fieldcount; ++i) { OSGeo.OGR.FieldDefn field = fd.GetFieldDefn(i); tmpField.Add(field.GetName(), field); } GisSmartTools.RS.ReferenceSystem rfs; OSGeo.OSR.SpatialReference osrrf = layer.GetSpatialRef(); if (osrrf.IsProjected() != 0) { rfs = new GisSmartTools.RS.SRS(osrrf); } else { rfs = new GisSmartTools.RS.GRS(osrrf); } Schema rs = new Schema(sname, layer.GetGeomType(), rfs, tmpField); //get featurecollection FeatureCollection fc = GetFeatureCollection(layer, rs); //close file //ds.Dispose(); return(new FeatureSource(rs, fc)); }
//read data really private static FeatureCollection GetFeatureCollection(OSGeo.OGR.Layer layer, GisSmartTools.Data.Schema schema) { FeatureCollection fc = new FeatureCollection(); OSGeo.OGR.Feature feature; OSGeo.OGR.FeatureDefn posDefn = layer.GetLayerDefn(); int fieldCount = posDefn.GetFieldCount(); int iField = 0; //read all the features in layer while ((feature = layer.GetNextFeature()) != null) { OSGeo.OGR.Geometry geometry = feature.GetGeometryRef(); GisSmartTools.Data.Feature feat = new Feature(feature.GetFID(), schema, null); //get and save feature's attributes for (iField = 0; iField < fieldCount; iField++) { OSGeo.OGR.FieldDefn oField = posDefn.GetFieldDefn(iField); Object objAttr; FieldType ft = oField.GetFieldType(); switch (ft) { case FieldType.OFTString: objAttr = feature.GetFieldAsString(iField); break; case FieldType.OFTInteger: objAttr = feature.GetFieldAsInteger(iField); break; case FieldType.OFTReal: objAttr = feature.GetFieldAsDouble(iField); break; case FieldType.OFTWideString: objAttr = feature.GetFieldAsString(iField); break; case FieldType.OFTStringList: objAttr = feature.GetFieldAsStringList(iField); break; case FieldType.OFTIntegerList: int outCount; objAttr = feature.GetFieldAsIntegerList(iField, out outCount); break; case FieldType.OFTRealList: int outCount2; objAttr = feature.GetFieldAsDoubleList(iField, out outCount2); break; case FieldType.OFTWideStringList: objAttr = feature.GetFieldAsStringList(iField); break; default: objAttr = feature.GetFieldAsString(iField); break; } feat.AddAttribute(oField.GetName(), objAttr); } //get geometry if (geometry != null) { OSGeo.OGR.wkbGeometryType goetype = geometry.GetGeometryType(); switch (goetype) //according to the type, we operate differently { case wkbGeometryType.wkbPoint: if (geometry != null && goetype == wkbGeometryType.wkbPoint) { feat.geometry = new PointD(geometry.GetX(0), geometry.GetY(0)); } //add feature to featureCollection fc.InsertFeature(feat); break; case wkbGeometryType.wkbLineString: List <PointD> pointlist = new List <PointD>(); int pointcount = geometry.GetPointCount(); for (int k = 0; k < pointcount; k++) { pointlist.Add(new PointD(geometry.GetX(k), geometry.GetY(k))); } feat.geometry = new SimplePolyline(pointlist); //add feature to featureCollection fc.InsertFeature(feat); break; case wkbGeometryType.wkbMultiLineString: feat.geometry = GetPolyline(geometry); fc.InsertFeature(feat); break; case wkbGeometryType.wkbPolygon: feat.geometry = GetSimplePolygon(geometry); //add feature to featureCollection fc.InsertFeature(feat); break; case wkbGeometryType.wkbMultiPolygon: feat.geometry = GetPolygon(geometry); fc.InsertFeature(feat); break; default: //we don't support another geometry types, //so we don't load those records to avoid errors break; } } else { Console.WriteLine("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"); } } return(fc); }