[mapguide-users] How to Create a layerDefinition
programmtically , using xml?
Brad L
bradl at mai-eng.com
Fri Sep 28 15:19:35 EDT 2007
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.
More information about the mapguide-users
mailing list