private function OutputGeoJson($schemas)
 {
     $read = 0;
     $agfRw = new MgAgfReaderWriter();
     $this->writer->SetHeader("Content-Type", MgMimeType::Json);
     $this->writer->StartChunking();
     $output = '{ "type": "FeatureCollection", "features": [' . "\n";
     $clsDef = $this->reader->GetClassDefinition();
     $clsIdProps = $clsDef->GetIdentityProperties();
     $idProp = NULL;
     if ($clsIdProps->GetCount() == 1) {
         $idProp = $clsIdProps->GetItem(0);
     }
     $propCount = $this->reader->GetPropertyCount();
     $firstFeature = true;
     while ($this->reader->ReadNext()) {
         $read++;
         if ($this->limit > 0 && $read > $this->limit) {
             break;
         }
         if (!$firstFeature) {
             $output .= ",";
         }
         $output .= MgGeoJsonWriter::FeatureToGeoJson($this->reader, $agfRw, $this->transform, $idProp != NULL ? $idProp->GetName() : NULL);
         $this->writer->WriteChunk($output);
         $output = "";
         $firstFeature = false;
     }
     $output .= "]}";
     $this->writer->WriteChunk($output);
     $this->writer->EndChunking();
     $this->reader->Close();
 }
Beispiel #2
0
 public static function PolygonToGeoJson($poly)
 {
     $str = '[';
     $extRing = $poly->GetExteriorRing();
     if ($extRing != null) {
         $coords = $extRing->GetCoordinates();
         $str .= MgGeoJsonWriter::CoordsToGeoJson($coords);
     }
     $count = $poly->GetInteriorRingCount();
     if ($count > 0) {
         if ($extRing != null) {
             $str .= ",";
         }
         for ($i = 0; $i < $count; $i++) {
             $ring = $poly->GetInteriorRing($i);
             $coords = $ring->GetCoordinates();
             $str .= MgGeoJsonWriter::CoordsToGeoJson($coords);
             if ($i < $count - 1) {
                 $str .= ",";
             }
         }
     }
     $str .= "]";
     return $str;
 }
 private function PutVectorTileXYZ($map, $groupName, $siteConn, $metersPerUnit, $csFactory, $path, $boundsMinx, $boundsMinY, $boundsMaxX, $boundsMaxY, $layerNames)
 {
     $wktRw = new MgWktReaderWriter();
     $agfRw = new MgAgfReaderWriter();
     $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
     $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
     $mapCsWkt = $map->GetMapSRS();
     $layers = $map->GetLayers();
     $groups = $map->GetLayerGroups();
     $baseGroup = $groups->GetItem($groupName);
     $layerCount = $layers->GetCount();
     $firstFeature = true;
     $scale = self::GetScaleFromBounds($map, self::XYZ_TILE_WIDTH, self::XYZ_TILE_HEIGHT, $metersPerUnit, $boundsMinx, $boundsMinY, $boundsMaxX, $boundsMaxY);
     $fp = fopen($path, "w");
     fwrite($fp, '{ "type": "FeatureCollection", "features": [');
     for ($i = 0; $i < $layerCount; $i++) {
         $layer = $layers->GetItem($i);
         $parentGroup = $layer->GetGroup();
         if ($parentGroup != null && $parentGroup->GetObjectId() == $baseGroup->GetObjectId()) {
             if (!self::IsLayerVisibleAtScale($layer, $resSvc, $scale)) {
                 continue;
             }
             //If list of layer names specified, skip if this layer is not in that list
             if ($layerNames != null) {
                 $bFound = false;
                 foreach ($layerNames as $layerName) {
                     if ($layer->GetName() == $layerName) {
                         $bFound = true;
                         break;
                     }
                 }
                 if (!$bFound) {
                     continue;
                 }
             }
             $wktPoly = MgUtils::MakeWktPolygon($boundsMinx, $boundsMinY, $boundsMaxX, $boundsMaxY);
             $geom = $wktRw->Read($wktPoly);
             $clsDef = $layer->GetClassDefinition();
             $clsProps = $clsDef->GetProperties();
             $idProps = $clsDef->GetIdentityProperties();
             $idName = NULL;
             if ($idProps->GetCount() == 1) {
                 $idp = $idProps->GetItem(0);
                 $idName = $idp->GetName();
             }
             $fsId = new MgResourceIdentifier($layer->GetFeatureSourceId());
             //Set up forward and inverse transforms. Inverse for transforming map bounding box
             //Forward for transforming source geometries to map space
             $xform = self::GetTransform($featSvc, $fsId, $clsDef, $mapCsWkt, $csFactory);
             $query = new MgFeatureQueryOptions();
             $geomName = $layer->GetFeatureGeometryName();
             if ($xform != null) {
                 $sourceCs = $xform->GetSource();
                 $targetCs = $xform->GetTarget();
                 $invXform = $csFactory->GetTransform($targetCs, $sourceCs);
                 $txgeom = $geom->Transform($invXform);
                 $query->SetSpatialFilter($geomName, $txgeom, MgFeatureSpatialOperations::EnvelopeIntersects);
             } else {
                 $query->SetSpatialFilter($geomName, $geom, MgFeatureSpatialOperations::EnvelopeIntersects);
             }
             for ($p = 0; $p < $clsProps->GetCount(); $p++) {
                 $propDef = $clsProps->GetItem($p);
                 $query->AddFeatureProperty($propDef->GetName());
             }
             //If we're rendering a vector tile for a single layer, we don't need these special attributes
             if ($layerNames == NULL || count($layerNames) > 1) {
                 $query->AddComputedProperty("_displayIndex", $layerCount - $i);
                 $query->AddComputedProperty("_layer", "'" . $layer->GetName() . "'");
                 $query->AddComputedProperty("_selectable", $layer->GetSelectable() ? "true" : "false");
             }
             $reader = $layer->SelectFeatures($query);
             $read = 0;
             while ($reader->ReadNext()) {
                 $read++;
                 if (!$reader->IsNull($geomName)) {
                     if (!$firstFeature) {
                         fwrite($fp, ",");
                     }
                     try {
                         $output = MgGeoJsonWriter::FeatureToGeoJson($reader, $agfRw, $xform, $idName);
                         fwrite($fp, $output);
                         $firstFeature = false;
                     } catch (MgException $ex) {
                     }
                 }
             }
             $reader->Close();
         }
     }
     fwrite($fp, ']}');
     fclose($fp);
     return $path;
 }
 /**
  * Writes the GET response body based on the current record of the given MgReader. The caller must not advance to the next record
  * in the reader while inside this method
  */
 protected function GetResponseBodyRecord($reader)
 {
     $output = "";
     if (!$this->firstFeature) {
         $output .= ",";
     }
     $propVals = array();
     $geomJson = "";
     $propCount = $reader->GetPropertyCount();
     for ($i = 0; $i < $propCount; $i++) {
         $name = $reader->GetPropertyName($i);
         $propType = $reader->GetPropertyType($i);
         if (!$reader->IsNull($i)) {
             switch ($propType) {
                 case MgPropertyType::Boolean:
                     array_push($propVals, '"' . $name . '": ' . $reader->GetBoolean($i));
                     break;
                 case MgPropertyType::Byte:
                     array_push($propVals, '"' . $name . '": ' . $reader->GetByte($i));
                     break;
                 case MgPropertyType::DateTime:
                     $dt = $reader->GetDateTime($i);
                     array_push($propVals, '"' . $name . '": "' . $dt->ToString() . '"');
                     break;
                 case MgPropertyType::Decimal:
                 case MgPropertyType::Double:
                     array_push($propVals, '"' . $name . '": ' . $reader->GetDouble($i));
                     break;
                 case MgPropertyType::Geometry:
                     try {
                         $agf = $reader->GetGeometry($i);
                         $geom = $this->transform != null ? $this->agfRw->Read($agf, $this->transform) : $this->agfRw->Read($agf);
                         $geomJson = MgGeoJsonWriter::ToGeoJson($geom);
                     } catch (MgException $ex) {
                         $geomJson = '"geometry": null';
                     }
                     break;
                 case MgPropertyType::Int16:
                     array_push($propVals, '"' . $name . '": ' . $reader->GetInt16($i));
                     break;
                 case MgPropertyType::Int32:
                     array_push($propVals, '"' . $name . '": ' . $reader->GetInt32($i));
                     break;
                 case MgPropertyType::Int64:
                     array_push($propVals, '"' . $name . '": ' . $reader->GetInt64($i));
                     break;
                 case MgPropertyType::Single:
                     array_push($propVals, '"' . $name . '": ' . $reader->GetSingle($i));
                     break;
                 case MgPropertyType::String:
                     array_push($propVals, '"' . $name . '": "' . MgUtils::EscapeJsonString($reader->GetString($i)) . '"');
                     break;
             }
         } else {
             array_push($propVals, '"' . $name . '": null');
         }
     }
     if ($geomJson !== "") {
         $output .= '{ "type": "Feature", ' . $geomJson . ', "properties": {' . implode(",", $propVals) . "} }\n";
     } else {
         $output .= '{ "type": "Feature", "properties": {' . implode(",", $propVals) . "} }\n";
     }
     $this->app->response->write($output);
     $output = "";
     $this->firstFeature = false;
 }