Example #1
0
 public function afterFind()
 {
     parent::afterFind();
     $scheme = static::getTableSchema();
     foreach ($scheme->columns as $column) {
         if (static::isSpatial($column)) {
             $field = $column->name;
             $attr = $this->getAttribute($field);
             // get WKT
             if ($attr) {
                 if (YII_DEBUG && preg_match('/[\\x80-\\xff]+/', $attr)) {
                     /* If you get an exception here, it probably means you have overridden find()
                        and did not return sjaakp\spatial\ActiveQuery. */
                     throw new InvalidCallException('Spatial attribute not converted.');
                 }
                 $geom = SpatialHelper::wktToGeom($attr);
                 // Transform geometry FeatureCollection...
                 if ($geom['type'] == 'GeometryCollection') {
                     $feats = [];
                     foreach ($geom['geometries'] as $g) {
                         $feats[] = ['type' => 'Feature', 'geometry' => $g, 'properties' => $this->featureProperties($field, $g)];
                     }
                     $feature = ['type' => 'FeatureCollection', 'features' => $feats];
                 } else {
                     // ... or to Feature
                     $feature = SpatialHelper::geomToFeature($geom, $this->featureProperties($field, $geom));
                 }
                 $this->setAttribute($field, Json::encode($feature));
             }
         }
     }
 }