public static void ReportLayer(Layer layer) { FeatureDefn def = layer.GetLayerDefn(); Console.WriteLine("Layer name: " + def.GetName()); Console.WriteLine("Feature Count: " + layer.GetFeatureCount(1)); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); Console.WriteLine("Extent: " + ext.MinX + "," + ext.MaxX + "," + ext.MinY + "," + ext.MaxY); /* -------------------------------------------------------------------- */ /* 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)"; } Console.WriteLine("Layer SRS WKT: " + srs_wkt); /* -------------------------------------------------------------------- */ /* Reading the fields */ /* -------------------------------------------------------------------- */ Console.WriteLine("Field definition:"); 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() + ")"); } /* -------------------------------------------------------------------- */ /* Reading the shapes */ /* -------------------------------------------------------------------- */ Console.WriteLine(""); Feature feat; while ((feat = layer.GetNextFeature()) != null) { ReportFeature(feat, def); feat.Dispose(); } }
/// <summary> /// Iterates through the layers of the map and builds a tree based on the layer information /// </summary> /// <param name="ds"></param> /// <returns>"TreeNode"</returns> /// public static TreeNode CreateTree() { DebugUtil.WriteLine(); DebugUtil.WriteLine("*******************************************************************************************************"); DebugUtil.WriteLine("**** Building Tree ****"); DebugUtil.WriteLine("*******************************************************************************************************"); DebugUtil.WriteLine(); TreeNode topNode = new TreeNode(); TreeNode metaTree = new TreeNode(); Layer layer; string srs_wkt; Envelope thisEnvelope = new Envelope(); string coordinateString; // add the name of the chart as the name of this root topNode.Text = Path.GetFileName(ds.name); topNode.Name = Path.GetFileName(ds.name); // Meta Tree metaTree.Text = "Meta Data"; metaTree.Name = "Meta Data"; // get layer 0 (should be "DSID) for the meta data layer = ds.GetLayerByIndex(0); /* -------------------------------------------------------------------- */ /* Get the spatial reference. It is consistent for the entire map so we*/ /* only need to do it once. */ /* -------------------------------------------------------------------- */ OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); // this should not be null if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); TreeUtil.AddChildNode(metaTree, "Spatial Reference", srs_wkt); DebugUtil.WriteLine("Spatial Reference: " + srs_wkt); } else { TreeUtil.AddChildNode(metaTree, "Spatial Reference", "Spatial Reference Uknown"); srs_wkt = "SRS: Unknown."; } // get Envelope for this layer layer.GetExtent(thisEnvelope, 1); // Get the Extents for the map. Again, they are consistent for the entire map, so we only need to do this once. if (thisEnvelope != null) { coordinateString = String.Format("MaxX = {0}, MaxY = {1}, MinX = {2}, MinY = {3}.", thisEnvelope.MaxX, thisEnvelope.MaxY, +thisEnvelope.MinX, thisEnvelope.MinY); DebugUtil.WriteLine("Extents: " + coordinateString); TreeUtil.AddChildNode(metaTree, "Envelope: ", coordinateString); } // add metaTree to topNode topNode.Nodes.Add(metaTree); // go through all the layers starting at zero for (int i = 0; i < ds.GetLayerCount(); i++) { layer = ds.GetLayerByIndex(i); if (layer != null) { // most of the work is done here CreateTreeLayers(topNode, layer, i); } else { DebugUtil.WriteLine("*********** Critical Error *************"); DebugUtil.WriteLine("Layer: " + i + " is null and does not exist."); MessageBox.Show("Critical Error: " + "Layer: " + i + " is null and does not exist."); } } DebugUtil.WriteLine(" TopNode node count is " + topNode.Nodes.Count + "."); // save the tree to a file. this is controlled by the debug flags. note that the file can get very large TreeUtil.OpenPrintRecursive("TreeNode.txt"); TreeUtil.PrintRecursive(topNode); // close the debug files DebugUtil.Close(); TreeUtil.Close(); return(topNode); }