public void opiszWarstwe() { FeatureDefn def = this.layer.GetLayerDefn(); Console.WriteLine("Nazwa warstwy: " + def.GetName()); Console.WriteLine("Liczba obiektów: " + this.layer.GetFeatureCount(1)); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); Console.WriteLine("Zasiêg: " + ext.MinX + "," + ext.MaxX + "," + ext.MinY + "," + ext.MaxY); SpatialReference sr = this.layer.GetSpatialRef(); string srs_wkt = "(unknown)"; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } Console.WriteLine("Uk³ad przestrzenny warstwy: " + srs_wkt); Console.WriteLine("Definicje atrybutów:"); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { FieldDefn fdef = def.GetFieldDefn(iAttr); Console.WriteLine(fdef.GetNameRef() + ": " + fdef.GetFieldTypeName(fdef.GetFieldType()) + " (" + fdef.GetWidth() + "." + fdef.GetPrecision() + ")"); } }
public static string ReportLayer(Layer layer) { string strInfomation = ""; FeatureDefn def = layer.GetLayerDefn(); strInfomation += ("Layer name: " + def.GetName()); strInfomation += ("Feature Count: " + layer.GetFeatureCount(1).ToString()); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); strInfomation += ("Extent: " + ext.MinX.ToString() + "," + ext.MaxX.ToString() + "," + ext.MinY.ToString() + "," + ext.MaxY.ToString()); /* -------------------------------------------------------------------- */ /* Reading the spatial reference */ /* -------------------------------------------------------------------- */ OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string srs_wkt; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } else { srs_wkt = "(unknown)"; } strInfomation += ("Layer SRS WKT: " + srs_wkt); /* -------------------------------------------------------------------- */ /* Reading the fields */ /* -------------------------------------------------------------------- */ strInfomation += ("Field definition:"); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { FieldDefn fdef = def.GetFieldDefn(iAttr); strInfomation += (fdef.GetNameRef() + ": " + fdef.GetFieldTypeName(fdef.GetFieldType()) + " (" + fdef.GetWidth().ToString() + "." + fdef.GetPrecision().ToString() + ")"); } /* -------------------------------------------------------------------- */ /* Reading the shapes */ /* -------------------------------------------------------------------- */ strInfomation += (""); Feature feat; while ((feat = layer.GetNextFeature()) != null) { strInfomation += ReportFeature(feat, def); feat.Dispose(); } return(strInfomation); }
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 ReportFeature(Feature feat, FeatureDefn def) { Console.WriteLine("Feature(" + def.GetName() + "): " + feat.GetFID()); for (int iField = 0; iField < feat.GetFieldCount(); iField++) { FieldDefn fdef = def.GetFieldDefn(iField); Console.Write(fdef.GetNameRef() + " (" + fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = "); if (feat.IsFieldSet(iField)) { Console.WriteLine(feat.GetFieldAsString(iField)); } else { Console.WriteLine("(null)"); } } if (feat.GetStyleString() != null) { Console.WriteLine(" Style = " + feat.GetStyleString()); } Geometry geom = feat.GetGeometryRef(); if (geom != null) { Console.WriteLine(" " + geom.GetGeometryName() + "(" + geom.GetGeometryType() + ")"); } Envelope env = new Envelope(); geom.GetEnvelope(env); Console.WriteLine(" ENVELOPE: " + env.MinX + "," + env.MaxX + "," + env.MinY + "," + env.MaxY); string geom_wkt; geom.ExportToWkt(out geom_wkt); Console.WriteLine(" " + geom_wkt); Console.WriteLine(""); }
public static void ReportFeature(Feature feat, FeatureDefn def) { Console.WriteLine("Feature(" + def.GetName() + "): " + feat.GetFID()); for (int iField = 0; iField < feat.GetFieldCount(); iField++) { FieldDefn fdef = def.GetFieldDefn(iField); Console.Write(fdef.GetNameRef() + " (" + fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = "); if (feat.IsFieldSet(iField)) { if (fdef.GetFieldType() == FieldType.OFTStringList) { string[] sList = feat.GetFieldAsStringList(iField); foreach (string s in sList) { Console.Write("\"" + s + "\" "); } Console.WriteLine(); } else if (fdef.GetFieldType() == FieldType.OFTIntegerList) { int count; int[] iList = feat.GetFieldAsIntegerList(iField, out count); for (int i = 0; i < count; i++) { Console.Write(iList[i] + " "); } Console.WriteLine(); } else if (fdef.GetFieldType() == FieldType.OFTRealList) { int count; double[] iList = feat.GetFieldAsDoubleList(iField, out count); for (int i = 0; i < count; i++) { Console.Write(iList[i].ToString() + " "); } Console.WriteLine(); } else { Console.WriteLine(feat.GetFieldAsString(iField)); } } else { Console.WriteLine("(null)"); } } if (feat.GetStyleString() != null) { Console.WriteLine(" Style = " + feat.GetStyleString()); } Geometry geom = feat.GetGeometryRef(); if (geom != null) { Console.WriteLine(" " + geom.GetGeometryName() + "(" + geom.GetGeometryType() + ")"); Geometry sub_geom; for (int i = 0; i < geom.GetGeometryCount(); i++) { sub_geom = geom.GetGeometryRef(i); if (sub_geom != null) { Console.WriteLine(" subgeom" + i + ": " + sub_geom.GetGeometryName() + "(" + sub_geom.GetGeometryType() + ")"); } } Envelope env = new Envelope(); geom.GetEnvelope(env); Console.WriteLine(" ENVELOPE: " + env.MinX + "," + env.MaxX + "," + env.MinY + "," + env.MaxY); string geom_wkt; geom.ExportToWkt(out geom_wkt); Console.WriteLine(" " + geom_wkt); } Console.WriteLine(""); }
/* -------------------------------------------------------------------- */ /* Reading the Fields in this Feature */ /* -------------------------------------------------------------------- */ public static void CreateTreeFields(ref TreeNode thisFeatureTree, Feature feature, FeatureDefn featureDefn) { // field definition as we iterate through the fields FieldDefn iFieldDefn; int nameIndex; int objlIndex; string nameString; string objlString; // To get the name and the S-57 Type code (OBJL), we have to look forward into the fields. This piece of code // is mostly here to show how that is done as will need this when rendering these objects. You could easily add // shapes or color, but it is not necessary here. The name and the code is nice for the debug file though so we can // see what each feature is. // OBJL field should always exist - the exception are the meta data fields such as DSID which has no code. objlIndex = featureDefn.GetFieldIndex("OBJL"); if (objlIndex > 0) { objlString = DecodeField(feature, featureDefn.GetFieldDefn(objlIndex), objlIndex); } else { objlString = "No Code"; } // name field may not exist. we could get either the info or text description if they exist nameIndex = featureDefn.GetFieldIndex("OBJNAM"); if (nameIndex > 0) { nameString = DecodeField(feature, featureDefn.GetFieldDefn(nameIndex), nameIndex); } else { nameString = "Not Named"; } DebugUtil.WriteLine(); DebugUtil.WriteLine(string.Format(" *** Feature: {0}, Name: {1}, S-57 Code: {2}", featureDefn.GetName(), nameString, objlString)); DebugUtil.WriteLine(); // get the styles, if any if (feature.GetStyleString() != null) { DebugUtil.WriteLine(" Style = " + feature.GetStyleString()); } // this gets the sub-geometries. not all features have sub-geometries Geometry geom = feature.GetGeometryRef(); if (geom != null) { DebugUtil.WriteLine(" Geometry Name: " + geom.GetGeometryName() + " Sub-geometry Count: " + geom.GetGeometryCount()); Geometry sub_geom; for (int i = 0; i < geom.GetGeometryCount(); i++) { sub_geom = geom.GetGeometryRef(i); if (sub_geom != null) { string subGeoString; DebugUtil.Write(" sub-geometry " + i + ": "); switch (sub_geom.GetGeometryType()) { case wkbGeometryType.wkbLineString: case wkbGeometryType.wkbPoint25D: sub_geom.ExportToWkt(out subGeoString); DebugUtil.WriteLine(subGeoString); TreeUtil.AddChildNode(thisFeatureTree, subGeoString); break; default: DebugUtil.WriteLine("Unhandled subgeometry type: " + sub_geom.GetGeometryType()); TreeUtil.AddChildNode(thisFeatureTree, "Unhandled sub-geomtry type", sub_geom.GetGeometryType().ToString()); break; } } } // a bit confusing, but if it has no sub geometries, use the base geometry. mostly applies to the type POINT if (geom.GetGeometryCount() == 0) { string geom_wkt; geom.ExportToWkt(out geom_wkt); DebugUtil.WriteLine(" geom_wkt: " + geom_wkt); TreeUtil.AddChildNode(thisFeatureTree, geom_wkt); } } // if geo != null for (int iField = 0; iField < feature.GetFieldCount(); iField++) { iFieldDefn = featureDefn.GetFieldDefn(iField); // not all fields have usable information, skip those that don't. if (feature.IsFieldSet(iField)) // IsFieldSet is a handy way to see if the field has any actual information { DebugUtil.Write(" "); DebugUtil.Write("Field Name: " + iFieldDefn.GetName() + ", "); DebugUtil.Write("Type Name: " + iFieldDefn.GetFieldTypeName(iFieldDefn.GetFieldType()) + ", "); DebugUtil.Write("Field Type: " + iFieldDefn.GetFieldType() + ", "); DebugUtil.Write("Value: " + DecodeField(feature, iFieldDefn, iField)); DebugUtil.WriteLine(); } // if isField } }
public static List <string> OGRInfo(string datasourceFileLocation) { List <string> info = new List <string>(); Ogr.RegisterAll(); DataSource ds = Ogr.Open(datasourceFileLocation, 0); if (ds == null) { info.Add("Couldn not open vector data source."); return(info); } OSGeo.OGR.Driver drv = ds.GetDriver(); if (drv == null) { info.Add("Could not find driver to open vector data source."); return(info); } info.Add("Using driver: " + drv.GetName()); ///Iterating through layers for (int iLayer = 0; iLayer < ds.GetLayerCount(); iLayer++) { Layer layer = ds.GetLayerByIndex(iLayer); if (layer == null) { info.Add("Could not find layers in the vector data source."); return(info); } FeatureDefn def = layer.GetLayerDefn(); info.Add("Layer name: " + def.GetName()); info.Add("Feature count: " + layer.GetFeatureCount(1)); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); info.Add("Extent: " + ext.MinX + ", " + ext.MinY + ", " + ext.MaxX + ", " + ext.MaxY); ///Reading the spatial reference OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string srs_wkt = string.Empty; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } else { srs_wkt = "(unknow)"; } info.Add("Layer SRS WKT: " + srs_wkt); ///Reading the fields info.Add("Field Names (type): "); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { FieldDefn fdef = def.GetFieldDefn(iAttr); info.Add(fdef.GetName() + " (" + fdef.GetFieldTypeName(fdef.GetFieldType()) + ")"); } } ds.Dispose(); return(info); }
public static void Test2() { try { Ogr.RegisterAll(); string shapefile = @"C:\data\Links\Links.shp"; DataSource ds = Ogr.Open(shapefile, 0); Driver driver = ds.GetDriver(); int nLayerCount = ds.GetLayerCount();//1 for (int iLayer = 0; iLayer < nLayerCount; iLayer++) { Layer layer = ds.GetLayerByIndex(iLayer); string layerName = layer.GetName(); int fc = layer.GetFeatureCount(1); Envelope env = new Envelope(); layer.GetExtent(env, 1); //MessageBox.Show("test sr"); OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string sr_wkt; sr.ExportToPrettyWkt(out sr_wkt, 1); layer.GetName(); FeatureDefn def = layer.GetLayerDefn(); def.GetName(); for (int iField = 0; iField < def.GetFieldCount(); iField++) { FieldDefn fdef = def.GetFieldDefn(iField); string fieldName = fdef.GetName(); //Id Name URL FieldType fieldType = fdef.GetFieldType(); //OFTInteger OFTString OFTString string fieldTypeName = fdef.GetFieldTypeName(fdef.GetFieldType()); //Integer String String int width = fdef.GetWidth(); //6 50 254 int precision = fdef.GetPrecision(); //0 0 0 } for (int fid = 0; fid < layer.GetFeatureCount(1); fid++) { Feature f = layer.GetFeature(fid); int id = f.GetFID(); int nFiledCount = f.GetFieldCount(); Geometry geom = f.GetGeometryRef(); // retrive geometry data //this.Geometrys.Add(geom); string geomName = geom.GetGeometryName(); //POINT string geomType = geom.GetGeometryType().ToString(); //wkbPoint Envelope geom_env = new Envelope(); geom.GetEnvelope(geom_env); // wkt string geom_wkt; geom.ExportToWkt(out geom_wkt);//"POINT (-63.490966216299803 46.66247022944782)" int wkbSize = geom.WkbSize(); if (wkbSize > 0) { // wkb byte[] geom_wkb = new byte[wkbSize]; geom.ExportToWkb(geom_wkb); string str_wkb = BitConverter.ToString(geom_wkb); // wkb--->wkt Geometry geom2 = Geometry.CreateFromWkb(geom_wkb); string geom2_wkt; geom2.ExportToWkt(out geom2_wkt); } f.Dispose(); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } }