Skip to content

Commit

Permalink
Feature/vrtdataset (#15)
Browse files Browse the repository at this point in the history
* VRTDataset and minor breaking changes.

OverviewResampling -> Resampling since not only overview use it

* getSrcDstWin

* ColorInterp for HSRasterBand. Fixed typo

* getters and setters for scale, offset and unittype

* toGDALDataset returns a Dataset instead of a handle
  • Loading branch information
albertov authored Aug 9, 2018
1 parent c3313ac commit 1bc49ec
Show file tree
Hide file tree
Showing 12 changed files with 547 additions and 50 deletions.
7 changes: 5 additions & 2 deletions bindings-gdal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ library
, GDAL
, GDAL.Algorithms
, GDAL.Warper
, GDAL.VRT

, GDAL.Internal.Types
, GDAL.Internal.Types.Value
Expand All @@ -74,6 +75,7 @@ library
, GDAL.Internal.Warper
, GDAL.Internal.HSDataset
, GDAL.Internal.HSDriver
, GDAL.Internal.VRT
build-depends: base >= 4.5 && < 5
, ghc-prim >= 0.3.1 && < 0.6
, bytestring >= 0.10.4 && < 0.11
Expand Down Expand Up @@ -112,7 +114,7 @@ library
install-includes: bindings.h
default-language: Haskell2010
include-dirs: include cbits
cc-options: -Wall -O3 -g
cc-options: -Wall -g
c-sources: cbits/errorhandler.c
, cbits/contourwriter.c
, cbits/driver.cpp
Expand Down Expand Up @@ -160,11 +162,12 @@ test-suite spec
, OGRGeometrySpec
, GDAL.AlgorithmsSpec
, GDAL.WarperSpec
, GDAL.VRTSpec
, Paths_bindings_gdal
, TestUtils
, Arbitrary
default-language: Haskell2010
ghc-options: -Wall -rtsopts -threaded -with-rtsopts=-N -O
ghc-options: -Wall -rtsopts -threaded -with-rtsopts=-N
if flag(static)
ghc-options: -optl-static -optl-static-libstdc++ -optl-static-libgcc -pgml=g++
include-dirs: src/GDAL/Internal/
Expand Down
16 changes: 16 additions & 0 deletions cbits/hsdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ class HSRasterBand : public GDALRasterBand

virtual CPLErr IReadBlock( int, int, void * );
virtual double GetNoDataValue( int *pbSuccess = NULL );
virtual CPLErr SetColorInterpretation(GDALColorInterp);
virtual GDALColorInterp GetColorInterpretation();

private:
const HsStablePtr state;
GDALColorInterp colorInterp;
const double noDataValue;
const bool hasNodata;
int (*const pfnReadBlock)(const HsStablePtr, const int, const int, void*);
Expand Down Expand Up @@ -157,6 +160,7 @@ HSRasterBand::HSRasterBand( HSDataset *poDS, int nBand,
this->nBlockXSize = impl.nBlockXSize;
this->nBlockYSize = impl.nBlockYSize;
this->eDataType = impl.eDataType;
this->colorInterp = impl.colorInterp;
}

/************************************************************************/
Expand Down Expand Up @@ -189,7 +193,19 @@ double HSRasterBand::GetNoDataValue( int *pbSuccess )
return this->noDataValue;
}

/************************************************************************/
/* HSRasterBand::GetColorInterpretation() */
/************************************************************************/
GDALColorInterp HSRasterBand::GetColorInterpretation()
{
return this->colorInterp;
}

CPLErr HSRasterBand::SetColorInterpretation(GDALColorInterp colorInterp)
{
this->colorInterp = colorInterp;
return CE_None;
}

/************************************************************************/
/* hs_gdal_create_dataset */
Expand Down
1 change: 1 addition & 0 deletions cbits/hsdataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef struct hsRasterBandImpl {
GDALDataType eDataType;
double nodata;
int hasNodata;
GDALColorInterp colorInterp;
int (*readBlock)( HsStablePtr, int, int, void* );
}* HSRasterBandImpl;

