我试图在PostGis中使用自定义映射类型实现Raster功能。要将Raster添加到数据库,
我正在创建一个包含栅格类型列的表。 要插入Raster-Data我用st_makeemptyraster插入一个新的空Raster,添加一个带有st_addband的新Band并用st_setvalues设置值。
所以,这可以通过这种方式在PLAIN SQL中完成:
CREATE TABLE IF NOT EXISTS rasters (id serial, rast raster);
INSERT INTO rasters(id,rast)
VALUES(
3,
st_setvalues(
st_addband(
ST_MakeEmptyRaster( 2, 2, 0.0005, 0.0005, 1, 1, 0, 0, 4326),
1,
'32BF',
1,
0
),
1,
1,
1,
ARRAY[[9, 9], [9, 9]]::double precision[][]
)
);
现在我尝试在doctrine中为raster添加自定义映射类型。
我添加了一个实体Raster:
<?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-Object:
<?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);
}
}
在我的第一个意图中,我试图将空栅格添加到数据库中。 而不是插入或更新栅格,将出现以下错误:
2) AppBundle\Tests\Entity\RasterTest::testCreateEmptyRasterAndAddBand
Doctrine\DBAL\Exception\DriverException: An exception occurred while executing 'INSERT INTO rasters (id, rast) VALUES (?, ?)' with params [2, "st_makeemptyraster(10, 10, 0.100000, 0.100000, 0.100000, 0.100000, 0.000000, 0.000000, 4269)"]:
SQLSTATE[XX000]: Internal error: 7 ERROR: rt_raster_from_wkb: wkb size (46) < min size (61)
与纯SQL-Query相同的数据可以正常工作:
INSERT INTO rasters (id, rast) VALUES (14, st_makeemptyraster(10, 10, 0.100000, 0.100000, 0.100000, 0.100000, 0.000000, 0.000000, 4269));
INSERT 0 1
有人知道如何解决这个问题吗? 谢谢
答案 0 :(得分:1)
(我无法评论我的声誉低于50.所以我只能写回答。有些mod可能会改为评论吗?)
我不熟悉Doctrine,但首先我对一个常见问题有一个假设。
您能否确保在构建查询之前,对任何查询传递值都不受字符串转换的影响?使用参数执行查询可能会将它们转换为带有转义字符的字符串值,以避免出现安全问题。
一个例子:
$yourDoctrineAdapter->executeInsert( 42, "storedProcedureCall( 'foo' )" );
可能会产生类似
的内容 INSERT INTO foo ( bar, baz ) VALUES ( '42', 'storedProcedureCall( \'foo\' )' );
我的第二个假设是关于内部错误消息而不是错误的类型。
rt_raster_from_wkb: wkb size (46) < min size (61)
。
我不知道st_makeemptyraster()
的返回值是什么,但是来自Doctrine的调用可能仍会导致字符串参数而不是存储的函数调用。插入列rast
中的值是类型框,然后是字符串化&#34;调用&#34;的WKB大小。小于存储函数返回值st_makeemptyraster()
的WKB大小。
我建议深入研究如何使用doctrine&#39;来执行存储的函数调用。