Skip to content

Instantly share code, notes, and snippets.

@rabbl
Last active February 22, 2016 08:28
Show Gist options
  • Save rabbl/13fb257d4bcf6a2cc111 to your computer and use it in GitHub Desktop.
Save rabbl/13fb257d4bcf6a2cc111 to your computer and use it in GitHub Desktop.
Raster as Custom Mapping Type in Doctrine
<?php
namespace AppBundle\Model;
class RasterBand
{
/**
* @var float[][]
*/
private $data;
/**
* @var string
*/
private $pixelType;
/**
* @var float
*/
private $initValue;
/**
* @var float
*/
private $noDataVal;
/**
* @return \float[][]
*/
public function getData()
{
return $this->data;
}
/**
* @param \float[][] $data
* @return RasterBand
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* @return string
*/
public function getPixelType()
{
return $this->pixelType;
}
/**
* @param string $pixelType
* @return RasterBand
*/
public function setPixelType($pixelType)
{
$this->pixelType = $pixelType;
return $this;
}
/**
* @return float
*/
public function getInitValue()
{
return $this->initValue;
}
/**
* @param float $initValue
* @return RasterBand
*/
public function setInitValue($initValue)
{
$this->initValue = $initValue;
return $this;
}
/**
* @return float
*/
public function getNoDataVal()
{
return $this->noDataVal;
}
/**
* @param float $noDataVal
* @return RasterBand
*/
public function setNoDataVal($noDataVal)
{
$this->noDataVal = $noDataVal;
return $this;
}
}
<?php
/**
* More Documentation here:
*
* http://postgis.net/docs/manual-dev/RT_ST_MakeEmptyRaster.html
*
* raster ST_MakeEmptyRaster(raster rast);
* raster ST_MakeEmptyRaster(integer width, integer height, float8 upperleftx, float8 upperlefty, float8 scalex, float8 scaley, float8 skewx, float8 skewy, integer srid=unknown);
* raster ST_MakeEmptyRaster(integer width, integer height, float8 upperleftx, float8 upperlefty, float8 pixelsize);
*
*
* http://postgis.net/docs/manual-2.2/RT_ST_AddBand.html
*
*
*/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use AppBundle\Model\Raster as RasterModel;
/**
* Raster
*
* @ORM\Entity(repositoryClass="AppBundle\Entity\RasterRepository")
* @ORM\Table(name="rasters")
*/
class Raster
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var RasterModel $raster
*
* @ORM\Column(name="rast", type="raster", nullable=true)
*/
private $raster;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set raster
*
* @param RasterModel $raster
*
* @return Raster
*/
public function setRaster(RasterModel $raster)
{
$this->raster = $raster;
return $this;
}
/**
* Get raster
*
* @return Raster
*/
public function getRaster()
{
return $this->raster;
}
}
<?php
namespace AppBundle\Model;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Raster
*/
class Raster
{
/**
* @var Raster $raster
*/
private $raster;
/**
* @var integer
*/
private $width;
/**
* @var integer
*/
private $height;
/**
* @var float
*/
private $upperLeftX;
/**
* @var float
*/
private $upperLeftY;
/**
* @var float
*/
private $scaleX;
/**
* @var float
*/
private $scaleY;
/**
* @var float
*/
private $skewX;
/**
* @var float
*/
private $skewY;
/**
* @var float
*/
private $pixelSize;
/**
* @var integer
*/
private $srid;
/**
* @var ArrayCollection RasterBand
*/
private $bands;
/**
* Raster constructor.
*/
public function __construct()
{
$this->bands = new ArrayCollection();
}
/**
* @return Raster
*/
public function getRaster()
{
return $this->raster;
}
/**
* @param Raster $raster
* @return Raster
*/
public function setRaster($raster)
{
$this->raster = $raster;
return $this;
}
/**
* @return int
*/
public function getWidth()
{
return $this->width;
}
/**
* @param int $width
* @return Raster
*/
public function setWidth($width)
{
$this->width = $width;
return $this;
}
/**
* @return int
*/
public function getHeight()
{
return $this->height;
}
/**
* @param int $height
* @return Raster
*/
public function setHeight($height)
{
$this->height = $height;
return $this;
}
/**
* @return float
*/
public function getUpperLeftX()
{
return $this->upperLeftX;
}
/**
* @param float $upperLeftX
* @return Raster
*/
public function setUpperLeftX($upperLeftX)
{
$this->upperLeftX = $upperLeftX;
return $this;
}
/**
* @return float
*/
public function getUpperLeftY()
{
return $this->upperLeftY;
}
/**
* @param float $upperLeftY
* @return Raster
*/
public function setUpperLeftY($upperLeftY)
{
$this->upperLeftY = $upperLeftY;
return $this;
}
/**
* @return float
*/
public function getScaleX()
{
return $this->scaleX;
}
/**
* @param float $scaleX
* @return Raster
*/
public function setScaleX($scaleX)
{
$this->scaleX = $scaleX;
return $this;
}
/**
* @return float
*/
public function getScaleY()
{
return $this->scaleY;
}
/**
* @param float $scaleY
* @return Raster
*/
public function setScaleY($scaleY)
{
$this->scaleY = $scaleY;
return $this;
}
/**
* @return float
*/
public function getSkewX()
{
return $this->skewX;
}
/**
* @param float $skewX
* @return Raster
*/
public function setSkewX($skewX)
{
$this->skewX = $skewX;
return $this;
}
/**
* @return float
*/
public function getSkewY()
{
return $this->skewY;
}
/**
* @param float $skewY
* @return Raster
*/
public function setSkewY($skewY)
{
$this->skewY = $skewY;
return $this;
}
/**
* @return float
*/
public function getPixelSize()
{
return $this->pixelSize;
}
/**
* @param float $pixelSize
* @return Raster
*/
public function setPixelSize($pixelSize)
{
$this->pixelSize = $pixelSize;
return $this;
}
/**
* @return int
*/
public function getSrid()
{
return $this->srid;
}
/**
* @param int $srid
* @return Raster
*/
public function setSrid($srid)
{
$this->srid = $srid;
return $this;
}
/**
* @return ArrayCollection
*/
public function getBands()
{
return $this->bands;
}
/**
* @param ArrayCollection $bands
* @return Raster
*/
public function setBands($bands)
{
$this->bands = $bands;
return $this;
}
/**
* @param RasterBand $band
* @return array|ArrayCollection
*/
public function addBand(RasterBand $band)
{
$this->bands[] = $band;
return $this->bands;
}
/**
* @param RasterBand $band
* @return $this
*/
public function removeBand(RasterBand $band)
{
$this->bands->removeElement($band);
return $this;
}
}
<?php
namespace AppBundle\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
class RasterType extends Type
{
const RASTER = 'raster';
public function getName()
{
return self::RASTER;
}
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return 'raster';
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
/**
* ToDo: Finish implementing it
*/
return new \AppBundle\Model\Raster();
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return sprintf('%d, %d, %f, %f, %f, %f, %f, %f, %d', 10, 10, 0.1, 0.1, 0.1, 0.1, 0, 0, 4269);
}
public function canRequireSQLConversion()
{
return true;
}
public function convertToPHPValueSQL($sqlExpr, $platform)
{
/**
* ToDo: Finish implementing it
*/
return sprintf('AsText(%s)', $sqlExpr);
}
public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
{
return sprintf('st_makeemptyraster(%s)', $sqlExpr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment