public void GetGdalInfoRaster(string file) { using var inputDataset = Gdal.Open(file, Access.GA_ReadOnly); var info = Gdal.GDALInfo(inputDataset, new GDALInfoOptions(null)); Assert.NotNull(info); }
/// <summary> /// Runs GdalInfo with passed parameters /// </summary> /// <param name="inputFilePath">Input GeoTiff's path</param> /// <param name="options">Array of string parameters for GdalInfo /// <remarks><para/><see langword="null"/> by default</remarks></param> /// <returns><see cref="string"/> from GdalInfo if everything OK; /// <para/><see langword="null"/> otherwise</returns> public static Task <string> InfoAsync(string inputFilePath, string[] options = null) { #region Preconditions checks CheckHelper.CheckFile(inputFilePath); #endregion // Initialize Gdal, if needed ConfigureGdal(); return(Task.Run(() => { using Dataset inputDataset = Gdal.Open(inputFilePath, Access.GA_ReadOnly); return Gdal.GDALInfo(inputDataset, new GDALInfoOptions(options)); })); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { string datasourceFileLocation = string.Empty; DA.GetData <string>(0, ref datasourceFileLocation); string dstFileLocation = string.Empty; DA.GetData <string>(1, ref dstFileLocation); string options = string.Empty; DA.GetData <string>(2, ref options); var re = new System.Text.RegularExpressions.Regex("(?<=\")[^\"]*(?=\")|[^\" ]+"); string[] translateOptions = re.Matches(options).Cast <Match>().Select(m => m.Value).ToArray(); string datasourceInfo = string.Empty; string dstInfo = string.Empty; string dstOutput = string.Empty; RESTful.GdalConfiguration.ConfigureGdal(); OSGeo.GDAL.Gdal.AllRegister(); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Look for more information about options at:"); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "https://gdal.org/programs/gdal_polygonize.html"); if (!string.IsNullOrEmpty(datasourceFileLocation)) { using (Dataset datasource = Gdal.Open(datasourceFileLocation, Access.GA_ReadOnly)) { if (datasource == null) { throw new Exception("Can't open GDAL dataset: " + datasourceFileLocation); } SpatialReference sr = new SpatialReference(datasource.GetProjection()); ///Check if SRS needs to be converted from ESRI format to WKT to avoid error: ///"No translation for Lambert_Conformal_Conic to PROJ.4 format is known." ///https://gis.stackexchange.com/questions/128266/qgis-error-6-no-translation-for-lambert-conformal-conic-to-proj-4-format-is-kn SpatialReference srEsri = sr; srEsri.MorphFromESRI(); string projEsri = string.Empty; srEsri.ExportToWkt(out projEsri); ///If no SRS exists, check Ground Control Points SRS SpatialReference srGCP = new SpatialReference(datasource.GetGCPProjection()); string projGCP = string.Empty; srGCP.ExportToWkt(out projGCP); if (!string.IsNullOrEmpty(projEsri)) { datasource.SetProjection(projEsri); sr = srEsri; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) morphed form ESRI format."); } else if (!string.IsNullOrEmpty(projGCP)) { datasource.SetProjection(projGCP); sr = srGCP; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) set from Ground Control Points (GCPs)."); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) is unknown or unsupported. " + "Try setting the SRS with the GdalWarp component using -t_srs EPSG:4326 for the option input."); //sr.SetWellKnownGeogCS("WGS84"); } ///Get info about image List <string> infoOptions = new List <string> { "-stats" }; datasourceInfo = Gdal.GDALInfo(datasource, new GDALInfoOptions(infoOptions.ToArray())); if (!string.IsNullOrEmpty(dstFileLocation)) { if (string.IsNullOrEmpty(options) && File.Exists(dstFileLocation)) { Dataset dst = Gdal.Open(dstFileLocation, Access.GA_ReadOnly); dstInfo = Gdal.GDALInfo(dst, null); dst.Dispose(); dstOutput = dstFileLocation; } else { ///https://github.com/OSGeo/gdal/issues/813 ///https://lists.osgeo.org/pipermail/gdal-dev/2017-February/046046.html ///Odd way to go about setting source dataset in parameters for Warp is a known issue var ptr = new[] { Dataset.getCPtr(datasource).Handle }; var gcHandle = GCHandle.Alloc(ptr, GCHandleType.Pinned); try { var dss = new SWIGTYPE_p_p_GDALDatasetShadow(gcHandle.AddrOfPinnedObject(), false, null); Dataset dst = Gdal.wrapper_GDALWarpDestName(dstFileLocation, 1, dss, new GDALWarpAppOptions(translateOptions), null, null); if (dst == null) { throw new Exception("GdalWarp failed: " + Gdal.GetLastErrorMsg()); } dstInfo = Gdal.GDALInfo(dst, new GDALInfoOptions(infoOptions.ToArray())); dst.Dispose(); dstOutput = dstFileLocation; } finally { if (gcHandle.IsAllocated) { gcHandle.Free(); } } } } datasource.Dispose(); } } DA.SetData(0, datasourceInfo); DA.SetData(1, dstInfo); DA.SetData(2, dstOutput); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { string datasourceFileLocation = string.Empty; DA.GetData <string>(0, ref datasourceFileLocation); string dstFileLocation = string.Empty; DA.GetData <string>(1, ref dstFileLocation); string options = string.Empty; DA.GetData <string>(2, ref options); var re = new System.Text.RegularExpressions.Regex("(?<=\")[^\"]*(?=\")|[^\" ]+"); string[] translateOptions = re.Matches(options).Cast <Match>().Select(m => m.Value).ToArray(); string datasourceInfo = string.Empty; string dstInfo = string.Empty; string dstOutput = string.Empty; RESTful.GdalConfiguration.ConfigureGdal(); OSGeo.GDAL.Gdal.AllRegister(); ///Specific settings for getting WMS images OSGeo.GDAL.Gdal.SetConfigOption("GDAL_HTTP_UNSAFESSL", "YES"); OSGeo.GDAL.Gdal.SetConfigOption("GDAL_SKIP", "WMS"); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Look for more information about options at:"); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "https://gdal.org/programs/gdal_translate.html"); if (!string.IsNullOrEmpty(datasourceFileLocation)) { using (Dataset datasource = Gdal.Open(datasourceFileLocation, Access.GA_ReadOnly)) { if (datasource == null) { throw new Exception("Can't open GDAL dataset: " + datasourceFileLocation); } SpatialReference sr = new SpatialReference(datasource.GetProjection()); ///Check if SRS needs to be converted from ESRI format to WKT to avoid error: ///"No translation for Lambert_Conformal_Conic to PROJ.4 format is known." ///https://gis.stackexchange.com/questions/128266/qgis-error-6-no-translation-for-lambert-conformal-conic-to-proj-4-format-is-kn SpatialReference srEsri = sr; srEsri.MorphFromESRI(); string projEsri = string.Empty; srEsri.ExportToWkt(out projEsri); ///If no SRS exists, check Ground Control Points SRS SpatialReference srGCP = new SpatialReference(datasource.GetGCPProjection()); string projGCP = string.Empty; srGCP.ExportToWkt(out projGCP); if (!string.IsNullOrEmpty(projEsri)) { datasource.SetProjection(projEsri); sr = srEsri; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) morphed form ESRI format."); } else if (!string.IsNullOrEmpty(projGCP)) { datasource.SetProjection(projGCP); sr = srGCP; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) set from Ground Control Points (GCPs)."); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) is unknown or unsupported. " + "Try setting the SRS with the GdalWarp component using -t_srs EPSG:4326 for the option input."); //sr.SetWellKnownGeogCS("WGS84"); } ///Get info about image List <string> infoOptions = new List <string> { "-stats" }; datasourceInfo = Gdal.GDALInfo(datasource, new GDALInfoOptions(infoOptions.ToArray())); if (!string.IsNullOrEmpty(dstFileLocation)) { if (string.IsNullOrEmpty(options) && File.Exists(dstFileLocation)) { Dataset dst = Gdal.Open(dstFileLocation, Access.GA_ReadOnly); dstInfo = Gdal.GDALInfo(dst, null); dst.Dispose(); dstOutput = dstFileLocation; } else { Dataset dst = Gdal.wrapper_GDALTranslate(dstFileLocation, datasource, new GDALTranslateOptions(translateOptions), null, null); dstInfo = Gdal.GDALInfo(dst, new GDALInfoOptions(infoOptions.ToArray())); dst.Dispose(); dstOutput = dstFileLocation; } } datasource.Dispose(); } } DA.SetData(0, datasourceInfo); DA.SetData(1, dstInfo); DA.SetData(2, dstOutput); }
protected override void SolveInstance(IGH_DataAccess DA) { List <Curve> boundary = new List <Curve>(); DA.GetDataList <Curve>(0, boundary); string IMG_file = string.Empty; DA.GetData <string>(1, ref IMG_file); RESTful.GdalConfiguration.ConfigureGdal(); OSGeo.GDAL.Gdal.AllRegister(); Dataset datasource = Gdal.Open(IMG_file, Access.GA_ReadOnly); OSGeo.GDAL.Driver drv = datasource.GetDriver(); if (datasource == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "The vector datasource was unreadable by this component. It may not a valid file type for this component or otherwise null/empty."); return; } ///Get info about image string srcInfo = string.Empty; List <string> infoOptions = new List <string> { "-stats" }; srcInfo = Gdal.GDALInfo(datasource, new GDALInfoOptions(infoOptions.ToArray())); ///Get the spatial reference of the input raster file and set to WGS84 if not known ///Set up transform from source to WGS84 OSGeo.OSR.SpatialReference sr = new SpatialReference(Osr.SRS_WKT_WGS84); if (datasource.GetProjection() == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Coordinate Reference System (CRS) is missing. CRS set automatically set to WGS84."); } else { sr = new SpatialReference(datasource.GetProjection()); if (sr.Validate() != 0) { ///Check if SRS needs to be converted from ESRI format to WKT to avoid error: ///"No translation for Lambert_Conformal_Conic to PROJ.4 format is known." ///https://gis.stackexchange.com/questions/128266/qgis-error-6-no-translation-for-lambert-conformal-conic-to-proj-4-format-is-kn SpatialReference srEsri = sr; srEsri.MorphFromESRI(); string projEsri = string.Empty; srEsri.ExportToWkt(out projEsri); ///If no SRS exists, check Ground Control Points SRS SpatialReference srGCP = new SpatialReference(datasource.GetGCPProjection()); string projGCP = string.Empty; srGCP.ExportToWkt(out projGCP); if (!string.IsNullOrEmpty(projEsri)) { datasource.SetProjection(projEsri); sr = srEsri; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) morphed form ESRI format."); } else if (!string.IsNullOrEmpty(projGCP)) { datasource.SetProjection(projGCP); sr = srGCP; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) set from Ground Control Points (GCPs)."); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) is unknown or unsupported. SRS assumed to be WGS84." + "Try setting the SRS with the GdalWarp component using -t_srs EPSG:4326 for the option input."); sr.SetWellKnownGeogCS("WGS84"); } } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Data source SRS: EPSG:" + sr.GetAttrValue("AUTHORITY", 1)); } } //OSGeo.OSR.SpatialReference sr = new SpatialReference(ds.GetProjection()); OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference(""); dst.SetWellKnownGeogCS("WGS84"); OSGeo.OSR.CoordinateTransformation coordTransform = new OSGeo.OSR.CoordinateTransformation(sr, dst); OSGeo.OSR.CoordinateTransformation revTransform = new OSGeo.OSR.CoordinateTransformation(dst, sr); double[] adfGeoTransform = new double[6]; double[] invTransform = new double[6]; datasource.GetGeoTransform(adfGeoTransform); Gdal.InvGeoTransform(adfGeoTransform, invTransform); int width = datasource.RasterXSize; int height = datasource.RasterYSize; ///Dataset bounding box double oX = adfGeoTransform[0] + adfGeoTransform[1] * 0 + adfGeoTransform[2] * 0; double oY = adfGeoTransform[3] + adfGeoTransform[4] * 0 + adfGeoTransform[5] * 0; double eX = adfGeoTransform[0] + adfGeoTransform[1] * width + adfGeoTransform[2] * height; double eY = adfGeoTransform[3] + adfGeoTransform[4] * width + adfGeoTransform[5] * height; ///Transform to WGS84 double[] extMinPT = new double[3] { oX, eY, 0 }; double[] extMaxPT = new double[3] { eX, oY, 0 }; coordTransform.TransformPoint(extMinPT); coordTransform.TransformPoint(extMaxPT); Point3d dsMin = new Point3d(extMinPT[0], extMinPT[1], extMinPT[2]); Point3d dsMax = new Point3d(extMaxPT[0], extMaxPT[1], extMaxPT[2]); Rectangle3d dsbox = new Rectangle3d(Plane.WorldXY, Heron.Convert.WGSToXYZ(dsMin), Heron.Convert.WGSToXYZ(dsMax)); double pixelWidth = dsbox.Width / width; double pixelHeight = dsbox.Height / height; ///Declare trees GH_Structure <GH_Point> pointcloud = new GH_Structure <GH_Point>(); GH_Structure <GH_Integer> rCount = new GH_Structure <GH_Integer>(); GH_Structure <GH_Integer> cCount = new GH_Structure <GH_Integer>(); GH_Structure <GH_Mesh> tMesh = new GH_Structure <GH_Mesh>(); for (int i = 0; i < boundary.Count; i++) { GH_Path path = new GH_Path(i); Curve clippingBoundary = boundary[i]; if (!clip) { clippingBoundary = dsbox.ToNurbsCurve(); } string clippedTopoFile = "/vsimem/topoclipped.tif"; if (!(dsbox.BoundingBox.Contains(clippingBoundary.GetBoundingBox(true).Min) && (dsbox.BoundingBox.Contains(clippingBoundary.GetBoundingBox(true).Max))) && clip) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "One or more boundaries may be outside the bounds of the topo dataset."); } ///Offsets to mesh/boundary based on pixel size Point3d clipperMinPreAdd = clippingBoundary.GetBoundingBox(true).Corner(true, false, true); Point3d clipperMinPostAdd = new Point3d(clipperMinPreAdd.X, clipperMinPreAdd.Y, clipperMinPreAdd.Z); Point3d clipperMin = Heron.Convert.XYZToWGS(clipperMinPostAdd); Point3d clipperMaxPreAdd = clippingBoundary.GetBoundingBox(true).Corner(false, true, true); ///add/subtract pixel width if desired to get closer to boundary Point3d clipperMaxPostAdd = new Point3d(); Point3d clipperMax = new Point3d(); if (clip) { clipperMaxPostAdd = new Point3d(clipperMaxPreAdd.X + pixelWidth, clipperMaxPreAdd.Y - pixelHeight, clipperMaxPreAdd.Z); clipperMax = Heron.Convert.XYZToWGS(clipperMaxPostAdd); } else { clipperMaxPostAdd = new Point3d(clipperMaxPreAdd.X, clipperMaxPreAdd.Y, clipperMaxPreAdd.Z); clipperMax = Heron.Convert.XYZToWGS(clipperMaxPostAdd); } double lonWest = clipperMin.X; double lonEast = clipperMax.X; double latNorth = clipperMin.Y; double latSouth = clipperMax.Y; var translateOptions = new[] { "-of", "GTiff", "-a_nodata", "0", "-projwin_srs", "WGS84", "-projwin", $"{lonWest}", $"{latNorth}", $"{lonEast}", $"{latSouth}" }; using (Dataset clippedDataset = Gdal.wrapper_GDALTranslate(clippedTopoFile, datasource, new GDALTranslateOptions(translateOptions), null, null)) { Band band = clippedDataset.GetRasterBand(1); width = clippedDataset.RasterXSize; height = clippedDataset.RasterYSize; clippedDataset.GetGeoTransform(adfGeoTransform); Gdal.InvGeoTransform(adfGeoTransform, invTransform); rCount.Append(new GH_Integer(height), path); cCount.Append(new GH_Integer(width), path); Mesh mesh = new Mesh(); List <Point3d> verts = new List <Point3d>(); //var vertsParallel = new System.Collections.Concurrent.ConcurrentDictionary<double[][], Point3d>(Environment.ProcessorCount, ((Urow - 1) * (Lrow - 1))); double[] bits = new double[width * height]; band.ReadRaster(0, 0, width, height, bits, width, height, 0, 0); for (int col = 0; col < width; col++) { for (int row = 0; row < height; row++) { // equivalent to bits[col][row] if bits is 2-dimension array double pixel = bits[col + row * width]; if (pixel < -10000) { pixel = 0; } double gcol = adfGeoTransform[0] + adfGeoTransform[1] * col + adfGeoTransform[2] * row; double grow = adfGeoTransform[3] + adfGeoTransform[4] * col + adfGeoTransform[5] * row; ///convert to WGS84 double[] wgsPT = new double[3] { gcol, grow, pixel }; coordTransform.TransformPoint(wgsPT); Point3d pt = new Point3d(wgsPT[0], wgsPT[1], wgsPT[2]); verts.Add(Heron.Convert.WGSToXYZ(pt)); } /*Parallel.For(Urow, Lrow - 1, rowP => * { * // equivalent to bits[col][row] if bits is 2-dimension array * double pixel = bits[col + rowP * width]; * if (pixel < -10000) * { * pixel = 0; * } * * double gcol = adfGeoTransform[0] + adfGeoTransform[1] * col + adfGeoTransform[2] * rowP; * double grow = adfGeoTransform[3] + adfGeoTransform[4] * col + adfGeoTransform[5] * rowP; * * Point3d pt = new Point3d(gcol, grow, pixel); * vertsParallel[] = Heron.Convert.ToXYZ(pt); * }); * */ } //Create meshes //non Parallel mesh.Vertices.AddVertices(verts); //Parallel //mesh.Vertices.AddVertices(vertsParallel.Values); for (int u = 1; u < cCount[path][0].Value; u++) { for (int v = 1; v < rCount[path][0].Value; v++) { mesh.Faces.AddFace(v - 1 + (u - 1) * (height), v - 1 + u * (height), v - 1 + u * (height) + 1, v - 1 + (u - 1) * (height) + 1); //(k - 1 + (j - 1) * num2, k - 1 + j * num2, k - 1 + j * num2 + 1, k - 1 + (j - 1) * num2 + 1) } } //mesh.Flip(true, true, true); tMesh.Append(new GH_Mesh(mesh), path); band.Dispose(); } Gdal.Unlink("/vsimem/topoclipped.tif"); } datasource.Dispose(); DA.SetDataTree(0, tMesh); DA.SetData(1, dsbox); DA.SetData(2, srcInfo); }
private void message_form_Shown(object sender, EventArgs e) { Form1 f = Application.OpenForms["Form1"] as Form1; if (situations == status_value.save) { this.Text = ""; this.ControlBox = false; timer1.Enabled = true; this.Size = new Size(140, 140); pictureBox1.Visible = true; label5.Visible = true; richTextBox1.Visible = false; dataGridView1.Visible = false; label1.Visible = false; label2.Visible = false; } if ((situations == status_value.info_img) || (situations == status_value.info_emp)) { this.Text = "Information"; this.ControlBox = true; timer1.Enabled = false; this.Size = new Size(360, 440); pictureBox1.Visible = false; label5.Visible = false; dataGridView1.Visible = false; label1.Visible = false; label2.Visible = false; richTextBox1.Visible = true; richTextBox1.Location = new Point(0, 0); richTextBox1.Text = ""; if ((id == -1) || (Form1.Project_ID == -1)) { richTextBox1.Text = "no selected image or project!"; } else { if (situations == status_value.info_img) { Gdal.AllRegister(); Dataset data_igdal = Gdal.Open(Form1.Imagelist[id], Access.GA_ReadOnly); richTextBox1.Text = Gdal.GDALInfo(data_igdal, null); } else { richTextBox1.Text = "Image name: " + f.treeView1.Nodes[Form1.Project_ID].Nodes[id].Text.Substring(7) + "\n" + "Back color: " + Empty_image.empty_page_clr.Name + "\n" + "Bands: 0" + "\n" + "Page width: " + Form1.main_width[id].ToString() + "\n" + "Page height: " + Form1.main_height[id].ToString() + "\n" + "GCP count: " + Form1.gcp_collected[id].Rows.Count.ToString() + "\n" + "ICP count: " + Form1.icp_collected[id].Rows.Count.ToString(); } } } if (situations == status_value.info_proje) { this.Text = "Information"; this.ControlBox = true; timer1.Enabled = false; this.Size = new Size(645, 275); pictureBox1.Visible = false; label5.Visible = false; richTextBox1.Visible = false; dataGridView1.Visible = true; label1.Visible = true; label2.Visible = true; label1.Text = "Proje name: " + f.treeView1.Nodes[id].Text.Substring(9); label2.Text = "Proje Information: " + Form1.proje_info[id]; int i; for (i = 0; i < Form1.Imagelist.Count; i++) { dataGridView1.Rows.Add(f.treeView1.Nodes[id].Nodes[i].Text.Substring(7), Form1.bants_img_count[i].ToString(), Form1.main_width[i].ToString(), Form1.main_height[i].ToString(), Form1.gcp_collected[i].Rows.Count, Form1.icp_collected[i].Rows.Count); } } }
protected override void SolveInstance(IGH_DataAccess DA) { Curve boundary = null; DA.GetData <Curve>(0, ref boundary); string sourceFileLocation = string.Empty; DA.GetData <string>(1, ref sourceFileLocation); string clippedLocation = string.Empty; DA.GetData <string>(2, ref clippedLocation); RESTful.GdalConfiguration.ConfigureGdal(); OSGeo.GDAL.Gdal.AllRegister(); ///Specific settings for getting WMS images OSGeo.GDAL.Gdal.SetConfigOption("GDAL_HTTP_UNSAFESSL", "YES"); OSGeo.GDAL.Gdal.SetConfigOption("GDAL_SKIP", "WMS"); ///Read in the raster data Dataset datasource = Gdal.Open(sourceFileLocation, Access.GA_ReadOnly); OSGeo.GDAL.Driver drv = datasource.GetDriver(); string srcInfo = string.Empty; if (datasource == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "The raster datasource was unreadable by this component. It may not a valid file type for this component or otherwise null/empty."); return; } ///Get the spatial reference of the input raster file and set to WGS84 if not known ///Set up transform from source to WGS84 OSGeo.OSR.SpatialReference sr = new SpatialReference(Osr.SRS_WKT_WGS84); if (datasource.GetProjection() == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) is missing. SRS set automatically set to WGS84."); } else { sr = new SpatialReference(datasource.GetProjection()); if (sr.Validate() != 0) { ///Check if SRS needs to be converted from ESRI format to WKT to avoid error: ///"No translation for Lambert_Conformal_Conic to PROJ.4 format is known." ///https://gis.stackexchange.com/questions/128266/qgis-error-6-no-translation-for-lambert-conformal-conic-to-proj-4-format-is-kn SpatialReference srEsri = sr; srEsri.MorphFromESRI(); string projEsri = string.Empty; srEsri.ExportToWkt(out projEsri); ///If no SRS exists, check Ground Control Points SRS SpatialReference srGCP = new SpatialReference(datasource.GetGCPProjection()); string projGCP = string.Empty; srGCP.ExportToWkt(out projGCP); if (!string.IsNullOrEmpty(projEsri)) { datasource.SetProjection(projEsri); sr = srEsri; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) morphed form ESRI format."); } else if (!string.IsNullOrEmpty(projGCP)) { datasource.SetProjection(projGCP); sr = srGCP; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) set from Ground Control Points (GCPs)."); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) is unknown or unsupported. SRS assumed to be WGS84." + "Try setting the SRS with the GdalWarp component using -t_srs EPSG:4326 for the option input."); sr.SetWellKnownGeogCS("WGS84"); } } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Data source SRS: EPSG:" + sr.GetAttrValue("AUTHORITY", 1)); } } ///Get info about image List <string> infoOptions = new List <string> { "-stats" }; srcInfo = Gdal.GDALInfo(datasource, new GDALInfoOptions(infoOptions.ToArray())); //OSGeo.OSR.SpatialReference sr = new SpatialReference(ds.GetProjection()); OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference(""); dst.SetWellKnownGeogCS("WGS84"); OSGeo.OSR.CoordinateTransformation coordTransform = new OSGeo.OSR.CoordinateTransformation(sr, dst); OSGeo.OSR.CoordinateTransformation revTransform = new OSGeo.OSR.CoordinateTransformation(dst, sr); double[] adfGeoTransform = new double[6]; double[] invTransform = new double[6]; datasource.GetGeoTransform(adfGeoTransform); Gdal.InvGeoTransform(adfGeoTransform, invTransform); Band band = datasource.GetRasterBand(1); int width = datasource.RasterXSize; int height = datasource.RasterYSize; ///Dataset bounding box double oX = adfGeoTransform[0] + adfGeoTransform[1] * 0 + adfGeoTransform[2] * 0; double oY = adfGeoTransform[3] + adfGeoTransform[4] * 0 + adfGeoTransform[5] * 0; double eX = adfGeoTransform[0] + adfGeoTransform[1] * width + adfGeoTransform[2] * height; double eY = adfGeoTransform[3] + adfGeoTransform[4] * width + adfGeoTransform[5] * height; ///Transform to WGS84. ///TODO: Allow for UserSRS double[] extMinPT = new double[3] { oX, eY, 0 }; double[] extMaxPT = new double[3] { eX, oY, 0 }; coordTransform.TransformPoint(extMinPT); coordTransform.TransformPoint(extMaxPT); Point3d dsMin = new Point3d(extMinPT[0], extMinPT[1], extMinPT[2]); Point3d dsMax = new Point3d(extMaxPT[0], extMaxPT[1], extMaxPT[2]); ///Get bounding box for entire raster data Rectangle3d datasourceBBox = new Rectangle3d(Plane.WorldXY, Heron.Convert.WGSToXYZ(dsMin), Heron.Convert.WGSToXYZ(dsMax)); ///https://gis.stackexchange.com/questions/312440/gdal-translate-bilinear-interpolation ///set output to georeferenced tiff as a catch-all string clippedRasterFile = clippedLocation + Path.GetFileNameWithoutExtension(sourceFileLocation) + "_clipped.tif"; string previewPNG = clippedLocation + Path.GetFileNameWithoutExtension(sourceFileLocation) + "_preview.png"; AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Original Resolution: " + datasource.RasterXSize.ToString() + "x" + datasource.RasterYSize.ToString()); if (boundary != null) { Point3d clipperMin = Heron.Convert.XYZToWGS(boundary.GetBoundingBox(true).Corner(true, false, true)); Point3d clipperMax = Heron.Convert.XYZToWGS(boundary.GetBoundingBox(true).Corner(false, true, true)); double lonWest = clipperMin.X; double lonEast = clipperMax.X; double latNorth = clipperMin.Y; double latSouth = clipperMax.Y; ///GDALTranslate should also be its own component with full control over options var translateOptions = new[] { "-of", "GTiff", //"-a_nodata", "0", "-projwin_srs", "WGS84", "-projwin", $"{lonWest}", $"{latNorth}", $"{lonEast}", $"{latSouth}" }; using (Dataset clippedDataset = Gdal.wrapper_GDALTranslate(clippedRasterFile, datasource, new GDALTranslateOptions(translateOptions), null, null)) { Dataset previewDataset = Gdal.wrapper_GDALTranslate(previewPNG, clippedDataset, null, null, null); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Clipped Resolution: " + clippedDataset.RasterXSize.ToString() + "x" + datasource.RasterYSize.ToString()); ///clean up clippedDataset.FlushCache(); clippedDataset.Dispose(); previewDataset.FlushCache(); previewDataset.Dispose(); AddPreviewItem(previewPNG, BBoxToRect(boundary.GetBoundingBox(true))); } } else { Dataset previewDataset = Gdal.wrapper_GDALTranslate(previewPNG, datasource, null, null, null); ///clean up previewDataset.FlushCache(); previewDataset.Dispose(); AddPreviewItem(previewPNG, datasourceBBox); } ///clean up datasource.FlushCache(); datasource.Dispose(); DA.SetData(0, srcInfo); DA.SetData(1, datasourceBBox); DA.SetData(2, clippedRasterFile); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { string srcFileLocation = string.Empty; DA.GetData <string>(0, ref srcFileLocation); string dstFileLocation = string.Empty; DA.GetData <string>(1, ref dstFileLocation); string options = string.Empty; DA.GetData <string>(2, ref options); string[] translateOptions = options.Split(' '); string srcInfo = string.Empty; string dstInfo = string.Empty; string dstOutput = string.Empty; RESTful.GdalConfiguration.ConfigureGdal(); OSGeo.GDAL.Gdal.AllRegister(); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Look for more information about options at:"); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "https://gdal.org/programs/gdalwarp.html"); if (!string.IsNullOrEmpty(srcFileLocation)) { using (Dataset src = Gdal.Open(srcFileLocation, Access.GA_ReadOnly)) { if (src == null) { throw new Exception("Can't open GDAL dataset: " + srcFileLocation); } srcInfo = Gdal.GDALInfo(src, null); if (!string.IsNullOrEmpty(dstFileLocation)) { if (string.IsNullOrEmpty(options) && File.Exists(dstFileLocation)) { Dataset dst = Gdal.Open(dstFileLocation, Access.GA_ReadOnly); dstInfo = Gdal.GDALInfo(dst, null); dst.Dispose(); dstOutput = dstFileLocation; } else { ///https://github.com/OSGeo/gdal/issues/813 ///https://lists.osgeo.org/pipermail/gdal-dev/2017-February/046046.html ///Odd way to go about setting source dataset in parameters for Warp is a known issue var ptr = new[] { Dataset.getCPtr(src).Handle }; var gcHandle = GCHandle.Alloc(ptr, GCHandleType.Pinned); try { var dss = new SWIGTYPE_p_p_GDALDatasetShadow(gcHandle.AddrOfPinnedObject(), false, null); Dataset dst = Gdal.wrapper_GDALWarpDestName(dstFileLocation, 1, dss, new GDALWarpAppOptions(translateOptions), null, null); if (dst == null) { throw new Exception("GdalWarp failed: " + Gdal.GetLastErrorMsg()); } dstInfo = Gdal.GDALInfo(dst, null); dst.Dispose(); dstOutput = dstFileLocation; } finally { if (gcHandle.IsAllocated) { gcHandle.Free(); } } } } src.Dispose(); } } DA.SetData(0, srcInfo); DA.SetData(1, dstInfo); DA.SetData(2, dstOutput); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { string srcFileLocation = string.Empty; DA.GetData <string>(0, ref srcFileLocation); string dstFileLocation = string.Empty; DA.GetData <string>(1, ref dstFileLocation); string options = string.Empty; DA.GetData <string>(2, ref options); string[] translateOptions = options.Split(' '); string srcInfo = string.Empty; string dstInfo = string.Empty; string dstOutput = string.Empty; RESTful.GdalConfiguration.ConfigureGdal(); OSGeo.GDAL.Gdal.AllRegister(); ///Specific settings for getting WMS images OSGeo.GDAL.Gdal.SetConfigOption("GDAL_HTTP_UNSAFESSL", "YES"); OSGeo.GDAL.Gdal.SetConfigOption("GDAL_SKIP", "WMS"); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Look for more information about options at:"); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "https://gdal.org/programs/gdal_translate.html"); if (!string.IsNullOrEmpty(srcFileLocation)) { using (Dataset src = Gdal.Open(srcFileLocation, Access.GA_ReadOnly)) { if (src == null) { throw new Exception("Can't open GDAL dataset: " + srcFileLocation); } srcInfo = Gdal.GDALInfo(src, null); if (!string.IsNullOrEmpty(dstFileLocation)) { if (string.IsNullOrEmpty(options) && File.Exists(dstFileLocation)) { Dataset dst = Gdal.Open(dstFileLocation, Access.GA_ReadOnly); dstInfo = Gdal.GDALInfo(dst, null); dst.Dispose(); dstOutput = dstFileLocation; } else { Dataset dst = Gdal.wrapper_GDALTranslate(dstFileLocation, src, new GDALTranslateOptions(translateOptions), null, null); dstInfo = Gdal.GDALInfo(dst, null); dst.Dispose(); dstOutput = dstFileLocation; } } src.Dispose(); } } DA.SetData(0, srcInfo); DA.SetData(1, dstInfo); DA.SetData(2, dstOutput); }