This project is read-only.

Load Map File

Oct 26, 2010 at 1:24 PM

Hi!

When using these lines to show  IT.SHP on map and than save it as MAP file, there is no chance to load this MAP file again!

     FeatureSet fs = new FeatureSet();

            fs.Open(@"C:\mapgis\IT.SHP");
            IFeatureLayer fl = geoMap1.Layers.Add(fs);
            fs.FillAttributes();                                        

            fl.LegendText = "My name";
What is wrong with this code? I tried these few lines in MW6 and DotSpatial.
Thank for any help, Vojko
Oct 26, 2010 at 4:40 PM
The code does not save the symbology aspects stored in the layer, or the layout of the map. For that we have project serialization, which is different code. Your code would allow you to show the raw data from the shapefile, which is an older data standard that has no information on topology much less layer symbology. To handle layer serialization for the map itself, the layers that are on the map, and the symbology for the map you would use the code pasted below. The XmlDeserializer is robust in the sense that it can serialize and deserialize things that are deeper in the framework than the entire map. You could just as easily pass in one layer in order to store symbology characteristics for just one layer in a file along with the original shapefile, for instance. It is advisable that you use a filename with the xml extension, however, since the resulting code will be xml.

Ted


private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show(this, "Are you sure you want to clear all data and open a different map?", "Confirm new map", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
var dlg = new OpenFileDialog();
dlg.Filter = "Map Files|*.map.xml";
if (dlg.ShowDialog(this) != DialogResult.OK) return;
XmlDeserializer d = new XmlDeserializer();
d.Deserialize(map1, File.ReadAllText(dlg.FileName));
map1.Invalidate();
try
{

}
catch (IOException)
{
MessageBox.Show(this, "Could not open the specified map file " + dlg.FileName, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (XmlException)
{
MessageBox.Show(this, "Failed to read the specified map file " + dlg.FileName, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
var dlg = new SaveFileDialog();
dlg.Filter = "Map Files|*.map.xml";
dlg.SupportMultiDottedExtensions = true;
if (dlg.ShowDialog(this) == DialogResult.OK)
{
try
{
XmlSerializer s = new XmlSerializer();
string xml = s.Serialize(map1);
File.WriteAllText(dlg.FileName, xml);
}
catch (XmlException)
{
MessageBox.Show(this, "Failed to write the specified map file " + dlg.FileName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (IOException)
{
MessageBox.Show(this, "Could not write to the specified map file " + dlg.FileName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

Ted




On Tue, Oct 26, 2010 at 5:24 AM, vzavec <notifications@codeplex.com> wrote:

From: vzavec

Hi!

When using these lines to show IT.SHP on map and than save it as MAP file, there is no chance to load this MAP file again!

     FeatureSet fs = new FeatureSet();

            fs.Open(@"C:\mapgis\IT.SHP");
            IFeatureLayer fl = geoMap1.Layers.Add(fs);
            fs.FillAttributes();                                        

            fl.LegendText = "My name";
What is wrong with this code? I tried these few lines in MW6 and DotSpatial.
Thank for any help, Vojko

Read the full discussion online.

To add a post to this discussion, reply to this email (DotSpatial@discussions.codeplex.com)

To start a new discussion for this project, email DotSpatial@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Oct 26, 2010 at 6:41 PM

Hi Ted!

Thanks for help, best regards Vojko

 

Oct 27, 2010 at 8:50 AM

HI ted!

No success !  I already have the same code for openMapMenuItem_Click and also for saveMapMenuItem_Click in frmMapWindow6.cs file as you sent to me. 
I like to send you my XML file which I produced and then tried to open it but do not know how?

Regards, Vojko

Jun 23, 2011 at 5:17 PM

I created a map.xml file from the DotSpatial Application. Now I'm trying to read the same map.xml file from a custom application that uses the map control.

I'm using the code recommended by Ted in October.

I get the following error:

Error loading map: Data at the root level is invalid. Line 1, position 1. ---    at System.Xml.XmlTextReaderImpl.Throw(Exception e)   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()   at System.Xml.XmlTextReaderImpl.Read()   at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)   at System.Xml.Linq.XDocument.Load(TextReader textReader, LoadOptions options)   at System.Xml.Linq.XDocument.Load(TextReader textReader)   at DotSpatial.Serialization.XmlDeserializer.Deserialize[T](T existingObject, String xml)   at WpfApplication1.mapProjectLoader.openMap() in C:\Tools\Applications\TestMapWindow\WpfApplication1\WpfApplication1\mapProjectLoader.cs:line 38

 

Are the map.xml files meant to be loaded into a control?

Jun 23, 2011 at 5:44 PM

Hi!

When using .dspx file extension in latest DSP save and open works OK!