コード例 #1
0
ファイル: markupmanager.php プロジェクト: kanbang/Colt
 function UploadMarkup()
 {
     $locale = "en";
     if (array_key_exists("LOCALE", $this->args)) {
         $locale = $this->args["LOCALE"];
     }
     $uploadFileParts = pathinfo($_FILES["UPLOADFILE"]["name"]);
     $fdoProvider = $this->GetProviderFromExtension($uploadFileParts["extension"]);
     if ($fdoProvider == null) {
         throw new Exception(GetLocalizedString("REDLINEUPLOADUNKNOWNPROVIDER", $locale));
     }
     $resourceService = $this->site->CreateService(MgServiceType::ResourceService);
     $featureService = $this->site->CreateService(MgServiceType::FeatureService);
     $bs = new MgByteSource($_FILES["UPLOADFILE"]["tmp_name"]);
     $br = $bs->GetReader();
     //Use file name to drive all parameters
     $baseName = $uploadFileParts["filename"];
     $this->UniqueMarkupName($baseName);
     //Guard against potential duplicates
     $ext = $uploadFileParts["extension"];
     $markupLayerResId = new MgResourceIdentifier($this->GetResourceIdPrefix() . $baseName . ".LayerDefinition");
     $markupFsId = new MgResourceIdentifier($this->GetResourceIdPrefix() . $baseName . '.FeatureSource');
     //Set up feature source document
     $dataName = $baseName . "." . $ext;
     $fileParam = "File";
     $shpFileParts = array();
     if (strcmp($fdoProvider, "OSGeo.SHP") == 0) {
         $dataName = null;
         $fileParam = "DefaultFileLocation";
         $zip = new ZipArchive();
         if ($zip->open($_FILES["UPLOADFILE"]["tmp_name"]) === TRUE) {
             for ($i = 0; $i < $zip->numFiles; $i++) {
                 $stat = $zip->statIndex($i);
                 $filePath = tempnam(sys_get_temp_dir(), "upload");
                 //Dump to temp file
                 file_put_contents($filePath, $zip->getFromIndex($i));
                 //Stash for later upload and cleanup
                 $entry = $stat["name"];
                 $shpFileParts[$entry] = $filePath;
                 if (substr($entry, strlen($entry) - strlen(".shp")) == ".shp") {
                     $dataName = $entry;
                 }
             }
             //Abort if we can't find a .shp file. This is not a valid zip file
             if ($dataName == null) {
                 throw new Exception(GetLocalizedString("REDLINEUPLOADSHPZIPERROR", $locale));
             }
         } else {
             throw new Exception(GetLocalizedString("REDLINEUPLOADSHPZIPERROR", $locale));
         }
     }
     $extraXml = "";
     if (strcmp($fdoProvider, "OSGeo.SDF") == 0) {
         //Need to set ReadOnly = false for SDF
         $extraXml = "<Parameter><Name>ReadOnly</Name><Value>FALSE</Value></Parameter>";
     }
     $fsXml = sprintf(file_get_contents("templates/markupfeaturesource.xml"), $fdoProvider, $fileParam, $dataName, $extraXml);
     $bs2 = new MgByteSource($fsXml, strlen($fsXml));
     $resourceService->SetResource($markupFsId, $bs2->GetReader(), null);
     if (count($shpFileParts) > 0) {
         foreach ($shpFileParts as $name => $path) {
             $bs3 = new MgByteSource($path);
             $resourceService->SetResourceData($markupFsId, $name, "File", $bs3->GetReader());
             //Cleanup
             unlink($path);
         }
     } else {
         //Not a SHP file
         $resourceService->SetResourceData($markupFsId, $dataName, "File", $bs->GetReader());
     }
     //Query the geometry types
     $schemas = $featureService->DescribeSchema($markupFsId, "", null);
     $schema = $schemas->GetItem(0);
     $classes = $schema->GetClasses();
     $klass = $classes->GetItem(0);
     $geomProp = $klass->GetDefaultGeometryPropertyName();
     $clsProps = $klass->GetProperties();
     $className = $schema->GetName() . ":" . $klass->GetName();
     $geomTypes = -1;
     if ($clsProps->IndexOf($geomProp) >= 0) {
         $geom = $clsProps->GetItem($geomProp);
         $geomTypes = $geom->GetGeometryTypes();
         //Since we're here. Validate the schema requirements. If this was created by this widget previously
         //it will be valid
         $idProps = $klass->GetIdentityProperties();
         if ($idProps->GetCount() != 1) {
             throw new Exception(GetLocalizedString("REDLINEUPLOADINVALIDSCHEMA", $locale));
         } else {
             //Must be auto-generated (implying numerical as well)
             $keyProp = $idProps->GetItem(0);
             if (!$keyProp->IsAutoGenerated()) {
                 throw new Exception(GetLocalizedString("REDLINEUPLOADINVALIDSCHEMA", $locale));
             }
         }
         if ($clsProps->IndexOf("Text") < 0) {
             throw new Exception(GetLocalizedString("REDLINEUPLOADINVALIDSCHEMA", $locale));
         }
     } else {
         throw new Exception(GetLocalizedString("REDLINEUPLOADINVALIDSCHEMA", $locale));
     }
     //Set up default style args
     $this->args["MARKUPNAME"] = $baseName;
     $this->args["MARKERCOLOR"] = DefaultStyle::MARKER_COLOR;
     $this->args["MARKERTYPE"] = DefaultStyle::MARKER_TYPE;
     $this->args["MARKERSIZEUNITS"] = DefaultStyle::MARKER_SIZE_UNITS;
     $this->args["MARKERSIZE"] = DefaultStyle::MARKER_SIZE;
     $this->args["LINECOLOR"] = DefaultStyle::LINE_COLOR;
     $this->args["LINEPATTERN"] = DefaultStyle::LINE_PATTERN;
     $this->args["LINESIZEUNITS"] = DefaultStyle::LINE_SIZE_UNITS;
     $this->args["LINETHICKNESS"] = DefaultStyle::LINE_THICKNESS;
     $this->args["FILLPATTERN"] = DefaultStyle::FILL_PATTERN;
     $this->args["FILLTRANSPARENCY"] = DefaultStyle::FILL_TRANSPARENCY;
     $this->args["FILLFORECOLOR"] = DefaultStyle::FILL_FORE_COLOR;
     $this->args["FILLBACKCOLOR"] = DefaultStyle::FILL_BACK_COLOR;
     $this->args["FILLBACKTRANS"] = DefaultStyle::FILL_BACK_TRANS;
     $this->args["BORDERPATTERN"] = DefaultStyle::BORDER_PATTERN;
     $this->args["BORDERSIZEUNITS"] = DefaultStyle::BORDER_SIZE_UNITS;
     $this->args["BORDERCOLOR"] = DefaultStyle::BORDER_COLOR;
     $this->args["BORDERTHICKNESS"] = DefaultStyle::BORDER_THICKNESS;
     $this->args["LABELSIZEUNITS"] = DefaultStyle::LABEL_SIZE_UNITS;
     $this->args["LABELFONTSIZE"] = DefaultStyle::LABEL_FONT_SIZE;
     //Omission is considered false, which is the default. If you ever change
     //the default style values, uncomment the matching "true" values
     //$this->args["LABELBOLD"] = DefaultStyle::LABEL_BOLD;
     //$this->args["LABELITALIC"] = DefaultStyle::LABEL_ITALIC;
     //$this->args["LABELUNDERLINE"] = DefaultStyle::LABEL_UNDERLINE;
     $this->args["LABELFORECOLOR"] = DefaultStyle::LABEL_FORE_COLOR;
     $this->args["LABELBACKCOLOR"] = DefaultStyle::LABEL_BACK_COLOR;
     $this->args["LABELBACKSTYLE"] = DefaultStyle::LABEL_BACK_STYLE;
     $markupLayerDefinition = $this->CreateMarkupLayerDefinitionContent($markupFsId->ToString(), $className);
     $layerBs = new MgByteSource($markupLayerDefinition, strlen($markupLayerDefinition));
     //Save to new resource or overwrite existing
     $resourceService->SetResource($markupLayerResId, $layerBs->GetReader(), null);
     //Add to markup registry
     $cmds = new MgFeatureCommandCollection();
     $props = new MgPropertyCollection();
     $props->Add(new MgStringProperty("ResourceId", $markupFsId->ToString()));
     $props->Add(new MgStringProperty("LayerDefinition", $markupLayerResId->ToString()));
     $props->Add(new MgStringProperty("Name", $markupLayerResId->GetName()));
     $props->Add(new MgStringProperty("FdoProvider", $fdoProvider));
     $props->Add(new MgInt32Property("GeometryTypes", $geomTypes));
     $insertCmd = new MgInsertFeatures("Default:MarkupRegistry", $props);
     $cmds->Add($insertCmd);
     $res = $featureService->UpdateFeatures($this->markupRegistryId, $cmds, false);
     MarkupManager::CleanupReaders($res);
     //Add to map
     $this->args["MARKUPLAYER"] = $markupLayerResId->ToString();
     $this->OpenMarkup();
 }