[mapguide-users] How to add layer to the map on the fly???
MichaelGl
michael_gl at stav-gis.com
Tue Nov 24 10:38:29 EST 2009
Actually i have seen some example on PHP,and I wanted to "translate" it to
C#.
So,I don't actually understand the SetLayerDefinition method.
and I don't know how to save "layerdefinition", I didn't found it in MgLayer
object methods.
If I try to change the destination to "Library://Debug/<name>.<type>"
this line :
featureService.CreateFeatureSource(resId, sdfParams);
fails.
So,now I stucked.
Kenneth Skovhede, GEOGRAF A/S wrote:
>
> There is something weird with the variable resId.
>
> You set it in this line:
>
> MgResourceIdentifier resId = new MgResourceIdentifier(featureSourceName);
>
> Then you use it for a layer in this line:
>
> MgLayer newLayer = new MgLayer(resId, resourceService);
>
> newLayer.SetLayerDefinition(resId, resourceService);
>
>
> That does not explain your error message, but it does not make it easier
> to understand
> the actual problem.
> From the errormessage I'm guessing that the layerdefinition points to
> the wrong featuresource.
>
> And you are not saving the layerdefinition?
>
> The easiest way to debug this would be to change the destination from
> the current
> "Session:<session-id>//<name>.<type>" to
> "Library://Debug/<name>.<type>"
>
> When your LayerDefinition and FeatureSource are saved in Library,
> you can open them with Maestro/Studio and examine them for errors.
>
>
> PS.: "ReadFileAsString" is in the framework:
> System.IO.File.ReadAllText
> http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx
>
> Regards, Kenneth Skovhede, GEOGRAF A/S
>
>
>
> MichaelGl skrev:
>> Kenneth Skovhede,hi
>> Yes,it is error in the log file :
>> Error: Resource was not found:
>> Session:c737c35c-ffff-ffff-8000-0025b3b865f4_en_7F0000010AFC0AFB0AFA//TemporaryLines.FeatureSource
>> StackTrace:
>> - MgResourceServiceHandler.ProcessOperation line 80 file
>> d:\buildforgeprojects\mapguide_open_source_v2.0\build_27.16\mgdev\server\src\services\resource\ResourceServiceHandler.cpp
>> - MgOpGetResourceContent.Execute line 120 file
>> d:\buildforgeprojects\mapguide_open_source_v2.0\build_27.16\mgdev\server\src\services\resource\OpGetResourceContent.cpp
>> - MgServerResourceService.GetResourceContent line 863 file
>> d:\buildforgeprojects\mapguide_open_source_v2.0\build_27.16\mgdev\server\src\services\resource\ServerResourceService.cpp
>> - MgApplicationRepositoryManager.GetResourceContent line 378 file
>> d:\buildforgeprojects\mapguide_open_source_v2.0\build_27.16\mgdev\server\src\services\resource\ApplicationRepositoryManager.cpp
>> - MgResourceDefinitionManager.GetResource line 320 file
>> d:\buildforgeprojects\mapguide_open_source_v2.0\build_27.16\mgdev\server\src\services\resource\ResourceDefinitionManager.cpp
>> - MgResourceContentManager.GetDocument line 590 file
>> d:\buildforgeprojects\mapguide_open_source_v2.0\build_27.16\mgdev\server\src\services\resource\ResourceContentManager.cpp
>> - MgResourceDefinitionManager.GetDocument line 476 file
>> d:\buildforgeprojects\mapguide_open_source_v2.0\build_27.16\mgdev\server\src\services\resource\ResourceDefinitionManager.cpp
>> Resource was not found:
>> Session:c737c35c-ffff-ffff-8000-0025b3b865f4_en_7F0000010AFC0AFB0AFA//TemporaryLines.FeatureSource
>>
>> The layer is NOT appear in the legend.
>> I don't really know how to check the signature of the layer and to
>> understand if it looks valid.
>> I'm new user of the MapGuide.
>> So,I'll be really happy if you help me to solve the problem.
>> How can I fix the error from the log file?
>>
>>
>> Kenneth Skovhede, GEOGRAF A/S wrote:
>>
>>> What errors do you see?
>>> Are there errors in the log file?
>>> Does the layer appear in the legend?
>>> Does the signature for the layer look valid?
>>>
>>> Try saving the temp layer and featuresource in Library:// so you can
>>> examine them from Studio/Maestro.
>>>
>>> Regards, Kenneth Skovhede, GEOGRAF A/S
>>>
>>>
>>>
>>> MichaelGl skrev:
>>>
>>>> I'm trying to add temporary layer to the map and I'm fail. Please
>>>> help...
>>>> I have a button :
>>>> <input type="button" value="Start" onclick="DigitizePolygon();">
>>>> This is the client side code :
>>>> <script type="text/javascript">
>>>>
>>>> function submitRequest()
>>>> {
>>>> var pageUrl =
>>>> document.getElementById("hfTargetURL").getAttribute("value");
>>>> var pointsStr =
>>>> document.getElementById("hfPoints").getAttribute("value");
>>>> params = new Array(
>>>> "SESSION",
>>>> parent.parent.mapFrame.GetSessionId(),
>>>> "MAPNAME", parent.parent.mapFrame.GetMapName(),
>>>> "POINTS", pointsStr
>>>> );
>>>>
>>>> parent.parent.formFrame.Submit(pageUrl, params, "scriptFrame");
>>>> }
>>>>
>>>> function DigitizePolygon()
>>>> {
>>>> var lblBox = document.getElementsByName("lblArea");
>>>> lblBox[0].innerText = "";
>>>> parent.parent.mapFrame.DigitizePolygon(OnPolygonDigitized);
>>>> }
>>>>
>>>> function OnPolygonDigitized(poly)
>>>> {
>>>> ShowResults(CalculatePolygonArea(poly));
>>>> }
>>>>
>>>> function CalculatePolygonArea(line)
>>>> {
>>>> var str = "";
>>>> var res = 0;
>>>> var tmpInt = 0;
>>>> var pointsStr = line.Count.toString() + ",";
>>>> for (var i = (line.Count - 1); i > 0; i--)
>>>> {
>>>> var currP = line.Point(i);
>>>> var nextP = line.Point(i - 1);
>>>> res += ((currP.X * nextP.Y) - (nextP.X * currP.Y));
>>>> pointsStr += currP.X.toString() + "," + currP.Y.toString()
>>>> +
>>>> ",";
>>>> if (i == 1)
>>>> {
>>>> pointsStr += nextP.X.toString() + "," +
>>>> nextP.Y.toString();
>>>> }
>>>> }
>>>> res /= 2;
>>>> tmpInt = parseInt(res * 100);
>>>> res = tmpInt / 100;
>>>> res = (res < 0) ? (res * -1) : res;
>>>> str = res.toString();
>>>> document.getElementById("hfPoints").setAttribute("value",
>>>> pointsStr);
>>>>
>>>> return str;
>>>> }
>>>>
>>>> function ShowResults(res)
>>>> {
>>>> var lblBox = document.getElementsByName("lblArea");
>>>> lblBox[0].innerText = res ;
>>>> submitRequest();
>>>> }
>>>>
>>>> </script>
>>>>
>>>> I'm passing all the points of the digitized polygon to the server
>>>> side,and
>>>> this is the code of the server side
>>>> C# :
>>>>
>>>> <script runat="server">
>>>> ArrayList xpts = new ArrayList();
>>>> ArrayList ypts = new ArrayList();
>>>> const string templPath = "c:\\Program
>>>> Files\\MapGuideOpenSource2.0\\WebServerExtensions\\www\\viewerfiles\\";
>>>>
>>>> protected void Page_Load(object sender, EventArgs e)
>>>> {
>>>> double centerX, centerY;
>>>> NameValueCollection requestParams;
>>>>
>>>> try
>>>> {
>>>> // Initialize and get request parameters
>>>> InitializeWebTier();
>>>> requestParams = GetRequestParameters();
>>>> String mgSessionId = requestParams["SESSION"];
>>>> String mgLocale = requestParams["LOCALE"];
>>>> String mgMapName = requestParams["MAPNAME"];
>>>> string pointsStr = requestParams["POINTS"];
>>>> string layerName = "Lines";
>>>> string layerLegendLabel = "New Lines";
>>>> string groupName = "Analysis";
>>>> string groupLegendLabel = "Analysis";
>>>> string featureSourceName = "Session:" + mgSessionId +
>>>> "//TemporaryLines.FeatureSource";
>>>>
>>>> BuildPointsLists(pointsStr);
>>>> // Connect to the site and create a session
>>>> MgUserInformation userInfo = new
>>>> MgUserInformation(mgSessionId);
>>>> MgSiteConnection siteConnection = new MgSiteConnection();
>>>> siteConnection.Open(userInfo);
>>>>
>>>> // Get an instance of the required service.
>>>> MgResourceService resourceService =
>>>> siteConnection.CreateService(MgServiceType.ResourceService) as
>>>> MgResourceService;
>>>> MgFeatureService featureService =
>>>> siteConnection.CreateService(MgServiceType.FeatureService) as
>>>> MgFeatureService;
>>>>
>>>> // Initiate the map
>>>> MgMap map = new MgMap(siteConnection);
>>>> map.Open(mgMapName);
>>>>
>>>> MgResourceIdentifier resId = new
>>>> MgResourceIdentifier(featureSourceName);
>>>> bool featureSourceExists = DoesResourceExist(resId,
>>>> resourceService);
>>>>
>>>> if (featureSourceExists == false)
>>>> {
>>>> // Create a temporary feature source to draw the lines
>>>> on
>>>>
>>>> // Create a feature class definition for the new
>>>> feature
>>>> source
>>>> MgClassDefinition classDefinition = new
>>>> MgClassDefinition();
>>>> classDefinition.SetName("Lines");
>>>> classDefinition.SetDescription("Lines to display.");
>>>> string geometryPropertyName = "SHPGEOM";
>>>>
>>>> classDefinition.SetDefaultGeometryPropertyName(geometryPropertyName);
>>>>
>>>> // Create an identify property
>>>> MgDataPropertyDefinition identityProperty = new
>>>> MgDataPropertyDefinition("KEY");
>>>> identityProperty.SetDataType(MgPropertyType.Int32);
>>>> identityProperty.SetAutoGeneration(true);
>>>> identityProperty.SetReadOnly(true);
>>>>
>>>> // Add the identity property to the class definition
>>>>
>>>> classDefinition.GetIdentityProperties().Add(identityProperty);
>>>> classDefinition.GetProperties().Add(identityProperty);
>>>> // Create a name property
>>>> MgDataPropertyDefinition nameProperty = new
>>>> MgDataPropertyDefinition("NAME");
>>>> nameProperty.SetDataType(MgPropertyType.String);
>>>>
>>>> // Add the name property to the class definition
>>>> classDefinition.GetProperties().Add(nameProperty);
>>>>
>>>> // Create a geometry property
>>>> MgGeometricPropertyDefinition geometryProperty = new
>>>> MgGeometricPropertyDefinition(geometryPropertyName);
>>>>
>>>> geometryProperty.SetGeometryTypes(MgFeatureGeometricType.Surface);
>>>>
>>>> // Add the geometry property to the class definition
>>>> classDefinition.GetProperties().Add(geometryProperty);
>>>>
>>>> // Create a feature schema
>>>> MgFeatureSchema featureSchema = new
>>>> MgFeatureSchema("SHP_Schema", "Line schema");
>>>>
>>>> // Add the feature schema to the class definition
>>>> featureSchema.GetClasses().Add(classDefinition);
>>>>
>>>> // Create the feature source
>>>> string wkt = map.GetMapSRS();
>>>> MgCreateSdfParams sdfParams = new
>>>> MgCreateSdfParams("spatial
>>>> context", wkt, featureSchema);
>>>> featureService.CreateFeatureSource(resId, sdfParams);
>>>> }
>>>> // Add the line to the feature source
>>>> MgBatchPropertyCollection batchPropertyCollection = new
>>>> MgBatchPropertyCollection();
>>>> for (int idx = 0; idx < xpts.Count - 1; idx++)
>>>> {
>>>> string lineName = "Line " + (idx + 1).ToString();
>>>> MgPropertyCollection propertyCollection =
>>>> MakeLine(lineName,
>>>>
>>>> Convert.ToDouble(xpts[idx]),
>>>>
>>>> Convert.ToDouble(ypts[idx]),
>>>>
>>>> Convert.ToDouble(xpts[idx + 1]),
>>>>
>>>> Convert.ToDouble(ypts[idx + 1]));
>>>> batchPropertyCollection.Add(propertyCollection);
>>>> }
>>>> ////////////////////////////////////////////
>>>> // Add the batch property collection to the feature source
>>>> MgInsertFeatures cmd = new MgInsertFeatures(layerName,
>>>> batchPropertyCollection);
>>>> MgFeatureCommandCollection featureCommandCollection = new
>>>> MgFeatureCommandCollection();
>>>> featureCommandCollection.Add(cmd);
>>>>
>>>> // Execute the "add" commands
>>>> featureService.UpdateFeatures(resId,
>>>> featureCommandCollection,
>>>> false);
>>>>
>>>> //---------------------------------------------------//
>>>> bool layerExists = DoesLayerExist(layerName, map);
>>>> if (layerExists == false)
>>>> {
>>>> // Create a new layer which uses that feature source
>>>>
>>>> // Create a line rule to stylize the lines
>>>> string ruleLegendLabel = "Lines Rule";
>>>> string filter = "";
>>>> string color = "FF0000FF";
>>>> string lineRule = ReadFileAsString(templPath +
>>>> "linerule.templ");
>>>> String[] vals = { ruleLegendLabel, filter, color };
>>>> lineRule = Substitute(lineRule, vals);
>>>>
>>>> // Create a line type style
>>>> string lineTypeStyle = ReadFileAsString(templPath +
>>>> "linetypestyle.templ");
>>>> String[] valsStyle = { lineRule };
>>>> lineTypeStyle = Substitute(lineTypeStyle, valsStyle);
>>>>
>>>> // Create a scale range
>>>> string minScale = "0";
>>>> string maxScale = "1000000000000";
>>>> string lineScaleRange = ReadFileAsString(templPath +
>>>> "scalerange.templ");
>>>> String[] valsScale = { minScale, maxScale,
>>>> lineTypeStyle
>>>> };
>>>> lineScaleRange = Substitute(lineScaleRange, valsScale);
>>>>
>>>> // Create the layer definiton
>>>> string featureName = "SHP_Schema:Lines";
>>>> string geometry = "SHPGEOM";
>>>> string layerDefinition = ReadFileAsString(templPath +
>>>> "layerdefinition.templ");
>>>> String[] valsLayerDef = { featureSourceName,
>>>> featureName,
>>>> geometry, lineScaleRange };
>>>> layerDefinition = Substitute(layerDefinition,
>>>> valsLayerDef);
>>>>
>>>> //---------------------------------------------------//
>>>> // Add the layer to the map
>>>> MgLayer newLayer = new MgLayer(resId, resourceService);
>>>> newLayer.SetName(layerName);
>>>> newLayer.SetLegendLabel(layerLegendLabel);
>>>> newLayer.SetLayerDefinition(resId, resourceService);
>>>> ///???????????????
>>>> // Add the layer to a layer group
>>>> MgLayerGroup newGroup = new MgLayerGroup(groupName);
>>>> newGroup.SetLegendLabel(layerLegendLabel);
>>>> map.GetLayerGroups().Add(newGroup);
>>>> map.GetLayers().Insert(0, newLayer);
>>>> }
>>>> // --------------------------------------------------//
>>>> // Turn on the visibility of this layer.
>>>> // (If the layer does not already exist in the map, it will
>>>> be
>>>> visible by default when it is added.
>>>> // But if the user has already run this script, he or she
>>>> may
>>>> have set the layer to be invisible.)
>>>> MgLayerCollection layerCollection = map.GetLayers();
>>>> if (layerCollection.Contains(layerName))
>>>> {
>>>> MgLayerBase linesLayer =
>>>> layerCollection.GetItem(layerName);
>>>> linesLayer.SetVisible(true);
>>>> }
>>>> MgLayerGroupCollection groupCollection =
>>>> map.GetLayerGroups();
>>>> if (groupCollection.Contains(groupName))
>>>> {
>>>> MgLayerGroup analysisGroup =
>>>> groupCollection.GetItem(groupName);
>>>> analysisGroup.SetVisible(true);
>>>> }
>>>> //---------------------------------------------------//
>>>> // Save the map back to the session repository
>>>> string sessionIdName = "Session:" + mgSessionId + "//" +
>>>> mgMapName + ".Map";
>>>> MgResourceIdentifier sessionResourceID = new
>>>> MgResourceIdentifier(sessionIdName);
>>>> sessionResourceID.Validate();
>>>> map.Save(resourceService, sessionResourceID);
>>>> }
>>>> catch (Exception ex)
>>>> {
>>>> Response.Write(ex.Message);
>>>> }
>>>> }
>>>>
>>>> void BuildPointsLists(string p_pointsStr)
>>>> {
>>>> string[] parameters = p_pointsStr.Split(',');
>>>> int length = Convert.ToInt32(parameters[0]);
>>>>
>>>> for (int idx = 1; idx <= (length * 2); idx+=2)
>>>> {
>>>> xpts.Add(Convert.ToDouble(parameters[idx]));
>>>> ypts.Add(Convert.ToDouble(parameters[idx + 1]));
>>>> }
>>>> }
>>>>
>>>> ///////////////////////////////////////////////////////////////////////////////////
>>>> bool DoesResourceExist(MgResourceIdentifier p_redId,
>>>> MgResourceService
>>>> p_resSrvc)
>>>> // Returns true if the resource already exists, or false otherwise
>>>> {
>>>> try
>>>> {
>>>> p_resSrvc.GetResourceContent(p_redId);
>>>> }
>>>> catch (MgResourceNotFoundException e)
>>>> {
>>>> return false;
>>>> }
>>>>
>>>> return true;
>>>> }
>>>>
>>>>
>>>> ///////////////////////////////////////////////////////////////////////////////////
>>>> MgPropertyCollection MakeLine(string p_name, double p_x0, double
>>>> p_y0
>>>> ,
>>>> double p_x1, double p_y1)
>>>> {
>>>> MgPropertyCollection propertyCollection = new
>>>> MgPropertyCollection();
>>>> MgStringProperty nameProperty = new MgStringProperty("NAME",
>>>> p_name);
>>>> propertyCollection.Add(nameProperty);
>>>>
>>>> MgWktReaderWriter wktReaderWriter = new MgWktReaderWriter();
>>>> MgAgfReaderWriter agfReaderWriter = new MgAgfReaderWriter();
>>>>
>>>> string linestring = "LINESTRING XY (" + p_x0.ToString() + " " +
>>>> p_y0.ToString() + ", " +
>>>> p_x1.ToString() + " " + p_y1.ToString() + ")";
>>>> MgGeometry geometry = wktReaderWriter.Read(linestring) as
>>>> MgLineString;
>>>> MgByteReader geometryByteReader =
>>>> agfReaderWriter.Write(geometry);
>>>> MgGeometryProperty geometryProperty = new
>>>> MgGeometryProperty("SHPGEOM", geometryByteReader);
>>>> propertyCollection.Add(geometryProperty);
>>>>
>>>> return propertyCollection;
>>>> }
>>>>
>>>> ///////////////////////////////////////////////////////////////////////////////////
>>>> bool DoesLayerExist(string p_layerName, MgMap p_map)
>>>> // Returns true if the layer already exists, or false otherwise
>>>> {
>>>> MgLayerCollection layerCollection = p_map.GetLayers();
>>>> return (layerCollection.Contains(p_layerName) ? true : false);
>>>> }
>>>>
>>>> ///////////////////////////////////////////////////////////////////////////////////
>>>> string ReadFileAsString(string p_fName)
>>>> {
>>>> string sContents = string.Empty;
>>>> System.IO.StreamReader sr = new
>>>> System.IO.StreamReader(p_fName);
>>>> sContents = sr.ReadToEnd();
>>>> sr.Close();
>>>>
>>>> return sContents;
>>>> }
>>>>
>>>> ///////////////////////////////////////////////////////////////////////////////////
>>>> String Substitute(String templ, String[] vals)
>>>> {
>>>> StringBuilder res = new StringBuilder();
>>>> int index = 0, val = 0;
>>>> bool found;
>>>> do
>>>> {
>>>> found = false;
>>>> int i = templ.IndexOf('%', index);
>>>> if (i != -1)
>>>> {
>>>> found = true;
>>>> res.Append(templ.Substring(index, i - index));
>>>> if (i < templ.Length - 1)
>>>> {
>>>> if (templ[i + 1] == '%')
>>>> res.Append('%');
>>>> else if (templ[i + 1] == 's')
>>>> res.Append(vals[val++]);
>>>> else
>>>> res.Append('@'); //add a character illegal
>>>> in
>>>> jscript so we know the template was incorrect
>>>> index = i + 2;
>>>> }
>>>> }
>>>> } while (found);
>>>> res.Append(templ.Substring(index));
>>>> return res.ToString();
>>>> }
>>>> </script>
>>>>
>>>>
>>>> <script type="text/javascript">
>>>> function OnPageLoad()
>>>> {
>>>> parent.mapFrame.Refresh();
>>>> }
>>>> </script>
>>>>
>>>> The problem is that the polygon is not appear on the map. What am I
>>>> missing???
>>>> Thanks a lot.
>>>>
>>>>
>>> _______________________________________________
>>> mapguide-users mailing list
>>> mapguide-users at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/mapguide-users
>>>
>>>
>>>
>>
>>
>
> _______________________________________________
> mapguide-users mailing list
> mapguide-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapguide-users
>
>
--
View this message in context: http://n2.nabble.com/How-to-add-layer-to-the-map-on-the-fly-tp4031367p4058591.html
Sent from the MapGuide Users mailing list archive at Nabble.com.
More information about the mapguide-users
mailing list