Save/Load shapefiles to a database

3 posts, 0 answers
  1. Ian
    Ian avatar
    23 posts
    Member since:
    Jun 2011

    Posted 19 Nov 2013 Link to this post

    I am trying to add the ability to load in any shape file, save it to a sql server and then retrieve it and load it back into the map.

    Here is my relevant code

    //get the shape file data
           _shapefileDataStream = dialog.File.OpenRead();
     
                StreamReader sreader = new StreamReader(_shapefileDataStream);
                _shapefileData += sreader.ReadToEnd();
                sreader.BaseStream.Position = 0;
     
    //get the dbf filedata
                _shapefiledbfDataStream = dialog2.File.OpenRead();
     
                StreamReader sreader = new StreamReader(_shapefiledbfDataStream);
                _shapefiledbfData += sreader.ReadToEnd();
                sreader.BaseStream.Position = 0;
     
            vm.SaveShapefile(_shapefileData,_shapefiledbfData, name, isChecked);

    In the database they are stored as nvarchar(max) values for both shapefile and dbffile streams

    To retrieve the data i do 

    Stream ShapefileStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(m.ShapefileStream),0,shapelength);
    ShapefileStream.Position = 0;
     
    Stream ShapefileDataStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(m.ShapefileDataStream),0,dbflength);
    ShapefileDataStream.Position = 0;
     
    if ((ShapefileStream != null) && (ShapefileDataStream != null))
    {
        List<FrameworkElement> mlist = ShapeFileReader.Read(ShapefileStream, ShapefileDataStream,System.Text.Encoding.UTF8);
     
        mlist.ForEach(element => element.Tag = m);
     
        _events.GetEvent<DisplayShapefileTemplateDataEvent>().Publish(mlist);
    }

    and the error i get

    Unable to read beyond the end of the stream.

       at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
       at System.IO.BinaryReader.ReadUInt32()
       at Telerik.Windows.Controls.Map.DbfHeader..ctor(Stream stream, Encoding encoding)
       at Telerik.Windows.Controls.Map.DbfHeader.Create(Stream stream, Encoding encoding)
       at Telerik.Windows.Controls.Map.DbfReader..ctor(Stream stream, ExtendedPropertySet propertySet, Encoding encoding)
       at Telerik.Windows.Controls.Map.ShapeFileReader.Read(ShapeFileReaderParameters parameters)
       at Telerik.Windows.Controls.Map.ShapeFileReader.Read(Stream shapeStream, Stream dbfStream, ExtendedPropertySet extendedPropertySet, Encoding encoding, ICoordinateConverter coordinateConverter)
       at Telerik.Windows.Controls.Map.ShapeFileReader.Read(Stream shapeStream, Stream dbfStream, ExtendedPropertySet extendedPropertySet, Encoding encoding)
       at Telerik.Windows.Controls.Map.ShapeFileReader.Read(Stream shapeStream, Stream dbfStream, Encoding encoding)
       at WIMMS.UI.Module.Main.SavedSearchView.DisplayShapefile_Click(Object sender, RoutedEventArgs e)



  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 20 Nov 2013 Link to this post

    Hi Ian,

    The .shp and .dbf files aren't text files, they are in a binary format. The StreamReader.ReadToEnd()  method reads data as string so you can't use it to read an ESRI shape file and DBF files. Binary files which you read this way will be corrupted. You must read these files as binary data (using BinaryReader, for example).

    You also can't use nvarchar column type to store binary data (either ESRI shape or DBF) directly. There are a couple approaches you can try:
    1. Store binary data in the VARBINARY column.
    2. Convert binary data to the Base64 string (using Convert.ToBase64String() method) and save it in the NTEXT column.

    Please consider the above notes, give my suggestions a try and let us know if they help.

    Regards,
    Andrey Murzov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.

    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.

    Sign up for Free application insights >>
  3. DevCraft banner
  4. Ian
    Ian avatar
    23 posts
    Member since:
    Jun 2011

    Posted 21 Nov 2013 Link to this post

    Thanks option 2 worked great
Back to Top