[mapguide-users] programmatically add layer to initial map

Kenneth, GEOGRAF A/S ks at geograf.dk
Thu Aug 16 07:21:57 EDT 2007


You should insert the "UTF-8" header into the mapDoc.
On top of my head it is something like:
XmlNode decl = mapDoc.CreateXmlDefinition("1.0", "UTF-8");
mapDoc.Insert(0, decl);
You might have to remove an existing xmldefinition.

After that, you can get the byte stream, and it will be UTF-8 encoded:
MemoryStream xmlStream = new MemoryStream();
mapDoc.Save(xmlStream);

If your stream starts with:
byte[] { 0xEF 0xBB 0xBF };
you must remove those three bytes.

You can then get the byte array by:
byte[] utf8EncodedXml = xmlStream.ToArray();

And finally:
MgByteSource byteSource = new MgByteSource(utf8EncodedXml , 
utf8EncodedXml.Length);
byteSource.SetMimeType(MgMimeType.Xml);


Regards, Kenneth, GEOGRAF A/S



Carl skrev:
> Following the logic below to create a weblayout and mapdefinition by
> modifying the xml and using SetResource to save the updated xml into the
> session repository, I am having some problems. 
>
> Initialize web tier
> Create a session, and open connection
> Use GetResource to retrieve the Xml for the WebLayout AND MapDefinition
> Modify the MapDefinition Xml to your liking (add layers etc.)
> Save the MapDefinition to the session repository, using SetResource.
> Update the WebLayout's map resourceid to point towards your modified 
> WebLayout.
> Save the WebLayout to the session repositiory, using SetResource
> Start the viewer, pointing at the newly created WebLayout.
>
> I am getting an error message 'The map definition is invalid.' when the
> viewer loads.
>
> The following code works no problem and the viewer displays the weblayout
> and map from the session repository.
>
> //Code above for modifying xml for web layout changing map resource and
> persisting to session
>
> MgResourceIdentifier mapResourceID = new
> MgResourceIdentifier("Library://Demo/Maps/Demo.MapDefinition");
> MgByteReader byteRdr2 = resService.GetResourceContent(mapResourceID);
> String sessMapName = mapResourceID.GetName();
> MgResourceIdentifier SessMapResId = new MgResourceIdentifier("Session:" +
> sessionId + "//" + sessMapName + ".MapDefinition");
> resService.SetResource(SessMapResId, byteRdr2, null);
>
> //Loads into viewer no problem
>
> But as soon as I try to load the map definition xml using SetResource I get
> the invalid XML error
>
> //Code above for modifying xml for web layout changing map resource and
> persisting to session
>
> MgResourceIdentifier mapResourceID = new
> MgResourceIdentifier("Library://Demo/Maps/Demo.MapDefinition");
> MgByteReader byteRdr2 = resService.GetResourceContent(mapResourceID);
> String sessMapName = mapResourceID.GetName();
>
> String mapResStr = byteRdr2.ToString();
>
> //Load the web layout into an xml document
> XmlDocument mapDoc = new XmlDocument();
> mapDoc.LoadXml(mapResStr);
> XmlElement mapRoot = mapDoc.DocumentElement;
>
> MemoryStream xmlStream = new MemoryStream();
> mapDoc.Save(xmlStream);
> byte[] mapDefinition = xmlStream.ToArray();
> Encoding utf8 = Encoding.UTF8;
> String mapDefStr = new String(utf8.GetChars(mapDefinition));
> mapDefinition = new byte[mapDefStr.Length - 1];
> int byteCount = utf8.GetBytes(mapDefStr, 1, mapDefStr.Length - 1,
> mapDefinition, 0);
> MgByteSource byteSource = new MgByteSource(mapDefinition,
> mapDefinition.Length);
> byteSource.SetMimeType(MgMimeType.Xml);
>
> //Load the modified map into the session repository
> MgResourceIdentifier SessMapResId = new MgResourceIdentifier("Session:" +
> sessionId + "//" + sessMapName + ".MapDefinition");
> resService.SetResource(SessMapResId, byteSource.GetReader(), null);
>
> //This causes an invalid map error when the viewer loads
>
>
> Any pointers on why manipulating the map definition is causing problems
> would be great. Using the same techniques I am having no problems
> manipulating the webDefintion or layerDefinition
>
> Thanks  
>
> Carl Grice
> Keynetix Ltd
> Tel. +44 (0) 1527 68888
> Fax. +44 (0) 1527 62880
> Web Site: http://www.keynetix.com
>
> IMPORTANT: Please address all support & sales correspondence
> keynetix at key-systems.com.
> Addressing support questions to individuals may delay the response.
> -----Original Message-----
> From: mapguide-users-bounces at lists.osgeo.org
> [mailto:mapguide-users-bounces at lists.osgeo.org] On Behalf Of Kenneth,
> GEOGRAF A/S
> Sent: 13 August 2007 15:16
> To: MapGuide Users Mail List
> Subject: Re: [mapguide-users] programmatically add layer to initial map
>
> When the viewer loads, it creates a map from the weblayout/mapdefinition 
> passed.
> Your runtime map gets overwritten when the viewer starts.
> Instead you can do this:
>
> Initialize web tier
> Create a session, and open connection
> Use GetResource to retrieve the Xml for the WebLayout AND MapDefinition
> Modify the MapDefinition Xml to your liking (add layers etc.)
> Save the MapDefinition to the session repository, using SetResource.
> Update the WebLayout's map resourceid to point towards your modified 
> WebLayout.
> Save the WebLayout to the session repositiory, using SetResource
> Start the viewer, pointing at the newly created WebLayout.
>
> You may of course use the "Library://" repository, if you want the 
> changes to persist.
>
> Regards, Kenneth, GEOGRAF A/S
>
>
>
> Scott, Brian skrev:
>   
>> Hi Carl - 
>>
>> What db are you using?  I found, for example, that I could not filter at
>>     
> run time against Postgres/PostGIS.  I was able to create a whole new layer
> based on points available in PG, but I  had to extact the attributes, create
> points, then apply them to a new layer.  Very cumbersome, but for whatever
> reason, filtering just wouldn't work w/PostGIS.  
>   
>> Good luck!
>> brian
>>
>> -----Original Message-----
>> From: mapguide-users-bounces at lists.osgeo.org
>> [mailto:mapguide-users-bounces at lists.osgeo.org]On Behalf Of Carl Grice
>> Sent: Monday, August 13, 2007 10:01 AM
>> To: mapguide-users at lists.osgeo.org
>> Subject: [mapguide-users] programmatically add layer to initial map
>>
>>
>>
>> Hi,
>>
>> I am trying to programmatically filter and add a layer to a map that
>>     
> exists
>   
>> in the repository before the map is initially loaded in the clients
>>     
> viewer.
>   
>> The steps i am performing are as follows;
>>
>> 1)Initialize the web teir and open site
>> 2)Create a new session and open a site connection
>> 3)Use map.create() to create a map from an existing map definition in the
>> repository
>> 4)Create a session resource identifier and use map.Save() to save the
>> existing map into the session repository
>> 5)Use map.Open() to open the map resource just created in the session
>> repository
>> 6)Use map.Getlayers() and insert() to add a new layer into the map
>> 7)use map.save() to save the map with the added layer into the session
>> repository
>>
>> When i debug my code i get no errors and the map displays in the viewer
>>     
> but
>   
>> the added layer does not (even after performing a map refresh).
>>
>> Please can you point me in the direction of a sample that performs a
>>     
> similar
>   
>> task or provide a code example? (I have found similar problems disscussed
>> but cannot find a solution)
>>
>> Thanks in advance.
>>   
>>     
> _______________________________________________
> 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
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapguide-users/attachments/20070816/15b81ca9/attachment.html


More information about the mapguide-users mailing list