Exemplo n.º 1
0
 /// <summary>
 /// Checking Data Types to make sure they are compatible
 /// </summary>
 /// <param name="dType"></param>
 private void checkType(GdalDataType dType)
 {
     // We need to make two conversions to make sure this is the right thing
     if (!dType.Equivalent(Datatype))
     {
         throw new InvalidDataException(string.Format("You are trying operate on a '{0}' raster using '{1}'. This is not allowed.", Datatype.TypeName, dType.TypeName));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Sometimes we want to create a raster with all properties the same as a template except DataType (think Hillshade)
        /// </summary>
        /// <param name="rTemplate"></param>
        /// <param name="sFilename"></param>
        /// <param name="leaveopen"></param>
        public Raster(Raster rTemplate, FileInfo sFilename, GdalDataType dType, bool leaveopen = false) : base(sFilename)
        {
            _temporary = false;
            ExtentRectangle theExtent = new ExtentRectangle(rTemplate.Extent);

            _Init(RasterDriver.GTiff, rTemplate.VerticalUnits, rTemplate.Proj, dType, theExtent, rTemplate.origNodataVal);
            if (!leaveopen)
            {
                UnloadDS();
            }
        }
Exemplo n.º 3
0
 /// <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;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Given a Template Raster, a vector and a field name, create a usable rasterized poly mask
        /// Notice how we use everything from the template raster except the projection, which MUST
        /// come from the vector in case there are slight differences.
        /// </summary>
        /// <param name="Template">Template will be used for Shape and projection only</param>
        /// <param name="input"></param>
        /// <param name="FieldName"></param>
        public VectorRaster(Raster Template, Vector vectorInput) : base(Template, vectorInput.Proj)
        {
            // This won't get populated
            FieldValues = new Dictionary <int, string> {
            };
            Datatype    = new GdalDataType(typeof(int));

            SetNoData(-1.0);

            // Do GDaL's rasterize first to get the rough boolean shape.
            Rasterize(vectorInput, this);
        }
Exemplo n.º 5
0
        /// <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();
            }
        }
Exemplo n.º 6
0
        /// <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();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Given a Template Raster, a vector and a field name, create a usable rasterized poly mask
        /// Notice how we use everything from the template raster except the projection, which MUST
        /// come from the vector in case there are slight differences.
        /// </summary>
        /// <param name="Template">Template will be used for Shape and projection only</param>
        /// <param name="input"></param>
        /// <param name="FieldName"></param>
        public VectorRaster(Raster Template, Vector vectorInput, string FieldName) : base(Template, vectorInput.Proj)
        {
            FieldValues = new Dictionary <int, string> {
            };
            Datatype    = new GdalDataType(typeof(int));

            SetNoData(-1.0);
            // Do GDaL's rasterize first to get the rough boolean shape.
            Rasterize(vectorInput, this);

            int fieldIndex = vectorInput.Features.First().Value.Feat.GetFieldIndex(FieldName);

            if (fieldIndex == -1)
            {
                throw new IndexOutOfRangeException(String.Format("Could not find field: `{0}`", FieldName));
            }

            int GDALMASKidx = vectorInput.Features.First().Value.Feat.GetFieldIndex(Vector.CGDMASKFIELD);

            if (GDALMASKidx == -1)
            {
                throw new IndexOutOfRangeException(String.Format("Could not find MANDATORY field: `{0}`", FieldName));
            }

            // Now make an equivalence between the GCDFID field and the FieldName Values
            foreach (KeyValuePair <long, VectorFeature> kvp in vectorInput.Features)
            {
                int    maskid = kvp.Value.Feat.GetFieldAsInteger(GDALMASKidx);
                string val    = kvp.Value.Feat.GetFieldAsString(fieldIndex);
                if (!FieldValues.ContainsKey(maskid))
                {
                    FieldValues.Add(maskid, val);
                }
            }
            vectorInput.Dispose();
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
 public bool Equivalent(GdalDataType otherType)
 {
     return(GDalSubsetType == otherType.GDalSubsetType);
 }
Exemplo n.º 10
0
 // Equivalence Operators
 public bool Equals(GdalDataType otherType)
 {
     return(_origType == otherType._origType);
 }
Exemplo n.º 11
0
 public GdalDataType(GdalDataType origType)
 {
     _origType = origType._origType;
 }