This is a migrated thread and some comments may be shown as answers.

Save/Load shapefiles to a database

2 Answers 17 Views
Map
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 20 Nov 2013, 05:43 AM
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 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 20 Nov 2013, 09:05 AM
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 >>
0
Ian
Top achievements
Rank 1
answered on 22 Nov 2013, 05:04 AM
Thanks option 2 worked great
Tags
Map
Asked by
Ian
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Ian
Top achievements
Rank 1
Share this question
or