Expand Down
13 changes: 10 additions & 3 deletions src/GDAL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ module GDAL (
, isInterruptedException

, ColorInterp(..)
, Georeference (..)
, Geotransform (..)
, GroundControlPoint (..)
, OverviewResampling (..)
, Resampling (..)
, Driver
, DriverName
, Dataset
Expand Down Expand Up @@ -127,8 +128,8 @@ module GDAL (
, setDatasetGCPs
, datasetBandCount

, bandColorInterpretaion
, setBandColorInterpretaion
, bandColorInterpretation
, setBandColorInterpretation
, bandDataType
, bandProjection
, bandGeotransform
Expand All @@ -142,6 +143,12 @@ module GDAL (
, sizeLen
, bandNodataValue
, setBandNodataValue
, bandOffset
, setBandOffset
, bandScale
, setBandScale
, bandUnitType
, setBandUnitType
, addBand
, getBand
, readDatasetRGBA
Expand Down
9 changes: 7 additions & 2 deletions src/GDAL/Internal/CPLError.chs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module GDAL.Internal.CPLError (
, throwBindingException
, bindingExceptionToException
, bindingExceptionFromException
, cplError
, checkCPLError
, checkGDALCall
, checkGDALCall_
Expand All @@ -40,8 +41,8 @@ import Foreign.Ptr (Ptr, FunPtr, nullPtr)
import Foreign.Storable (peek)
import Foreign.Marshal.Utils (with)

import GDAL.Internal.Util (toEnumC, runBounded)
import GDAL.Internal.CPLString (peekEncodedCString)
import GDAL.Internal.Util (fromEnumC, toEnumC, runBounded)
import GDAL.Internal.CPLString (peekEncodedCString, useAsEncodedCString)

#include "cpl_error.h"
#include "errorhandler.h"
Expand Down Expand Up @@ -151,5 +152,9 @@ withQuietErrorHandler a = runBounded ((pushIt >> a) `finally` popIt)
popIt = {#call unsafe CPLPopErrorHandler as ^#}


cplError :: ErrorType -> ErrorNum -> Text -> IO ()
cplError err errNum msg = useAsEncodedCString msg $
{#call unsafe CPLError as ^#} (fromEnumC err) (fromEnumC errNum)

foreign import ccall "cpl_error.h &CPLQuietErrorHandler"
c_quietErrorHandler :: FunPtr (CInt -> CInt -> Ptr CChar -> IO ())
15 changes: 15 additions & 0 deletions src/GDAL/Internal/DataType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module GDAL.Internal.DataType (

, sizeOfDataType
, hsDataType
, dataTypeK
) where

import GDAL.Internal.Types (Pair(..), pFst)
Expand All @@ -39,6 +40,7 @@ import Data.Coerce (coerce)

import Foreign.Storable (Storable(sizeOf))
import Foreign.C.Types (CDouble(..))
import GHC.TypeLits


data DataType :: * -> * where
Expand Down Expand Up @@ -80,7 +82,9 @@ class ( Storable a
, Show a
, Num a
, Typeable a
, KnownNat (SizeOf a)
) => GDALType a where
type SizeOf a :: Nat
dataType :: DataType a
toCDouble :: a -> CDouble
fromCDouble :: CDouble -> a
Expand Down Expand Up @@ -114,6 +118,7 @@ hsDataType _ = dataTypeK (dataType :: DataType a)
------------------------------------------------------------------------------

instance GDALType Word8 where
type SizeOf Word8 = 1
dataType = GDT_Byte
toCDouble = fromIntegral
fromCDouble = truncate
Expand All @@ -122,6 +127,7 @@ instance GDALType Word8 where
{-# INLINE fromCDouble #-}

instance GDALType Word16 where
type SizeOf Word16 = 2
dataType = GDT_UInt16
toCDouble = fromIntegral
fromCDouble = truncate
Expand All @@ -130,6 +136,7 @@ instance GDALType Word16 where
{-# INLINE fromCDouble #-}

instance GDALType Word32 where
type SizeOf Word32 = 4
dataType = GDT_UInt32
toCDouble = fromIntegral
fromCDouble = truncate
Expand All @@ -138,6 +145,7 @@ instance GDALType Word32 where
{-# INLINE fromCDouble #-}

instance GDALType Int16 where
type SizeOf Int16 = 2
dataType = GDT_Int16
toCDouble = fromIntegral
fromCDouble = truncate
Expand All @@ -146,6 +154,7 @@ instance GDALType Int16 where
{-# INLINE fromCDouble #-}

instance GDALType Int32 where
type SizeOf Int32 = 4
dataType = GDT_Int32
toCDouble = fromIntegral
fromCDouble = truncate
Expand All @@ -154,6 +163,7 @@ instance GDALType Int32 where
{-# INLINE fromCDouble #-}

instance GDALType Float where
type SizeOf Float = 4
dataType = GDT_Float32
toCDouble = realToFrac
fromCDouble = realToFrac
Expand All @@ -162,6 +172,7 @@ instance GDALType Float where
{-# INLINE fromCDouble #-}

instance GDALType Double where
type SizeOf Double = 8
dataType = GDT_Float64
-- We use coerce to work around https://ghc.haskell.org/trac/ghc/ticket/3676
toCDouble = coerce
Expand All @@ -171,6 +182,7 @@ instance GDALType Double where
{-# INLINE fromCDouble #-}

instance GDALType (Pair Int16) where
type SizeOf (Pair Int16) = 4
dataType = GDT_CInt16
toCDouble = fromIntegral . pFst
fromCDouble = (:+: 0) . truncate
Expand All @@ -179,6 +191,7 @@ instance GDALType (Pair Int16) where
{-# INLINE fromCDouble #-}

instance GDALType (Pair Int32) where
type SizeOf (Pair Int32) = 8
dataType = GDT_CInt32
toCDouble = fromIntegral . pFst
fromCDouble = (:+: 0) . truncate
Expand All @@ -187,6 +200,7 @@ instance GDALType (Pair Int32) where
{-# INLINE fromCDouble #-}

instance GDALType (Pair Float) where
type SizeOf (Pair Float) = 8
dataType = GDT_CFloat32
toCDouble = realToFrac . pFst
fromCDouble = (:+: 0) . realToFrac
Expand All @@ -195,6 +209,7 @@ instance GDALType (Pair Float) where
{-# INLINE fromCDouble #-}

instance GDALType (Pair Double) where
type SizeOf (Pair Double) = 16
dataType = GDT_CFloat64
toCDouble = coerce . pFst
fromCDouble = (:+: 0) . coerce
Expand Down
Loading

0 comments on commit 1bc49ec

Please sign in to comment.