[mapguide-users] Adding a temporary Layer in MapGuide
Kenneth Skovhede, GEOGRAF A/S
ks at geograf.dk
Fri Jan 9 03:52:05 EST 2009
Please be aware that the code below attempts to create the layer before
the viewer
starts up. As I read the question, the map is already loaded, but should
have the layer added.
Most of the steps are the same, until this line and down:
$md = "Library://Test/X.MapDefinition";
You should instead get a reference to the runtime map, and
then call layers.add() with an MgLayer object.
Regards, Kenneth Skovhede, GEOGRAF A/S
Arnaud De Groof skrev:
>
> Hi,
>
>
>
> Please find an example of code for the different steps:
>
>
>
> <?php
>
>
>
> include "../../mapviewerphp/constants.php";
>
> include "../../mapviewerphp/common.php";
>
> //include 'common.php';
>
> //include 'constants.php';
>
>
>
> $args = ($_SERVER['REQUEST_METHOD'] == "POST") ? $_POST : $_GET;
>
> $sessionId = $args['SESSION'];
>
> $mapName = $args['MAPNAME'];
>
>
>
> $locale = "en";
>
> $dataName = "test";
>
> $popup = 0;
>
> $lcolor = "0000ff";
>
> $ffcolor = "ff0000";
>
> $fbcolor = "ffffff";
>
> $transparent = 0;
>
> $distance = 0;
>
> $units = "Points";
>
> $linestyle = "Solid";
>
> $fillstyle = "Solid";
>
> $thickness = 1;
>
> $merge = 0;
>
> $foretrans = 50;
>
> $srs = "";
>
> $featureName = "Z";
>
> $params = null;
>
>
>
>
>
> //SetLocalizedFilesPath(GetLocalizationPath());
>
>
>
> $dataSource = "Session:" . $sessionId . "//" . $dataName .
> ".FeatureSource";
>
> $layerDef = "Session:" . $sessionId . "//" . $dataName .
> ".LayerDefinition";
>
>
>
> InitializeWebTier();
>
>
>
> $cred = new MgUserInformation($sessionId);
>
> $cred->SetClientIp(GetClientIp());
>
> $cred->SetClientAgent(GetClientAgent());
>
>
>
> //connect to the site and get a feature service and a resource
> service instances
>
> $site = new MgSiteConnection();
>
> $site->Open($cred);
>
> $featureSrvc =
> $site->CreateService(MgServiceType::FeatureService);
>
> $resourceSrvc =
> $site->CreateService(MgServiceType::ResourceService);
>
>
>
> $dataSourceId = new MgResourceIdentifier($dataSource);
>
> $layerDefId = new MgResourceIdentifier($layerDef);
>
>
>
> //load the map runtime state
>
> $map = new MgMap();
>
> $map->Open($resourceSrvc, $mapName);
>
>
>
> //locate the buffer layer in the map. It might or might not
> already exist
>
> $layers = $map->GetLayers();
>
>
>
> // Get the map SRS
>
> $srsFactory = new MgCoordinateSystemFactory();
>
> $srsDefMap = GetMapSRS($map);
>
> $mapSrsUnits = "";
>
> $srsMap = $srsFactory->Create($srsDefMap);
>
> $arbitraryMapSrs = $srsMap->GetType() ==
> MgCoordinateSystemType::Arbitrary;
>
> if($arbitraryMapSrs)
>
> $mapSrsUnits = $srsMap->GetUnits();
>
>
>
> //Create/Modify layer definition
>
> $layerDefContent = BuildLayerDefinitionContent();
>
> $resourceSrvc->SetResource($layerDefId, $layerDefContent, null);
>
>
>
> $classDef = new MgClassDefinition();
>
>
>
> $classDef->SetName($featureName);
>
>
> $classDef->SetDescription(GetLocalizedString("DATACLASSDESCR", $locale));
>
> $classDef->SetDefaultGeometryPropertyName("GEOM");
>
>
>
> //Set KEY property
>
> $prop = new MgDataPropertyDefinition("KEY");
>
> $prop->SetDataType(MgPropertyType::Int32);
>
> $prop->SetAutoGeneration(true);
>
> $prop->SetReadOnly(true);
>
> $classDef->GetIdentityProperties()->Add($prop);
>
> $classDef->GetProperties()->Add($prop);
>
>
>
> //Set ID property.
>
> $prop = new MgDataPropertyDefinition("ID");
>
> $prop->SetDataType(MgPropertyType::Int32);
>
> $classDef->GetProperties()->Add($prop);
>
>
>
> //Set geometry property
>
> $prop = new MgGeometricPropertyDefinition("GEOM");
>
>
> ////$prop->SetGeometryTypes(MgFeatureGeometricType::mfgtSurface);
> //*TODO* use the constant when exposed
>
> $prop->SetGeometryTypes(4);
>
> $classDef->GetProperties()->Add($prop);
>
>
>
> //Create the schema
>
> $schema = new MgFeatureSchema("DataSchema",
> GetLocalizedString("DATASCHEMADESCR", $locale));
>
> $schema->GetClasses()->Add($classDef);
>
>
>
> //finally, creation of the feature source
>
> $sdfParams = new MgCreateSdfParams("LatLong", $srsDefMap,
> $schema);
>
> $featureSrvc->CreateFeatureSource($dataSourceId, $sdfParams);
>
>
>
> //Add layer to map
>
> $layer = new MgLayer($layerDefId, $resourceSrvc);
>
> $layer->SetName($dataName);
>
> $layer->SetLegendLabel($dataName);
>
> $layer->SetDisplayInLegend(true);
>
> $layer->SetSelectable(true);
>
> $layers->Insert(0, $layer);
>
>
>
> $fsResourceId = new MgResourceIdentifier($dataSource);
>
> $fsReader = $resourceSrvc->GetResourceContent($fsResourceId);
>
> $fsXml = $fsReader->ToString();
>
> $fsDomDoc = DOMDocument::loadXML($fsXml);
>
>
>
> $ldResourceId = new MgResourceIdentifier($layerDef);
>
> $ldReader = $resourceSrvc->GetResourceContent($ldResourceId);
>
> $ldXml = $ldReader->ToString();
>
> $ldDomDoc = DOMDocument::loadXML($lddXml);
>
>
>
> // Read the web layout into an XML DOM document object.
>
> $md = "Library://Test/X.MapDefinition";
>
> $mdResourceId = new MgResourceIdentifier($md);
>
> $mdReader = $resourceSrvc->GetResourceContent($mdResourceId);
>
> $mdXml = $mdReader->ToString();
>
> $mdDomDoc = DOMDocument::loadXML($mdXml);
>
>
>
> // Create the MapLayer XML nodeset in the first position
>
> $targetNode = $mdDomDoc->getElementsByTagName("MapLayer")->item(0);
>
> $newNode = $targetNode->parentNode->insertBefore(new
> DOMElement("MapLayer"), $targetNode);
>
> $newNode->appendChild($mdDomDoc->createElement("Name", $dataName));
>
> $newNode->appendChild($mdDomDoc->createElement("ResourceId", $layerDef));
>
> $newNode->appendChild($mdDomDoc->createElement("Selectable", "false"));
>
> $newNode->appendChild($mdDomDoc->createElement("ShowInLegend", "false"));
>
> $newNode->appendChild($mdDomDoc->createElement("LegendLabel"));
>
> $newNode->appendChild($mdDomDoc->createElement("ExpandInLegend",
> "false"));
>
> $newNode->appendChild($mdDomDoc->createElement("Visible", "true"));
>
> $newNode->appendChild($mdDomDoc->createElement("Group"));
>
>
>
> // Prepare the updated XML to be written out to the session.
>
> $updatedXml = $mdDomDoc->saveXML();
>
> $byteSource = new MgByteSource($updatedXml, strlen($updatedXml));
>
>
>
> // Create a web layout in the session to hold the updated version
>
> // from the library.
>
> $sessionMapName = $mdResourceId->GetName();
>
> $sessionMapDefinition =
> "Session:$sessionId//$sessionMapName.MapDefinition";
>
> $sessionResourceId = new MgResourceIdentifier($sessionMapDefinition);
>
>
>
> // Write the updated mapdefinition
>
> $resourceSrvc->SetResource($sessionResourceId,
> $byteSource->GetReader(), null);
>
>
>
> //Test
>
> $md1 = "Session:$sessionId//X.MapDefinition";
>
> $mdResourceId2 = new MgResourceIdentifier($md1);
>
> $mdReader2 = $resourceSrvc->GetResourceContent($mdResourceId2);
>
> $mdXml2 = $mdReader2->ToString();
>
> $mdDomDoc2 = DOMDocument::loadXML($mdXml2);
>
>
>
> // Read the web layout into an XML DOM document object.
>
> $wl = "Library://Test/Y.WebLayout"; // *TODO* Constant!
>
> $wlResourceId = new MgResourceIdentifier($wl);
>
> $wlReader = $resourceSrvc->GetResourceContent($wlResourceId);
>
> $wlXml = $wlReader->ToString();
>
> $wlDomDoc = DOMDocument::loadXML($wlXml);
>
>
>
> // Add the MapDefinition in the WebLayout
>
> $mapdef = $wlDomDoc->getElementsByTagName("ResourceId")->item(0);
>
> $mapdef->nodeValue = "$md1";
>
>
>
> // Prepare the updated XML to be written out to the session.
>
> $wlupdatedXml = $wlDomDoc->saveXML();
>
> $wlbyteSource = new MgByteSource($wlupdatedXml, strlen($wlupdatedXml));
>
>
>
> // Create a web layout in the session to hold the updated version
>
> // from the library.
>
> $wlsessionMapName = $wlResourceId->GetName();
>
> $wlsessionWebLayout = "Session:$sessionId//$wlsessionMapName.WebLayout";
>
> $wlsessionResourceId = new MgResourceIdentifier($wlsessionWebLayout);
>
>
>
> // Write the updated web layout to the session.
>
> $resourceSrvc->SetResource($wlsessionResourceId,
> $wlbyteSource->GetReader(), null);
>
>
>
> // Redirect to the Ajax viewer pointing at the map at the desired
> coordinates.
>
> $redirectTo =
> "mapguide/mapviewerajax/?SESSION=$sessionId&WEBLAYOUT=$wlsessionWebLayout";
>
> $redirectTo =
> "mapguide/mapviewerajax/?WEBLAYOUT=$wlsessionWebLayout&SESSION=$sessionId";
>
> $host = $_SERVER["HTTP_HOST"];
>
> $url = "http://$host/$redirectTo";
>
>
>
>
>
> function BuildLayerDefinitionContent()
>
> {
>
> global $dataSource, $featureName, $ffcolor, $fbcolor,
> $transparent, $linestyle, $thickness, $lcolor, $fillstyle, $foretrans;
>
>
>
> $xtrans = sprintf("%02x", 255 * $foretrans / 100);
>
> $layerTempl =
> file_get_contents("../../viewerfiles/arealayerdef.templ");
>
> $xmlStr = sprintf($layerTempl,
>
> $dataSource,
>
> $featureName,
>
> "GEOM",
>
> $fillstyle,
>
> $xtrans . $ffcolor,
>
> $transparent? "ff" . $fbcolor: "00" . $fbcolor,
>
> $linestyle,
>
> $thickness,
>
> $lcolor);
>
> $src = new MgByteSource($xmlStr, strlen($xmlStr));
>
> return $src->GetReader();
>
>
>
> function GetMapSrs($map)
>
> {
>
> $srs = $map->GetMapSRS();
>
> if($srs != "")
>
> return $srs;
>
>
>
> //No SRS, set to ArbitrayXY meters
>
>
>
> return "LOCALCS[\"Non-Earth (Meter)\",LOCAL_DATUM[\"Local
> Datum\",0],UNIT[\"Meter\", 1],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]";
>
> }
>
>
>
> Regards,
>
>
>
> Arnaud De Groof
>
> ------------------------------------------------------------------------
>
> *From:* mapguide-users-bounces at lists.osgeo.org
> [mailto:mapguide-users-bounces at lists.osgeo.org] *On Behalf Of *Kenneth
> Skovhede, GEOGRAF A/S
> *Sent:* vendredi 9 janvier 2009 9:03
> *To:* MapGuide Users Mail List
> *Subject:* Re: [mapguide-users] Adding a temporary Layer in MapGuide
>
>
>
> You need several steps.
>
> 1. Create a temporary FeatureSource for an SDF
> 2. Insert data into the temporary FeatureSource
> 3. Create a temporary LayerDefinition that points to the temporary
> FeatureSource
> 4. Insert the temporary layer into the runtime map
> 5. Refresh the map
>
> This is a generic recipe for building dynamic layers, redlining,
> buffering, etc.
> There exist code that does this in the "buffer" function used in the
> basic AJAX viewer.
> Look at the "buffer.[php,aspx,jsp]" file in:
> C:\Program
> Files\MapGuideOpenSource2.0\WebServerExtensions\www\mapviewer[php,net,jsp]
>
> If you have trouble with one of the steps, please ask again.
>
> An easier way of dealing with it would be to create a layer that
> displays ALL points.
> Then use the SQL WHERE statement as the layers "filter" property.
> This approach obviously has some limitations.
>
> Regards, Kenneth Skovhede, GEOGRAF A/S
>
>
>
> Carmelo Saffioti skrev:
>
> Hi everybody!
>
> I hope you can help me about this problem I'm having since several days:
>
>
>
> 1) having x,y coordinates of a point, resulting from a sql query, how
> can I display it at runtime on the map?
>
>
>
> 2) having x,y coordinates of a sequence of points, resulting from a
> sql query, how can I display them at runtime on the map?
>
>
>
>
>
>
>
> I really hope you can help me with that
>
> Thank you very much in advance
>
> Carmelo
>
>
>
>
> ------------------------------------------------------------------------
>
>
>
> _______________________________________________
> mapguide-users mailing list
> mapguide-users at lists.osgeo.org <mailto:mapguide-users at lists.osgeo.org>
> http://lists.osgeo.org/mailman/listinfo/mapguide-users
>
>
> *
> ------------------------------------------------------------------------
> E-MAIL DISCLAIMER*
>
> The present message may contain confidential and/or legally privileged
> information. If you are not the intended addressee and in case of a
> transmission error, please notify the sender immediately and destroy
> this E-mail. Disclosure, reproduction or distribution of this document
> and its possible attachments is strictly forbidden.
>
> SPACEBEL denies all liability for incomplete, improper, inaccurate,
> intercepted, (partly) destroyed, lost and/or belated transmission of
> the current information given that unencrypted electronic transmission
> cannot currently be guaranteed to be secure or error free.
> Upon request or in conformity with formal, contractual agreements, an
> originally signed hard copy will be sent to you to confirm the
> information contained in this E-mail.
>
> SPACEBEL denies all liability where E-mail is used for private use.
>
> SPACEBEL cannot be held responsible for possible viruses that might
> corrupt this message and/or your computer system.
>
> ------------------------------------------------------------------------
> ------------------------------------------------------------------------
>
> _______________________________________________
> mapguide-users mailing list
> mapguide-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapguide-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapguide-users/attachments/20090109/5189b552/attachment.html
More information about the mapguide-users
mailing list