Skip to content

Instantly share code, notes, and snippets.

@furkantektas
Created September 12, 2014 14:35
Show Gist options
  • Save furkantektas/e9d1fbbe8388d83e4270 to your computer and use it in GitHub Desktop.
Save furkantektas/e9d1fbbe8388d83e4270 to your computer and use it in GitHub Desktop.
PostGIS GeoJSON validator for Yii. Place these files under application/protected/components/
<?php
require_once('SpatialGeoJSONValidator.php');
/**
* PointValidator class for Yii.
* Author: Furkan Tektas github.com/furkantektas
* Date: 2014/09/12
* Version: v1.0
* Sample GeoJSON: {"type":"Point","coordinates":[41.00603847483829,29.009458422660828],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}}
*/
class PointValidator extends SpatialGeoJSONValidator {
public $geometryType = 'ST_Point';
public $geometryName = 'point';
}
<?php
require_once('SpatialGeoJSONValidator.php');
/**
* PolygonValidator class for Yii.
* Author: Furkan Tektas github.com/furkantektas
* Date: 2014/09/12
* Version: v1.0
* Sample GeoJSON: {"type":"Polygon","coordinates":[[[29.010263085365295,41.00709100018642],[29.006754755973816,41.00742294700135],[29.006711840629578,41.00730959949646],[29.008331894874573,41.004880677538175],[29.012473225593567,41.00445965529471],[29.010263085365295,41.00709100018642]]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}}
*/
class PolygonValidator extends SpatialGeoJSONValidator {
public $geometryType = 'ST_Polygon';
public $geometryName = 'polygon';
}
<?php
/**
* SpatialGeoJSONValidator class for Yii.
* Author: Furkan Tektas github.com/furkantektas
* Date: 2014/09/12
* Version: v1.0
*/
abstract class SpatialGeoJSONValidator extends CValidator
{
/**
* @var boolean whether the attribute value can be null or empty. Defaults to true,
* meaning that if the attribute is empty, it is considered valid.
*/
public $allowEmpty=true;
const SQL_QUERY_FORMAT_STRING = "SELECT ST_IsValid(ST_GeomFromGeoJSON('%s')), ST_GeometryType(ST_GeomFromGeoJSON('%s'))";
const ERROR_MESSAGE = "Invalid %s structure.";
public $geometryType = '';
public $geometryName = '';
/**
* validateAttribute validates GeoJSON object and it's shape.
* $geometryType should be correctly set to work properly.
*
* string is not valid, exception is thrown and regarded as invalid GeoJSON object.
* @inheritDoc
*/
public function validateAttribute($object, $attribute)
{
if($this->allowEmpty && $this->isEmpty($object->$attribute))
return;
$query = sprintf(SpatialGeoJSONValidator::SQL_QUERY_FORMAT_STRING,trim($object->{$attribute}),trim($object->{$attribute}));
try {
$command = Yii::app()->db->createCommand($query)->queryRow();
if(!$command["st_isvalid"] || $command['st_geometrytype'] != $this->geometryType)
$this->addError($object, $attribute, SpatialGeoJSONValidator::ERROR_MESSAGE);
} catch (Exception $e) {
$this->addError($object, $attribute, sprintf(SpatialGeoJSONValidator::ERROR_MESSAGE,$this->geometryName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment