<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
First, you need to understand that resources are just xml documents.<br>
Then you need to understand the relationship between resources.<br>
Finally you need to know the difference between a runtime map and a
mapdefinition.<br>
<br>
A WebLayout (or Fusion) points to a MapDefinition.<br>
A MapDefinition points to a number of LayerDefinitions.<br>
A LayerDefinition points to a FeatureSource.<br>
<br>
So, to create and use layer, you need to do this:<br>
1) Create or re-use a FeatureSource<br>
2) Setup a LayerDefinition<br>
3) Point the LayerDefinitoin to the FeatureSource<br>
4) Save the LayerDefinition<br>
5) Attach the layer to the MapDefinition or RuntimeMap<br>
<br>
When you start the viewer, it takes the MapDefinition xml and produce a
binary copy,<br>
called a Runtime Map (the class name is MgMap).<br>
<br>
If you consider the runtime map a copy of the MapDefinition, you can see<br>
that just as you need to have a FeatureSource and LayerDefinition in
Studio/Maestro,<br>
you also need it runtime.<br>
You need to perform the steps 1-5 as stated above to get this to work.<br>
<br>
You can use the method SetResourceContent method to save Xml.<br>
<br>
The naming of resources require that you know the type of resource,<br>
that you are saving. <br>
In my previous response text inside angle brackets (&lt; &gt;) must be
replaced.<br>
<br>
<pre class="moz-signature" cols="72">Regards, Kenneth Skovhede, GEOGRAF A/S
</pre>
<br>
<br>
MichaelGl skrev:
<blockquote cite="mid:1259077109734-4058591.post@n2.nabble.com"
 type="cite">
  <pre wrap="">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/&lt;name&gt;.&lt;type&gt;"
this line :
                 featureService.CreateFeatureSource(resId, sdfParams);
fails.
So,now I stucked.


Kenneth Skovhede, GEOGRAF A/S wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">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:&lt;session-id&gt;//&lt;name&gt;.&lt;type&gt;" to
"Library://Debug/&lt;name&gt;.&lt;type&gt;"

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
<a class="moz-txt-link-freetext" href="http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx">http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx</a>

Regards, Kenneth Skovhede, GEOGRAF A/S



MichaelGl skrev:
    </pre>
    <blockquote type="cite">
      <pre wrap="">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:
  
      </pre>
      <blockquote type="cite">
        <pre wrap="">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:
    
        </pre>
        <blockquote type="cite">
          <pre wrap="">I'm trying to add temporary layer to the map and I'm fail. Please
help...
I have a button :
&lt;input type="button" value="Start" onclick="DigitizePolygon();"&gt;
This is the client side code :
&lt;script type="text/javascript"&gt;

    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 &gt; 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 &lt; 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();
    }    
    
&lt;/script&gt;

I'm passing all the points of the digitized polygon to the server
side,and
this is the code of the server side 
C# :

&lt;script runat="server"&gt;
    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 &lt; 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 &lt;= (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 &lt; 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();
    }     
&lt;/script&gt;


&lt;script type="text/javascript"&gt;
    function OnPageLoad()
    {        
        parent.mapFrame.Refresh();
    }
&lt;/script&gt;

The problem is that the polygon is not appear on the map. What am I
missing???
Thanks a lot.
  
      
          </pre>
        </blockquote>
        <pre wrap="">_______________________________________________
mapguide-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:mapguide-users@lists.osgeo.org">mapguide-users@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/mapguide-users">http://lists.osgeo.org/mailman/listinfo/mapguide-users</a>


    
        </pre>
      </blockquote>
      <pre wrap="">  
      </pre>
    </blockquote>
    <pre wrap="">_______________________________________________
mapguide-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:mapguide-users@lists.osgeo.org">mapguide-users@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/mapguide-users">http://lists.osgeo.org/mailman/listinfo/mapguide-users</a>


    </pre>
  </blockquote>
  <pre wrap=""><!---->
  </pre>
</blockquote>
</body>
</html>