[mapguide-users] Re: How to Create a
layerDefinitionprogrammtically , using xml?
Brad L
bradl at mai-eng.com
Sat Sep 29 01:01:42 EDT 2007
This may or may not make a difference. I am mentioning it because it is the
only difference that I can find from the method that works for me. Is there
a reason to use MGLayerBase instead of MGLayer? I think someone pointed out
that it could be an inheritance issue.
Brad Leighninger
Mathews & Associates, Inc.
417-869-6009
Champin wrote:
>
> Brad L,您好!
>
> Thanks for your help,Brad!
>
> Actually, My used the DOM-processing method the create the layer
> definition exactly like the code you provided, they run well and created
> the MgLayer object successfully, but the following steps (step 4 ,as I
> mentioned first mail) ,the map.Save(resourceService) will raise the
> MgNotImplementedException, So I changed to the string-processing way to
> create layerdefinition. Both XML-processing and string-processing can
> create the layerdefiniton and create the MgLayer object successfully ,and
> both of them get the same exception in the following step---map.Save(...).
>
>
>
> ======= 2007-09-29 03:19:42 您在来信中写道:=======
>
>>Try this
>>
>>public MgLayer CreateTrackLineLayer(String rootPath, MgResourceService
>>resService, MgResourceIdentifier TrackResId, String sessionId)
>> {
>> // Load the TrackLinelayerdefinition template into
>> // an ASPX XML object, find the "ResourceId" element, and
>> // modify it's content to reference the feature source.
>>
>> XmlDocument doc = new XmlDocument();
>> doc.PreserveWhitespace = true;
>> doc.Load(rootPath + @"\Template\TrackLineLayerDefinition2.xml");
>> XmlNode featureSourceNode =
>>doc.GetElementsByTagName("ResourceId").Item(0);
>> XmlNode resContent = doc.CreateTextNode(TrackResId.ToString());
>> featureSourceNode.AppendChild(resContent);
>>
>> // Get the updated layer definition from the DOM object
>> // and save it to the repository using the
>> // ResourceService object.
>>
>> MemoryStream xmlStream = new MemoryStream();
>> doc.Save(xmlStream);
>> byte[] layerDefinition = xmlStream.ToArray();
>> Encoding utf8 = Encoding.UTF8;
>> String layerDefStr = new String(utf8.GetChars(layerDefinition));
>> layerDefinition = new byte[layerDefStr.Length - 1];
>> int byteCount = utf8.GetBytes(layerDefStr, 1, layerDefStr.Length -
>>1, layerDefinition, 0);
>> MgByteSource byteSource = new MgByteSource(layerDefinition,
>>layerDefinition.Length);
>> byteSource.SetMimeType(MgMimeType.Xml);
>>
>> //this stores it to the session but you could pass your
>>MgResourceIdentifier as you did in your code
>> MgResourceIdentifier tempLayerResId = new
>>MgResourceIdentifier("Session:" + sessionId +
>>"//TrackLineLayer.LayerDefinition");
>>
>> resService.SetResource(tempLayerResId, byteSource.GetReader(),
>>null);
>>
>> // Create an MgLayer object based on the new layer definition
>> // and return it to the caller.
>>
>> MgLayer newLayer = new MgLayer(tempLayerResId, resService);
>>
>> return newLayer;
>> }
>>
>>I didn't debug this but it should get you in the ball park.
>>
>>Brad Leighninger
>>Mathews & Associates, Inc
>>417-869-6009
>>
>>
>>
>>Champin wrote:
>>>
>>> How to Create a layerDefinition programmtically , using xml?
>>> ---------------------------------------
>>>
>>>
>>> Here is my steps, but something wrong happened,
>>> MgNotImplementedException
>>> raised when added the layer to the map and Map.Save()
>>>
>>>
>>> 1.Using MapGuide Studio, Open an LayerDefinition , File --> Save as xml;
>>> in fact, I want to create temp layer the same as the existing layer
>>> ,so
>>> , the temp layers' layerDefinition should be the same as the existing
>>> layer,except the <ResourceID>.....</ResourceID>
>>>
>>> 2. Change the resourceId to the replace flage ,such as <ResourceID>
>>> %resourceID</ResourceID> and save the xml as
>>> layerDefinitionTemplate.xml
>>>
>>> 3.Create the Layer
>>>
>>> public MgLayerBase CreateTrackLineLayer(String rootPath,
>>> MgResourceService resService, String featureSourceName,
>>> MgResourceIdentifier LayerId)
>>> {
>>> MgLayerBase newLayer = null;
>>>
>>> //字符串方法
>>> string layerDefinition = File.ReadAllText(rootPath +
>>> @"\Template\TrackLineLayerDefinition2.xml");
>>> layerDefinition = layerDefinition.Replace("%resourId",
>>> featureSourceName);
>>>
>>> MgByteReader reader = new MgByteReader(layerDefinition,
>>> "text/xml");
>>> resService.SetResource(LayerId, reader, null);
>>>
>>> newLayer = new MgLayerBase(LayerId, resService);
>>> return newLayer;
>>> }
>>>
>>> these code goes well while debuging
>>>
>>> 4. Added the layer to the map and save .
>>>
>>>
>>> MgLayerBase TrackLineLayer = CreateTrackLineLayer(.......);
>>> // 设置层属性
>>> TrackLineLayer.SetName("TrackLineLayer");
>>> TrackLineLayer.SetDisplayInLegend(true);
>>> TrackLineLayer.SetLegendLabel("航迹");
>>> TrackLineLayer.SetVisible(true);
>>> TrackLineLayer.ForceRefresh();
>>> map.GetLayers().Insert(0, TrackLineLayer);
>>>
>>> // add some feature to the layer feature source
>>> MgPropertyCollection props =BuildeNewPropCollection(.......);
>>> MgFeatureCommandCollection commands = new
>>> MgFeatureCommandCollection();
>>> commands.Add(new MgInsertFeatures(featureName, props));
>>> featureService.UpdateFeatures(TrackLineFeatureSourceID,
>>> commands,
>>> false);
>>>
>>> //Save the map
>>> TrackLineLayer.SetVisible(true);
>>> TrackLineLayer.SetSelectable(true);
>>> TrackLineLayer.ForceRefresh();
>>>
>>> map.Save(resService); //**** ERROR
>>> here raise the Exception " MgNotImplementedException "
>>>
>>>
>>>
>>> Can you tell me what am I doing wrong? I have been blocked here for a
>>> whole day, any suggestion will be very appreciate .
>>> Thank you !
>>>
>>> My envionment :
>>> Windows XP SP2
>>> VS2005 .net 2.0 + MapGuide Open Source 1.2
>>>
>>>
>>>
>>
>>--
>>View this message in context:
http://www.nabble.com/How-to-Create-a-layerDefinition-programmtically-%2C-using-xml--tf4514122s16610.html#a12946832
>>Sent from the MapGuide Users mailing list archive at Nabble.com.
>>
>>_______________________________________________
>>mapguide-users mailing list
>>mapguide-users at lists.osgeo.org
>>http://lists.osgeo.org/mailman/listinfo/mapguide-users
>
> = = = = = = = = = = = = = = = = = = = =
>
>
> 致
> 礼!
>
>
> champingfor
> champingfor at 163.com
> 2007-09-29
> ---------------------------------
> MapGuide Developer Network(China)
> http://www.mgdn.com.cn
>
>
> _______________________________________________
> mapguide-users mailing list
> mapguide-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapguide-users
>
>
--
View this message in context: http://www.nabble.com/How-to-Create-a-layerDefinition-programmtically-%2C-using-xml--tf4514122s16610.html#a12952223
Sent from the MapGuide Users mailing list archive at Nabble.com.
More information about the mapguide-users
mailing list