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 void Main(string[] args) { if (args.Length != 1) usage(); Console.WriteLine(""); try { /* -------------------------------------------------------------------- */ /* Register driver(s). */ /* -------------------------------------------------------------------- */ Gdal.AllRegister(); /* -------------------------------------------------------------------- */ /* Open dataset. */ /* -------------------------------------------------------------------- */ Dataset ds = Gdal.Open( args[0], Access.GA_ReadOnly ); if (ds == null) { Console.WriteLine("Can't open " + args[0]); System.Environment.Exit(-1); } Console.WriteLine("Raster dataset parameters:"); Console.WriteLine(" Projection: " + ds.GetProjectionRef()); Console.WriteLine(" RasterCount: " + ds.RasterCount); Console.WriteLine(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")"); /* -------------------------------------------------------------------- */ /* Get driver */ /* -------------------------------------------------------------------- */ Driver drv = ds.GetDriver(); if (drv == null) { Console.WriteLine("Can't get driver."); System.Environment.Exit(-1); } Console.WriteLine("Using driver " + drv.LongName); /* -------------------------------------------------------------------- */ /* Get metadata */ /* -------------------------------------------------------------------- */ string[] metadata = ds.GetMetadata(""); if (metadata.Length > 0) { Console.WriteLine(" Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { Console.WriteLine(" " + iMeta + ": " + metadata[iMeta]); } Console.WriteLine(""); } /* -------------------------------------------------------------------- */ /* Report "IMAGE_STRUCTURE" metadata. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("IMAGE_STRUCTURE"); if (metadata.Length > 0) { Console.WriteLine(" Image Structure Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { Console.WriteLine(" " + iMeta + ": " + metadata[iMeta]); } Console.WriteLine(""); } /* -------------------------------------------------------------------- */ /* Report subdatasets. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("SUBDATASETS"); if (metadata.Length > 0) { Console.WriteLine(" Subdatasets:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { Console.WriteLine(" " + iMeta + ": " + metadata[iMeta]); } Console.WriteLine(""); } /* -------------------------------------------------------------------- */ /* Report geolocation. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("GEOLOCATION"); if (metadata.Length > 0) { Console.WriteLine(" Geolocation:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { Console.WriteLine(" " + iMeta + ": " + metadata[iMeta]); } Console.WriteLine(""); } /* -------------------------------------------------------------------- */ /* Report corners. */ /* -------------------------------------------------------------------- */ Console.WriteLine( "Corner Coordinates:" ); Console.WriteLine(" Upper Left (" + GDALInfoGetPosition( ds, 0.0, 0.0) + ")"); Console.WriteLine(" Lower Left (" + GDALInfoGetPosition( ds, 0.0, ds.RasterYSize) + ")"); Console.WriteLine(" Upper Right (" + GDALInfoGetPosition( ds, ds.RasterXSize, 0.0) + ")"); Console.WriteLine(" Lower Right (" + GDALInfoGetPosition( ds, ds.RasterXSize, ds.RasterYSize) + ")"); Console.WriteLine(" Center (" + GDALInfoGetPosition( ds, ds.RasterXSize / 2, ds.RasterYSize / 2) + ")"); Console.WriteLine(""); /* -------------------------------------------------------------------- */ /* Report projection. */ /* -------------------------------------------------------------------- */ string projection = ds.GetProjectionRef(); if (projection != null) { SpatialReference srs = new SpatialReference(null); if (srs.ImportFromWkt(ref projection) == 0) { string wkt; srs.ExportToPrettyWkt(out wkt, 0); Console.WriteLine("Coordinate System is:"); Console.WriteLine(wkt); } else { Console.WriteLine("Coordinate System is:"); Console.WriteLine(projection); } } /* -------------------------------------------------------------------- */ /* Report GCPs. */ /* -------------------------------------------------------------------- */ if( ds.GetGCPCount( ) > 0 ) { Console.WriteLine( "GCP Projection: ", ds.GetGCPProjection()); GCP[] GCPs = ds.GetGCPs(); for( int i = 0; i < ds.GetGCPCount(); i++ ) { Console.WriteLine("GCP[" + i + "]: Id=" + GCPs[i].Id + ", Info=" + GCPs[i].Info); Console.WriteLine(" (" + GCPs[i].GCPPixel + "," + GCPs[i].GCPLine + ") -> (" + GCPs[i].GCPX + "," + GCPs[i].GCPY + "," + GCPs[i].GCPZ + ")"); Console.WriteLine(""); } Console.WriteLine(""); double[] transform = new double[6]; Gdal.GCPsToGeoTransform(GCPs, transform, 0); Console.WriteLine("GCP Equivalent geotransformation parameters: ", ds.GetGCPProjection()); for (int i = 0; i < 6; i++) Console.WriteLine("t[" + i + "] = " + transform[i].ToString()); Console.WriteLine(""); } /* -------------------------------------------------------------------- */ /* Get raster band */ /* -------------------------------------------------------------------- */ for (int iBand = 1; iBand <= ds.RasterCount; iBand++) { Band band = ds.GetRasterBand(iBand); Console.WriteLine("Band " + iBand + " :"); Console.WriteLine(" DataType: " + Gdal.GetDataTypeName(band.DataType)); Console.WriteLine(" ColorInterpretation: " + Gdal.GetColorInterpretationName(band.GetRasterColorInterpretation())); ColorTable ct = band.GetRasterColorTable(); if (ct != null) Console.WriteLine(" Band has a color table with " + ct.GetCount() + " entries."); Console.WriteLine(" Description: " + band.GetDescription()); Console.WriteLine(" Size (" + band.XSize + "," + band.YSize + ")"); int BlockXSize, BlockYSize; band.GetBlockSize(out BlockXSize, out BlockYSize); Console.WriteLine(" BlockSize (" + BlockXSize + "," + BlockYSize + ")"); double val; int hasval; band.GetMinimum(out val, out hasval); if (hasval != 0) Console.WriteLine(" Minimum: " + val.ToString()); band.GetMaximum(out val, out hasval); if (hasval != 0) Console.WriteLine(" Maximum: " + val.ToString()); band.GetNoDataValue(out val, out hasval); if (hasval != 0) Console.WriteLine(" NoDataValue: " + val.ToString()); band.GetOffset(out val, out hasval); if (hasval != 0) Console.WriteLine(" Offset: " + val.ToString()); band.GetScale(out val, out hasval); if (hasval != 0) Console.WriteLine(" Scale: " + val.ToString()); for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++) { Band over = band.GetOverview(iOver); Console.WriteLine(" OverView " + iOver + " :"); Console.WriteLine(" DataType: " + over.DataType); Console.WriteLine(" Size (" + over.XSize + "," + over.YSize + ")"); Console.WriteLine(" PaletteInterp: " + over.GetRasterColorInterpretation().ToString()); } } } catch (Exception e) { Console.WriteLine("Application error: " + e.Message); } }
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); }
private static void ReportLayer(Layer layer) { //layer info string layerName = layer.GetName(); //Links string layerName2 = layer.GetLayerDefn().GetName(); //Links int fc = layer.GetFeatureCount(1); //16 Envelope ext = new Envelope(); layer.GetExtent(ext, 1); /* -------------------------------------------------------------------- */ /* 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)"; } // feature definition FeatureDefn def = layer.GetLayerDefn(); //string layerName2 = def.GetName();//Links /* -------------------------------------------------------------------- */ /* Reading the fields */ /* -------------------------------------------------------------------- */ int nFieldCount = def.GetFieldCount();//3 for (int iField = 0; iField < nFieldCount; iField++) { // field definition FieldDefn fdef = def.GetFieldDefn(iField); // field info string fieldName = fdef.GetName(); //Id Name URL string fieldNameRef = fdef.GetNameRef(); // 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 } /* -------------------------------------------------------------------- */ /* Reading the shapes */ /* -------------------------------------------------------------------- */ for (int fid = 0; fid < layer.GetFeatureCount(1); fid++) { Feature f = layer.GetFeature(fid); ReportFeature(f, def); f.Dispose(); } //Feature f; //while ((f = layer.GetNextFeature()) != null) //{ // ReportFeature(f, def); // f.Dispose(); //} }
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; } }