/** * Implementation générique pour GetFeatures. */ public function getFeatures($polygon) { if (!$this->isAuthenticated()) { return $this->getAuthenticationErrorMessage(); } $wkt = GeometryConverter::ConvertJsonToWkt($polygon); $fields_sql = $this->CreateSqlSelectFieldsSection(); $fields_sql .= "," . $this->getIdentifier(); $spatialQueryBuilder = $this->getSpatialQueryBuilder(); $transform = $spatialQueryBuilder->ST_Transform($this->getGeometryName(), 4326); $geomAsText = $spatialQueryBuilder->ST_AsText($transform); $srid = $this->getSRID(); $bboxFromText = $spatialQueryBuilder->ST_GeomFromText($wkt, $srid); $bbox_srid = $spatialQueryBuilder->ST_SetSRID($bboxFromText, 4326); $bbox = $spatialQueryBuilder->ST_Transform($bbox_srid, $this->getSRID()); $intersects = $spatialQueryBuilder->ST_Intersects($this->getGeometryName(), $bbox); $sql = "SELECT {$fields_sql}, {$geomAsText} as {$this->getGeometryName()} FROM {$this->getViewName()} WHERE {$intersects} AND {$this->getStatutName()} <> 'D'"; $config = $this->getDi()->get("config"); if ($config->application->debug == true) { error_log($sql); } $connection = $this->getConnection(); $features = array(); $result = $connection->query($sql); $result->setFetchMode(\Phalcon\Db::FETCH_ASSOC); $fields = $this->getFields(null); if (!$result) { throw new Exception('Database error'); } while ($r = $result->fetch()) { $geom = $r[$this->getGeometryName()]; $feature = new stdClass(); $feature->type = "Feature"; $feature->geometry = GeometryConverter::ConvertWktToJson($this->getGeometryType(), $r[$this->getGeometryName()]); $feature->properties = new stdClass(); for ($i = 0; $i < count($fields); $i++) { $fieldName = $fields[$i]->propriete; $feature->properties->{$fieldName} = $r[$fields[$i]->propriete]; } $identifierName = $this->getIdentifier(); $feature->properties->{$identifierName} = $r[$this->getIdentifier()]; $features[] = $feature; } $featureCollection = new stdClass(); $featureCollection->type = "FeatureCollection"; $featureCollection->features = $features; return $featureCollection; }
public static function ConvertWktToJson($geometryType, $wkt) { if ($geometryType == "point") { return GeometryConverter::ConvertWktPointToJson($wkt); } else { if ($geometryType == "LineString") { return GeometryConverter::ConvertWktPolylineToJson($wkt); } else { if ($geometryType == "polygon") { return GeometryConverter::ConvertWktPolygonToJson($wkt); } else { if ($geometryType == "circle") { return GeometryConverter::ConvertWktPolygonToJson($wkt); } else { throw new Exception("ERREUR, non implémenté: {$geometryType}"); } } } } }
/** * Constructs the sql geometry. * * @return string representing the sql geometry. */ protected function GetSqlGeometry($geometry) { $sqlGeometry = null; if ($this->getGeometryType() == "point" || $this->getGeometryType() == "LineString" || $this->getGeometryType() == "polygon" || $this->getGeometryType() == "circle") { $wkt = GeometryConverter::ConvertJsonToWkt($geometry); $spatialQueryBuilder = $this->getSpatialQueryBuilder(); $geomFromText = $spatialQueryBuilder->ST_GeomFromText($wkt); $geomFromTextSrid = $spatialQueryBuilder->ST_SetSrid($geomFromText, 4326); return $spatialQueryBuilder->ST_Transform($geomFromTextSrid, $this->getSRID()); } else { throw new Exception("ERREUR, non implémenté: {$this->getGeometryType()}"); } return $sqlGeometry; }