/// <summary> /// Union this rectangle with another /// </summary> /// <param name="rect"></param> /// <returns></returns> public ExtentRectangle Union(ExtentRectangle rect) { decimal newright, newleft, newtop, newbottom; if (CellHeight > 0) { newtop = Math.Min(Top, rect.Top); newbottom = Math.Max(Bottom, rect.Bottom); } else { newtop = Math.Max(Top, rect.Top); newbottom = Math.Min(Bottom, rect.Bottom); } if (CellWidth > 0) { newleft = Math.Min(Left, rect.Left); newright = Math.Max(Right, rect.Right); } else { newleft = Math.Max(Left, rect.Left); newright = Math.Min(Right, rect.Right); } int newcols = (int)((newright - newleft) / Math.Abs(CellWidth)); int newrows = (int)((newtop - newbottom) / Math.Abs(CellHeight)); return(new ExtentRectangle(newtop, newleft, CellHeight, CellWidth, newrows, newcols)); }
/// <summary> /// Create from existing rectangle but apply new cell size /// </summary> /// <param name="orig"></param> public ExtentRectangle(ExtentRectangle orig, decimal mCellHeight, decimal mCellWidth) { int newRows = Convert.ToInt32(Math.Ceiling(orig.Height / Math.Abs(mCellHeight))); int newCols = Convert.ToInt32(Math.Floor(orig.Width / Math.Abs(mCellWidth))); _Init(orig.Top, orig.Left, mCellHeight, mCellWidth, newRows, newCols); }
/// <summary> /// We just want a list of keys back /// </summary> /// <param name="ext"></param> /// <returns></returns> public List <long> FIDIntersectExtent(ExtentRectangle ext) { Open(); List <long> retVal = new List <long>(); Geometry ring = new Geometry(wkbGeometryType.wkbLinearRing); ring.AddPoint((double)ext.Left, (double)ext.Top, 0); ring.AddPoint((double)ext.Right, (double)ext.Top, 0); ring.AddPoint((double)ext.Right, (double)ext.Bottom, 0); ring.AddPoint((double)ext.Left, (double)ext.Bottom, 0); ring.AddPoint((double)ext.Left, (double)ext.Top, 0); Geometry extrect = new Geometry(wkbGeometryType.wkbPolygon); extrect.AddGeometry(ring); foreach (KeyValuePair <long, VectorFeature> kvp in Features) { Geometry geo = kvp.Value.Feat.GetGeometryRef(); if (geo != null && extrect.Intersects(geo)) { retVal.Add(kvp.Key); } } return(retVal); }
/// <summary> /// This is a create constructor /// </summary> /// <param name="rTemplate"></param> /// <param name="rExtent"></param> /// <param name="sFilename"></param> /// <param name="leaveopen"></param> public Raster(Raster rTemplate, ExtentRectangle rExtent, FileInfo sFilename, bool leaveopen = false) : base(sFilename) { _Init(rTemplate.driver, rTemplate.VerticalUnits, rTemplate.Proj, rTemplate.Datatype, rExtent, rTemplate.origNodataVal); if (!leaveopen) { UnloadDS(); } }
/// <summary> /// Temporary file constructor /// </summary> /// <param name="rTemplate"></param> /// <param name="sFilename"></param> /// <param name="leaveopen"></param> public Raster(Raster rTemplate, Projection proj) : base(TempRasterFileInfo()) { _temporary = true; ExtentRectangle theExtent = new ExtentRectangle(rTemplate.Extent); _Init(RasterDriver.GTiff, rTemplate.VerticalUnits, proj, rTemplate.Datatype, theExtent, rTemplate.origNodataVal); Create(); }
/// <summary> /// Is this raster concurrent with another? /// </summary> /// <param name="otherExtent"></param> /// <returns></returns> public bool IsConcurrent(ExtentRectangle otherExtent) { return(CellWidth == otherExtent.CellWidth && CellHeight == otherExtent.CellHeight && Left == otherExtent.Left && Right == otherExtent.Right && Top == otherExtent.Top && Bottom == otherExtent.Bottom); }
/// <summary> /// Return (x,y) integer typle representing how shifted one rectangle is relative to this one /// </summary> /// <param name="rect"></param> /// <returns>Tuple (x,y)</returns> public int[] GetTranslation(ExtentRectangle rect) { int ydiff = (int)(rect.Top - Top / Math.Abs(CellHeight)); int xdiff = (int)(rect.Left - Left / Math.Abs(CellWidth)); return(new int[2] { xdiff, ydiff }); }
/// <summary> /// Get a tuple representing row translation between two Extentrectangles /// </summary> /// <param name="rExtent1"></param> /// <param name="rExtent2"></param> /// <returns>(colT, rowT)</returns> public int[] GetTopCornerTranslationRowCol(ExtentRectangle rExtent2) { int colT = (int)((Left - rExtent2.Left) / CellWidth); int rowT = (int)((Top - rExtent2.Top) / CellHeight); return(new int[2] { rowT, colT }); }
/// <summary> /// Determine if two extents overlap /// </summary> /// <param name="otherExtent"></param> /// <returns></returns> public bool HasOverlap(ExtentRectangle otherExtent) { // TODO: TEST THIS THOROUGHLY, ESPECIALLY -/+ widht heights decimal ulx = Math.Max(Left, otherExtent.Left); decimal uly = Math.Max(Bottom, otherExtent.Bottom); decimal lrx = Math.Min(Right, otherExtent.Right); decimal lry = Math.Min(Top, otherExtent.Top); return(ulx <= lrx && uly <= lry); }
/// <summary> /// Pass in a list of rasters to extend the extent of this raster /// </summary> /// <param name="rasters"></param> /// <returns></returns> public static ExtentRectangle RasterExtentExpand(List <Raster> rasters) { ExtentRectangle newExtent = rasters[0].Extent; foreach (Raster raster in rasters) { newExtent.Union(raster.Extent); } return(newExtent); }
// This is mainly for testing purposes public Raster(Raster rTemplate, bool leaveopen = false) : base() { _temporary = false; ExtentRectangle theExtent = new ExtentRectangle(rTemplate.Extent); _Init(RasterDriver.GTiff, rTemplate.VerticalUnits, rTemplate.Proj, rTemplate.Datatype, theExtent, rTemplate.origNodataVal); if (!leaveopen) { UnloadDS(); } }
/// <summary> /// Initialization function /// </summary> protected void _Init(RasterDriver rdDriver, LengthUnit lUnits, Projection proj, GdalDataType dType, ExtentRectangle theExtent, double?ndv) { _writepermission = false; origNodataVal = ndv; Datatype = dType; Extent = theExtent; Proj = proj; VerticalUnits = lUnits; driver = rdDriver; }
/// <summary> /// Explicit Constructor to create a new raster /// </summary> /// <param name="fTop"></param> /// <param name="fLeft"></param> /// <param name="dCellHeight"></param> /// <param name="dCellWidth"></param> /// <param name="nRows"></param> /// <param name="nCols"></param> /// <param name="dNoData"></param> /// <param name="psDriver"></param> /// <param name="eDataType"></param> /// <param name="psProjection"></param> /// <param name="psUnit"></param> public Raster(decimal fTop, decimal fLeft, decimal dCellHeight, decimal dCellWidth, int nRows, int nCols, double?dNoData, RasterDriver psDriver, GdalDataType dType, string psProjection, string psUnit, bool leaveopen = false) : base() { _temporary = false; ExtentRectangle theExtent = new ExtentRectangle(fTop, fLeft, dCellHeight, dCellWidth, nRows, nCols); _Init(psDriver, UnitFromString(psUnit), new Projection(psProjection), dType, theExtent, dNoData); if (!leaveopen) { UnloadDS(); } }
/// <summary> /// Translate the 1D array index into another rectangle's 1D array index /// </summary> /// <param name="origid"></param> /// <param name="otherExtent"></param> /// <returns>the transformed ID, -1 if it's not valid</returns> public int RelativeId(int origid, ExtentRectangle otherExtent) { int[] origRowCol = Id2RowCol(origid); int[] offsetRowCol = GetTopCornerTranslationRowCol(otherExtent); int newInd; int newRow = origRowCol[0] + offsetRowCol[0]; int newCol = origRowCol[1] + offsetRowCol[1]; if (newCol <= 0 || newRow <= 0 || newCol > otherExtent.Cols || newRow > otherExtent.Rows) { newInd = -1; } else { newInd = (newRow - 1) * otherExtent.Cols + (newCol) - 1; } return(newInd); }
/// <summary> /// Transform a cell into a rectangle geometry /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <param name="ext"></param> /// <returns></returns> public static Geometry CellToRect(int row, int col, ExtentRectangle ext) { double l = (double)(ext.Left + ((col - 1) * ext.CellWidth)); double r = l + (double)ext.CellWidth; double t = (double)(ext.Top + ((row - 1) * ext.CellHeight)); double b = t + (double)ext.CellWidth; Geometry ring = new Geometry(wkbGeometryType.wkbLinearRing); ring.AddPoint(l, t, 0); ring.AddPoint(r, t, 0); ring.AddPoint(r, b, 0); ring.AddPoint(l, b, 0); ring.AddPoint(l, t, 0); Geometry extrect = new Geometry(wkbGeometryType.wkbPolygon); extrect.AddGeometry(ring); return(extrect); }
public void CreateDS(Raster.RasterDriver driver, FileInfo finfo, ExtentRectangle theExtent, Projection proj, GdalDataType theType) { List <string> creationOpts = new List <string>(); switch (driver) { case Raster.RasterDriver.GTiff: creationOpts.Add("COMPRESS=LZW"); break; case Raster.RasterDriver.HFA: creationOpts.Add("COMPRESS=PACKBITS"); break; } Driver driverobj = Gdal.GetDriverByName(Enum.GetName(typeof(Raster.RasterDriver), driver)); _ds = driverobj.Create(finfo.FullName, theExtent.Cols, theExtent.Rows, 1, theType._origType, creationOpts.ToArray()); _ds.SetGeoTransform(theExtent.Transform); _ds.SetProjection(proj.OriginalString); SetNoData((double)origNodataVal); }
/// <summary> /// Load all relevant settings from a file /// </summary> protected override void _initfromfile() { bool leaveOpen = IsOpen; Open(); Band rBand1 = _ds.GetRasterBand(1); GdalDataType dType = new GdalDataType(rBand1.DataType); string sDriver = _ds.GetDriver().ShortName; ExtentRectangle theExtent = new ExtentRectangle(_ds); _Init(_str2driver(sDriver), UnitFromString(rBand1.GetUnitType()), new Projection(_ds.GetProjection()), dType, theExtent, GetNodataVal()); if (!leaveOpen) { UnloadDS(); } }
/// <summary> /// Othoganility check for this rectangle /// </summary> /// <param name="otherExtent"></param> /// <returns></returns> public bool IsOrthogonal(ExtentRectangle otherExtent) { return(IsDivisible() && otherExtent.IsDivisible()); }
public bool Equals(ExtentRectangle other) { return(IsConcurrent(other)); }
/// <summary> /// Copy constructor /// </summary> /// <param name="orig"></param> public ExtentRectangle(ExtentRectangle orig) { _Init(orig.Top, orig.Left, orig.CellHeight, orig.CellWidth, orig.Rows, orig.Cols); }
public List <Geometry> GeometriesIntersectExtent(ExtentRectangle ext) { List <VectorFeature> feats = FeaturesIntersectExtent(ext).Values.ToList(); return(feats.Select(x => x.Feat.GetGeometryRef()).ToList()); }