public static GeoBitmap LoadImageInfo(string file) { using (var ds = OSGeo.GDAL.Gdal.Open(file, OSGeo.GDAL.Access.GA_ReadOnly)) { log.InfoFormat("Raster dataset parameters:"); log.InfoFormat(" Projection: " + ds.GetProjectionRef()); log.InfoFormat(" RasterCount: " + ds.RasterCount); log.InfoFormat(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")"); OSGeo.GDAL.Driver drv = ds.GetDriver(); log.InfoFormat("Using driver " + drv.LongName); string[] metadata = ds.GetMetadata(""); if (metadata.Length > 0) { log.InfoFormat(" Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { // log.InfoFormat(" " + iMeta + ": " + metadata[iMeta]); } log.InfoFormat(""); } metadata = ds.GetMetadata("IMAGE_STRUCTURE"); if (metadata.Length > 0) { log.InfoFormat(" Image Structure Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { log.InfoFormat(" " + iMeta + ": " + metadata[iMeta]); } log.InfoFormat(""); } metadata = ds.GetMetadata("SUBDATASETS"); if (metadata.Length > 0) { log.InfoFormat(" Subdatasets:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { log.InfoFormat(" " + iMeta + ": " + metadata[iMeta]); } log.InfoFormat(""); } metadata = ds.GetMetadata("GEOLOCATION"); if (metadata.Length > 0) { log.InfoFormat(" Geolocation:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { log.InfoFormat(" " + iMeta + ": " + metadata[iMeta]); } log.InfoFormat(""); } log.InfoFormat("Corner Coordinates:"); log.InfoFormat(" Upper Left (" + GDALInfoGetPosition(ds, 0.0, 0.0) + ")"); log.InfoFormat(" Lower Left (" + GDALInfoGetPosition(ds, 0.0, ds.RasterYSize) + ")"); log.InfoFormat(" Upper Right (" + GDALInfoGetPosition(ds, ds.RasterXSize, 0.0) + ")"); log.InfoFormat(" Lower Right (" + GDALInfoGetPosition(ds, ds.RasterXSize, ds.RasterYSize) + ")"); log.InfoFormat(" Center (" + GDALInfoGetPosition(ds, ds.RasterXSize / 2, ds.RasterYSize / 2) + ")"); log.InfoFormat(""); 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); log.InfoFormat("Coordinate System is:"); log.InfoFormat(wkt); } else { log.InfoFormat("Coordinate System is:"); log.InfoFormat(projection); } } if (ds.GetGCPCount() > 0) { log.InfoFormat("GCP Projection: ", ds.GetGCPProjection()); GCP[] GCPs = ds.GetGCPs(); for (int i = 0; i < ds.GetGCPCount(); i++) { log.InfoFormat("GCP[" + i + "]: Id=" + GCPs[i].Id + ", Info=" + GCPs[i].Info); log.InfoFormat(" (" + GCPs[i].GCPPixel + "," + GCPs[i].GCPLine + ") -> (" + GCPs[i].GCPX + "," + GCPs[i].GCPY + "," + GCPs[i].GCPZ + ")"); log.InfoFormat(""); } log.InfoFormat(""); double[] transform = new double[6]; Gdal.GCPsToGeoTransform(GCPs, transform, 0); log.InfoFormat("GCP Equivalent geotransformation parameters: ", ds.GetGCPProjection()); for (int i = 0; i < 6; i++) { log.InfoFormat("t[" + i + "] = " + transform[i].ToString()); } log.InfoFormat(""); } var TL = GDALInfoGetPositionDouble(ds, 0.0, 0.0); var BR = GDALInfoGetPositionDouble(ds, ds.RasterXSize, ds.RasterYSize); var resolution = Math.Abs(BR[0] - TL[0]) / ds.RasterXSize; if (resolution == 1) { throw new Exception("Invalid coords"); } return(new GeoBitmap(file, resolution, ds.RasterXSize, ds.RasterYSize, TL[0], TL[1], BR[0], BR[1])); } }
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]); 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."); 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()); } } Console.ReadLine(); } catch (Exception e) { Console.WriteLine("Application error: " + e.Message); } }
private void getGdalinfo() { //================================================================ // Initialize //================================================================ reset(); //================================================================ // Get gdal info //================================================================ try { //------------------------------------------------------------ // Open dataset //------------------------------------------------------------ m_tGdalDs = Gdal.Open(m_strGeoTiffPath, Access.GA_ReadOnly); if (m_tGdalDs == null) { m_ctrlLogger.FatalFormat("Can't open {0}", m_strGeoTiffPath); throw new ArgumentException("Parameter cannot be null", "original"); } m_strGdalInfoTxt.AppendLine("GeoTIFF file path:"); m_strGdalInfoTxt.AppendFormat(" {0}", m_strGeoTiffPath).AppendLine(); m_strGdalInfoTxt.AppendLine("Raster dataset parameters:"); m_strGdalInfoTxt.AppendFormat(" Projection: {0}", m_tGdalDs.GetProjectionRef()).AppendLine(); m_strGdalInfoTxt.AppendFormat(" RasterCount: {0}", m_tGdalDs.RasterCount).AppendLine(); m_strGdalInfoTxt.AppendFormat(" RasterSize ({0},{1})", m_tGdalDs.RasterXSize, m_tGdalDs.RasterYSize).AppendLine(); //------------------------------------------------------------ // Get driver //------------------------------------------------------------ m_tGdalDriver = m_tGdalDs.GetDriver(); if (m_tGdalDriver == null) { m_ctrlLogger.Fatal("Can't get driver."); throw new ArgumentException("Parameter cannot be null", "original"); } m_strGdalInfoTxt.AppendFormat("Using driver {0}", m_tGdalDriver.LongName).AppendLine(); //------------------------------------------------------------ // Get meta data //------------------------------------------------------------ string[] metadata = m_tGdalDs.GetMetadata(string.Empty); if (metadata.Length > 0) { m_strGdalInfoTxt.AppendLine(" Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { m_strGdalInfoTxt.AppendFormat(" {0}: {1}", iMeta, metadata[iMeta]).AppendLine(); } m_strGdalInfoTxt.AppendLine(); } //------------------------------------------------------------ // Report "IMAGE_STRUCTURE" metadata //------------------------------------------------------------ metadata = m_tGdalDs.GetMetadata("IMAGE_STRUCTURE"); if (metadata.Length > 0) { m_strGdalInfoTxt.AppendLine(" Image Structure Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { m_strGdalInfoTxt.AppendFormat(" {0}: {1}", iMeta, metadata[iMeta]).AppendLine(); } m_strGdalInfoTxt.AppendLine(); } //------------------------------------------------------------ // Report subdatasets //------------------------------------------------------------ metadata = m_tGdalDs.GetMetadata("SUBDATASETS"); if (metadata.Length > 0) { m_strGdalInfoTxt.AppendLine(" Subdatasets:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { m_strGdalInfoTxt.AppendFormat(" {0}: {1}", iMeta, metadata[iMeta]).AppendLine(); } m_strGdalInfoTxt.AppendLine(); } //------------------------------------------------------------ // Report geolocation //------------------------------------------------------------ metadata = m_tGdalDs.GetMetadata("GEOLOCATION"); if (metadata.Length > 0) { m_strGdalInfoTxt.AppendLine(" Geolocation:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { m_strGdalInfoTxt.AppendFormat(" {0}: {1}", iMeta, metadata[iMeta]).AppendLine(); } m_strGdalInfoTxt.AppendLine(); } //------------------------------------------------------------ // Report corners //------------------------------------------------------------ m_strGdalInfoTxt.AppendLine("Corner Coordinates:"); m_strGdalInfoTxt.AppendFormat(" Upper Left ({0})", GDALInfoGetPosition(0.0, 0.0)).AppendLine(); m_strGdalInfoTxt.AppendFormat(" Lower Left ({0})", GDALInfoGetPosition(0.0, m_tGdalDs.RasterYSize)).AppendLine(); m_strGdalInfoTxt.AppendFormat(" Upper Right ({0})", GDALInfoGetPosition(m_tGdalDs.RasterXSize, 0.0)).AppendLine(); m_strGdalInfoTxt.AppendFormat(" Lower Right ({0})", GDALInfoGetPosition(m_tGdalDs.RasterXSize, m_tGdalDs.RasterYSize)).AppendLine(); m_strGdalInfoTxt.AppendFormat(" Center ({0})", GDALInfoGetPosition(m_tGdalDs.RasterXSize / 2, m_tGdalDs.RasterYSize / 2)).AppendLine(); m_strGdalInfoTxt.AppendLine(); //------------------------------------------------------------ // Report projection //------------------------------------------------------------ string projection = m_tGdalDs.GetProjectionRef(); if (projection != null) { SpatialReference srs = new SpatialReference(null); if (srs.ImportFromWkt(ref projection) == 0) { string wkt; srs.ExportToPrettyWkt(out wkt, 0); m_strGdalInfoTxt.AppendLine("Coordinate System is:"); m_strGdalInfoTxt.AppendLine(wkt); } else { m_strGdalInfoTxt.AppendLine("Coordinate System is:"); m_strGdalInfoTxt.AppendLine(projection); } } //------------------------------------------------------------ // Report GCPs //------------------------------------------------------------ if (m_tGdalDs.GetGCPCount() > 0) { m_strGdalInfoTxt.AppendFormat("GCP Projection: {0}", m_tGdalDs.GetGCPProjection()).AppendLine(); GCP[] GCPs = m_tGdalDs.GetGCPs(); for (int i = 0; i < m_tGdalDs.GetGCPCount(); i++) { m_strGdalInfoTxt.AppendFormat("GCP[{0}]: Id={1}, Info={2}", i, GCPs[i].Id, GCPs[i].Info).AppendLine(); m_strGdalInfoTxt.AppendFormat(" ({0},{1})->({2},{3},{4})", GCPs[i].GCPPixel, GCPs[i].GCPLine, GCPs[i].GCPX, GCPs[i].GCPY, GCPs[i].GCPZ); m_strGdalInfoTxt.AppendLine(); } m_strGdalInfoTxt.AppendLine(); double[] transform = new double[6]; Gdal.GCPsToGeoTransform(GCPs, transform, 0); m_strGdalInfoTxt.AppendFormat("GCP Equivalent geotransformation parameters: {0}", m_tGdalDs.GetGCPProjection()); for (int i = 0; i < 6; i++) { m_strGdalInfoTxt.AppendFormat("[{0}]= {1}", i, transform[i].ToString()); } m_strGdalInfoTxt.AppendLine(); } //------------------------------------------------------------ // Get raster band //------------------------------------------------------------ for (int iBand = 1; iBand <= m_tGdalDs.RasterCount; iBand++) { Band band = m_tGdalDs.GetRasterBand(iBand); m_strGdalInfoTxt.AppendFormat("Band {0} :", iBand).AppendLine(); m_strGdalInfoTxt.AppendFormat(" DataType: {0}", Gdal.GetDataTypeName(band.DataType)).AppendLine(); m_strGdalInfoTxt.AppendFormat(" ColorInterpretation: {0}", Gdal.GetColorInterpretationName(band.GetRasterColorInterpretation())).AppendLine(); ColorTable ct = band.GetRasterColorTable(); if (ct != null) { m_strGdalInfoTxt.AppendFormat(" Band has a color table with {0} entries.", ct.GetCount()).AppendLine(); } m_strGdalInfoTxt.AppendFormat(" Description: {0}", band.GetDescription()).AppendLine(); m_strGdalInfoTxt.AppendFormat(" Size ({0},{1})", band.XSize, band.YSize).AppendLine(); int BlockXSize, BlockYSize; band.GetBlockSize(out BlockXSize, out BlockYSize); m_strGdalInfoTxt.AppendFormat(" BlockSize ({0},{1})", BlockXSize, BlockYSize).AppendLine(); double val; int hasval; band.GetMinimum(out val, out hasval); if (hasval != 0) { m_strGdalInfoTxt.AppendFormat(" Minimum: {0}", val.ToString()).AppendLine(); } band.GetMaximum(out val, out hasval); if (hasval != 0) { m_strGdalInfoTxt.AppendFormat(" Maximum: {0}", val.ToString()).AppendLine(); } band.GetNoDataValue(out val, out hasval); if (hasval != 0) { m_strGdalInfoTxt.AppendFormat(" NoDataValue: {0}", val.ToString()).AppendLine(); } band.GetOffset(out val, out hasval); if (hasval != 0) { m_strGdalInfoTxt.AppendFormat(" Offset: {0}", val.ToString()).AppendLine(); } band.GetScale(out val, out hasval); if (hasval != 0) { m_strGdalInfoTxt.AppendFormat(" Scale: {0}", val.ToString()).AppendLine(); } for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++) { Band over = band.GetOverview(iOver); m_strGdalInfoTxt.AppendFormat(" OverView {0}:", iOver).AppendLine(); m_strGdalInfoTxt.AppendFormat(" DataType: {0}", over.DataType).AppendLine(); m_strGdalInfoTxt.AppendFormat(" Size ({0},{1})", over.XSize, over.YSize).AppendLine(); m_strGdalInfoTxt.AppendFormat(" PaletteInterp: {0}", over.GetRasterColorInterpretation().ToString()).AppendLine(); } } } catch (Exception ex) { m_ctrlLogger.FatalFormat("Application error: {0}", ex.Message); } }
public static String GetInfo(Dataset ds) { StringBuilder builder = new StringBuilder(); try { if (ds == null) { return(""); } //builder.AppendLine(" Projection: " + ds.GetProjectionRef()); /* -------------------------------------------------------------------- */ /* Get driver */ /* -------------------------------------------------------------------- */ Driver drv = ds.GetDriver(); if (drv == null) { return("Can't get driver."); } builder.AppendLine("Driver " + drv.ShortName + "/" + drv.LongName); string[] fileList = ds.GetFileList(); if (fileList.Length > 0) { builder.AppendLine("Files:"); for (int iMeta = 0; iMeta < fileList.Length; iMeta++) { byte[] bytes = Encoding.Default.GetBytes(fileList[iMeta]); String fileName = Encoding.UTF8.GetString(bytes); builder.AppendLine(" " + fileName); } } builder.AppendLine("BandCount: " + ds.RasterCount); builder.AppendLine("Size is " + ds.RasterXSize + ", " + ds.RasterYSize); /* -------------------------------------------------------------------- */ /* 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); builder.AppendLine("Coordinate System is:"); builder.AppendLine(wkt); } else { builder.AppendLine("Coordinate System is:"); builder.AppendLine(projection); } } /* -------------------------------------------------------------------- */ /* Report GCPs. */ /* -------------------------------------------------------------------- */ if (ds.GetGCPCount() > 0) { builder.AppendLine("GCP Projection: " + ds.GetGCPProjection()); GCP[] GCPs = ds.GetGCPs(); for (int i = 0; i < ds.GetGCPCount(); i++) { builder.AppendLine("GCP[" + i + "]: Id=" + GCPs[i].Id + ", Info=" + GCPs[i].Info); builder.AppendLine(" (" + GCPs[i].GCPPixel + "," + GCPs[i].GCPLine + ") -> (" + GCPs[i].GCPX + "," + GCPs[i].GCPY + "," + GCPs[i].GCPZ + ")"); builder.AppendLine(""); } builder.AppendLine(""); double[] geotransform = new double[6]; Gdal.GCPsToGeoTransform(GCPs, geotransform, 0); builder.AppendLine("GCP Equivalent geotransformation parameters: " + ds.GetGCPProjection()); for (int i = 0; i < 6; i++) { builder.AppendLine("t[" + i + "] = " + geotransform[i].ToString()); } builder.AppendLine(""); } double[] transform = GetGeoTransform(ds); builder.AppendLine("Origin = (" + GDALInfoGetPosition(transform, 0.0, 0.0) + ")"); builder.AppendLine("Pixel Size = (" + transform[1].ToString("f16") + "," + transform[5].ToString("f16") + ")"); /* -------------------------------------------------------------------- */ /* Get metadata */ /* -------------------------------------------------------------------- */ string[] metadata = ds.GetMetadata(""); if (metadata.Length > 0) { builder.AppendLine("Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { builder.AppendLine(" " + metadata[iMeta]); } } /* -------------------------------------------------------------------- */ /* Report "IMAGE_STRUCTURE" metadata. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("IMAGE_STRUCTURE"); if (metadata.Length > 0) { builder.AppendLine("Image Structure Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { builder.AppendLine(" " + metadata[iMeta]); } } /* -------------------------------------------------------------------- */ /* Report subdatasets. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("SUBDATASETS"); if (metadata.Length > 0) { builder.AppendLine(" Subdatasets:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { builder.AppendLine(" " + metadata[iMeta]); } builder.AppendLine(""); } /* -------------------------------------------------------------------- */ /* Report geolocation. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("GEOLOCATION"); if (metadata.Length > 0) { builder.AppendLine(" Geolocation:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { builder.AppendLine(" " + metadata[iMeta]); } builder.AppendLine(""); } /* -------------------------------------------------------------------- */ /* Report corners. */ /* -------------------------------------------------------------------- */ builder.AppendLine("Corner Coordinates:"); builder.AppendLine(" Upper Left (" + GDALInfoGetPosition(transform, 0.0, 0.0) + ")"); builder.AppendLine(" Lower Left (" + GDALInfoGetPosition(transform, 0.0, ds.RasterYSize) + ")"); builder.AppendLine(" Upper Right (" + GDALInfoGetPosition(transform, ds.RasterXSize, 0.0) + ")"); builder.AppendLine(" Lower Right (" + GDALInfoGetPosition(transform, ds.RasterXSize, ds.RasterYSize) + ")"); builder.AppendLine(" Center (" + GDALInfoGetPosition(transform, ds.RasterXSize / 2, ds.RasterYSize / 2) + ")"); builder.AppendLine(""); /* -------------------------------------------------------------------- */ /* Get raster band */ /* -------------------------------------------------------------------- */ for (int iBand = 1; iBand <= ds.RasterCount; iBand++) { Band band = ds.GetRasterBand(iBand); int blockXSize, blockYSize; band.GetBlockSize(out blockXSize, out blockYSize); builder.Append("Band "); builder.Append(iBand); builder.Append(" Block="); builder.Append(blockXSize); builder.Append("x"); builder.Append(blockYSize); builder.Append(", Type="); builder.Append(Gdal.GetDataTypeName(band.DataType)); builder.Append(", ColorInterp="); builder.Append(Gdal.GetColorInterpretationName(band.GetRasterColorInterpretation())); double val; int hasval; band.GetMinimum(out val, out hasval); if (hasval != 0) { builder.Append(", Minimum=" + val.ToString()); } band.GetMaximum(out val, out hasval); if (hasval != 0) { builder.Append(", Maximum=" + val.ToString()); } band.GetNoDataValue(out val, out hasval); if (hasval != 0) { builder.Append(", NoDataValue=" + val.ToString()); } band.GetOffset(out val, out hasval); if (hasval != 0) { builder.Append(", Offset=" + val.ToString()); } band.GetScale(out val, out hasval); if (hasval != 0) { builder.Append(", Scale=" + val.ToString()); } builder.AppendLine(""); if (band.GetOverviewCount() > 0) { builder.Append(" OverViews: "); for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++) { Band over = band.GetOverview(iOver); builder.Append(over.XSize + "x" + over.YSize + ", "); // builder.AppendLine(" DataType: " + over.DataType); // builder.AppendLine(" Size (" + over.XSize + "," + over.YSize + ")"); //builder.AppendLine(" PaletteInterp: " + over.GetRasterColorInterpretation().ToString()); } builder.AppendLine(""); } ColorTable ct = band.GetRasterColorTable(); if (ct != null) { builder.AppendLine(" Band has a color table with " + ct.GetCount() + " entries."); } String des = band.GetDescription(); if (des != null && !String.IsNullOrWhiteSpace(des)) { builder.AppendLine(" Description: " + band.GetDescription()); } } } catch (Exception e) { return("Application error: " + e.Message); } return(builder.ToString()); }
public static String DumpDatasetInfo(Dataset ds) { var sb = new StringBuilder(); sb.AppendLine("Raster dataset parameters:"); sb.AppendLine(" Projection: " + ds.GetProjectionRef()); sb.AppendLine(" RasterCount: " + ds.RasterCount); sb.AppendLine(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")"); /* -------------------------------------------------------------------- */ /* Get driver */ /* -------------------------------------------------------------------- */ var drv = ds.GetDriver(); if (drv == null) { sb.AppendLine("Can't get driver."); return(sb.ToString()); } sb.AppendLine("Using driver " + drv.LongName); /* -------------------------------------------------------------------- */ /* Get metadata */ /* -------------------------------------------------------------------- */ string[] metadata = ds.GetMetadata(""); if (metadata.Length > 0) { sb.AppendLine(" Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { sb.AppendLine(" " + iMeta + ": " + metadata[iMeta]); } sb.AppendLine(""); } /* -------------------------------------------------------------------- */ /* Report "IMAGE_STRUCTURE" metadata. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("IMAGE_STRUCTURE"); if (metadata.Length > 0) { sb.AppendLine(" Image Structure Metadata:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { sb.AppendLine(" " + iMeta + ": " + metadata[iMeta]); } sb.AppendLine(""); } /* -------------------------------------------------------------------- */ /* Report subdatasets. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("SUBDATASETS"); if (metadata.Length > 0) { sb.AppendLine(" Subdatasets:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { sb.AppendLine(" " + iMeta + ": " + metadata[iMeta]); } sb.AppendLine(""); } /* -------------------------------------------------------------------- */ /* Report geolocation. */ /* -------------------------------------------------------------------- */ metadata = ds.GetMetadata("GEOLOCATION"); if (metadata.Length > 0) { sb.AppendLine(" Geolocation:"); for (int iMeta = 0; iMeta < metadata.Length; iMeta++) { sb.AppendLine(" " + iMeta + ": " + metadata[iMeta]); } sb.AppendLine(""); } /* -------------------------------------------------------------------- */ /* Report corners. */ /* -------------------------------------------------------------------- */ sb.AppendLine("Corner Coordinates:"); sb.AppendLine(" Upper Left (" + GdalInfoGetPosition(ds, 0.0, 0.0) + ")"); sb.AppendLine(" Lower Left (" + GdalInfoGetPosition(ds, 0.0, ds.RasterYSize) + ")"); sb.AppendLine(" Upper Right (" + GdalInfoGetPosition(ds, ds.RasterXSize, 0.0) + ")"); sb.AppendLine(" Lower Right (" + GdalInfoGetPosition(ds, ds.RasterXSize, ds.RasterYSize) + ")"); sb.AppendLine(" Center (" + GdalInfoGetPosition(ds, ds.RasterXSize / 2.0, ds.RasterYSize / 2.0) + ")"); sb.AppendLine(""); /* -------------------------------------------------------------------- */ /* Report projection. */ /* -------------------------------------------------------------------- */ string projection = ds.GetProjectionRef(); if (projection != null) { SpatialReference srs = new SpatialReference(null); if (srs.ImportFromWkt(ref projection) == 0) { srs.ExportToPrettyWkt(out var wkt, 0); sb.AppendLine("Coordinate System is:"); sb.AppendLine(wkt); } else { sb.AppendLine("Coordinate System is:"); sb.AppendLine(projection); } } /* -------------------------------------------------------------------- */ /* Report GCPs. */ /* -------------------------------------------------------------------- */ if (ds.GetGCPCount() > 0) { sb.AppendLine("GCP Projection: " + ds.GetGCPProjection()); GCP[] gcps = ds.GetGCPs(); for (int i = 0; i < ds.GetGCPCount(); i++) { sb.AppendLine("GCP[" + i + "]: Id=" + gcps[i].Id + ", Info=" + gcps[i].Info); sb.AppendLine(" (" + gcps[i].GCPPixel + "," + gcps[i].GCPLine + ") -> (" + gcps[i].GCPX + "," + gcps[i].GCPY + "," + gcps[i].GCPZ + ")"); sb.AppendLine(""); } sb.AppendLine(""); double[] transform = new double[6]; Gdal.GCPsToGeoTransform(gcps, transform, 0); sb.AppendLine("GCP Equivalent geotransformation parameters: " + ds.GetGCPProjection()); for (int i = 0; i < 6; i++) { sb.AppendLine("t[" + i + "] = " + transform[i]); } sb.AppendLine(""); } /* -------------------------------------------------------------------- */ /* Get raster band */ /* -------------------------------------------------------------------- */ for (int iBand = 1; iBand <= ds.RasterCount; iBand++) { Band band = ds.GetRasterBand(iBand); sb.AppendLine("Band " + iBand + " :"); sb.AppendLine(" DataType: " + Gdal.GetDataTypeName(band.DataType)); sb.AppendLine(" ColorInterpretation: " + Gdal.GetColorInterpretationName(band.GetRasterColorInterpretation())); ColorTable ct = band.GetRasterColorTable(); if (ct != null) { sb.AppendLine(" Band has a color table with " + ct.GetCount() + " entries."); } sb.AppendLine(" Description: " + band.GetDescription()); sb.AppendLine(" Size (" + band.XSize + "," + band.YSize + ")"); band.GetBlockSize(out var blockXSize, out var blockYSize); sb.AppendLine(" BlockSize (" + blockXSize + "," + blockYSize + ")"); band.GetMinimum(out var val, out var hasVal); if (hasVal != 0) { sb.AppendLine(" Minimum: " + val); } band.GetMaximum(out val, out hasVal); if (hasVal != 0) { sb.AppendLine(" Maximum: " + val); } band.GetNoDataValue(out val, out hasVal); if (hasVal != 0) { sb.AppendLine(" NoDataValue: " + val); } band.GetOffset(out val, out hasVal); if (hasVal != 0) { sb.AppendLine(" Offset: " + val); } band.GetScale(out val, out hasVal); if (hasVal != 0) { sb.AppendLine(" Scale: " + val); } for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++) { Band over = band.GetOverview(iOver); sb.AppendLine(" OverView " + iOver + " :"); sb.AppendLine(" DataType: " + over.DataType); sb.AppendLine(" Size (" + over.XSize + "," + over.YSize + ")"); sb.AppendLine(" PaletteInterpretation: " + over.GetRasterColorInterpretation()); } } return(sb.ToString()); }