Silverlight RadMap ShapeFile Reading Uri Problem

4 posts, 1 answers
  1. Martin
    Martin avatar
    3 posts
    Member since:
    Apr 2013

    Posted 23 May 2013 Link to this post

    Hi, I am sorry if I am making this topic at the wrong place or if there is already one I couldn't find.
    Anyway, I am very new to Silverlight and RadMap and I am trying to read a shapefile. The scenario has just an empty RadMap that works fine (I use BingMaps as provider) - "MapAM".
    I want to press a button, which opens a file dialog and chooses the shapefile. When the file is chosen to be displayed on the map.
    I have followed the tutorials on the matter and I came up with the following:

    private void btnOpenShape_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                bool? clickedOk = openFileDialog.ShowDialog();
     
                // Process input if the user clicked OK.
                if (clickedOk == true)
                {
                    string strShpUri = openFileDialog.File.FullName;
                    string strDbfUri = strShpUri.Substring(0, strShpUri.Length - 3) + "dbf"; //because the shp and dbf files have the same name
     
                    Uri shapeUri = new Uri(strShpUri, UriKind.Absolute);
                    Uri dbfUri = new Uri(strDbfUri, UriKind.Absolute);
     
                    MapShapeReader shpReader = new MapShapeReader();
                     
                    shpReader.PreviewReadCompleted += shpReader_PreviewReadCompleted;
                    shpReader.Read(
                                new Uri(strShpUri, UriKind.Absolute),
                                new Uri(strDbfUri, UriKind.Absolute));
     
                    InformationLayer layer = new InformationLayer();
                    layer.ShapeFill = new MapShapeFill();
                    layer.ShapeFill.Fill = new SolidColorBrush(Colors.Green);
                    layer.ShapeFill.Stroke = new SolidColorBrush(Colors.Red);
                    layer.Reader = shpReader;
                    MapAM.Items.Add(layer);
                }
            }
     
            void shpReader_PreviewReadCompleted(object sender, PreviewReadShapesCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                }
            }

    This code results in the following error:
    {System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotSupportedException: The URI prefix is not recognized.
       в System.Net.WebRequest.Create(Uri requestUri)
       в System.Net.WebClient.GetWebRequest(Uri address)
       в System.Net.WebClient.OpenReadAsync(Uri address, Object userToken)
       --- Край на вътрешното проследяване на стека за грешки ---}

    In order to be able to open files, as I understand is not an easy task in Silverlight, I set the Silverlight project to be opened out of browser and checked the 'Require elevated trust ...' . This resolves the exception I get for file permissions. 
    So far so good, but the mentioned exception occurs at the PreviewReadCompleted event. 
    By the way the files I test it with are correct. Meaning that I have two files - "18099.shp" and "18099.dbf", that have the same name but different extension. And I open them through file dialog so, (and i've checked) my uri's are correct.
    I've spent a lot of time reading about this exception in internet and the only solution I seem to find says that to resolve this problem I need to set the start-up project to the hosting application. Which means that the Silverlight project will no longer open under Out of browser and I get stuck with the File permissions exception. 

    I am kind of hoping I am doing something wrong or using a methodology that is incorrect. 
    I am of hope that you could provide sample code or at least a mentioning of what is going wrong :) 
    thank you in advance, 
  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 23 May 2013 Link to this post

    Hello Martin,

    Unfortunately you can't use the MapShapeReader for local files in OOB application, because it uses WebClient for loading files. In this case you should use the ShapeFileReader instead of it. The ShapeFileReader allows to load shape-file using streams.
    The sample code is below.
    private void btnOpenShape_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        bool? clickedOk = openFileDialog.ShowDialog();
     
        // Process input if the user clicked OK.
        if (clickedOk == true)
        {
            string strShpUri = openFileDialog.File.FullName;
            string strDbfUri = strShpUri.Substring(0, strShpUri.Length - 3) + "dbf"; //because the shp and dbf files have the same name
     
            try
            {
                Stream shpStream = File.OpenRead(strShpUri);
                Stream dbfStream = null;
                if (File.Exists(strDbfUri))
                {
                    dbfStream = File.OpenRead(strDbfUri);
                }
     
                InformationLayer layer = new InformationLayer();
                layer.ShapeFill = new MapShapeFill();
                layer.ShapeFill.Fill = new SolidColorBrush(Colors.Green);
                layer.ShapeFill.Stroke = new SolidColorBrush(Colors.Red);
                layer.ShapeFill.StrokeThickness = 1;
     
                List<FrameworkElement> shapes = ShapeFileReader.Read(shpStream, dbfStream);
                foreach (FrameworkElement shape in shapes)
                {
                    layer.Items.Add(shape);
                }
     
                MapAM.Items.Add(layer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    Regards,
    Andrey Murzov
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Martin
    Martin avatar
    3 posts
    Member since:
    Apr 2013

    Posted 27 May 2013 Link to this post

    Thank you Andrey for the fast and accurate response, I am glad :) that I was using the wrong class, and thank you for correcting me. 
    However, I entered your code in the project and it still doesnt work. 
    Now, I just get a white screen when it gets to the line MapAM.Items.Add(layer)...
    If i comment this line I dont get white screen, but I dont get the shapes too ofcourse.
    I kept the try catch block that waits for ANY and ALL exceptions, and I have no Messages or anything ... just white screen. 
    I have noticed that adding layer to the map gives me problems occasionaly. For instance, I had a class that reads some geometry from a database and put it in a layer. 
    I used the following model: 
    (not real code I removed it)

    class geometryLoader()
    {
          public Load(InformationLayer layer)
          {
             ....
               MapPolygon item = myClasThatParsesGeometry();
             layer.add(item);
            ...
         }
    }
     
    //and in mainpage.cs
     
    public MainPage()
            {
                InitializeComponent();
                InformationLayer layer = new InformationLayer();
                geometryLoader.Load(layer);
                MapAM.Items.Add(layer);
             }
    which resulted in the same kind of an error. 
    Again, I guess I am doing something wrong, for instance do I have to initialize the Map.Items collection or something ... 

    anyway, I worked arround that problem, now I dont know how to proceed with the "white screen" error in the shapefile reading.

    Any and all help will be appriciated, again thank you... :)

    I guess I can send you the shp and dbf files I am trying to open ... but I am not allowed anything other than gif, jpg, png ... :)
  5. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 28 May 2013 Link to this post

    Hi Martin,

    It is hard to define a particular cause of this problem using just a code snippet you sent. At least we should inspect the shape-file you use.
    The support ticket allows you to attach zip or rar archives. Could you, please, open the support ticket and attach your shape-file for detail analysis?

    Regards,
    Andrey Murzov
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top