예제 #1
0
        public static void Write(string indexFileName, List<ShpIndex> indexes, bool overwrite = false)
        {
            System.IO.MemoryStream writer = new System.IO.MemoryStream();

            for (int i = 0; i < indexes.Count; i++)
            {
                writer.Write(System.BitConverter.GetBytes(indexes[i].RecordNumber), 0, ShapeConstants.IntegerSize);

                writer.Write(System.BitConverter.GetBytes(indexes[i].MinimumBoundingBox.XMin), 0, ShapeConstants.DoubleSize);

                writer.Write(System.BitConverter.GetBytes(indexes[i].MinimumBoundingBox.YMin), 0, ShapeConstants.DoubleSize);

                writer.Write(System.BitConverter.GetBytes(indexes[i].MinimumBoundingBox.XMax), 0, ShapeConstants.DoubleSize);

                writer.Write(System.BitConverter.GetBytes(indexes[i].MinimumBoundingBox.YMax), 0, ShapeConstants.DoubleSize);
            }

            var mode = Shapefile.GetMode(indexFileName, overwrite);

            System.IO.FileStream stream = new System.IO.FileStream(indexFileName, mode);

            writer.WriteTo(stream);

            writer.Close();

            stream.Close();
        }
예제 #2
0
        public static void Write(string fileName, IShapeCollection shapes, ShapeType shapeType, bool overwrite = false)
        {
            System.IO.MemoryStream shxWriter = new System.IO.MemoryStream();

            int offset = 50;

            foreach (IShape item in shapes)
            {
                shxWriter.Write(WriteHeaderToByte(offset, item.ContentLength), 0, 2 * ShapeConstants.IntegerSize);

                offset += item.ContentLength + 4;
            }

            System.IO.MemoryStream writer = new System.IO.MemoryStream();

            int fileLength = (int)shxWriter.Length / 2 + 50;

            writer.Write(WriteMainHeader(shapes, fileLength, shapeType), 0, 100);

            writer.Write(shxWriter.ToArray(), 0, (int)shxWriter.Length);

            //var mode = overwrite ? System.IO.FileMode.Create : System.IO.FileMode.CreateNew;
            var mode = Shapefile.GetMode(fileName, overwrite);

            System.IO.FileStream stream = new System.IO.FileStream(fileName, mode);

            writer.WriteTo(stream);

            shxWriter.Close();

            writer.Close();

            stream.Close();
        }
예제 #3
0
        public static void Write(string shpFileName, IShapeCollection shapes, bool createDbf = false, bool overwrite = false)
        {
            if (shapes == null || shapes.Count < 1)
            {
                return;
            }

            var directory = System.IO.Path.GetDirectoryName(shpFileName);

            if (!System.IO.Directory.Exists(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }

            ShapeType shapeType = shapes.First().Type;

            using (System.IO.MemoryStream featureWriter = new System.IO.MemoryStream())
            {
                int recordNumber = 0;

                foreach (IShape item in shapes)
                {
                    featureWriter.Write(WriteHeaderToByte(++recordNumber, item), 0, 2 * ShapeConstants.IntegerSize);

                    featureWriter.Write(item.WriteContentsToByte(), 0, 2 * item.ContentLength);
                }

                using (System.IO.MemoryStream shpWriter = new System.IO.MemoryStream())
                {
                    int fileLength = (int)featureWriter.Length / 2 + 50;

                    shpWriter.Write(WriteMainHeader(shapes, fileLength, shapeType), 0, 100);

                    shpWriter.Write(featureWriter.ToArray(), 0, (int)featureWriter.Length);

                    //var mode = overwrite ? System.IO.FileMode.Create : System.IO.FileMode.CreateNew;
                    var mode = Shapefile.GetMode(shpFileName, overwrite);

                    System.IO.FileStream stream = new System.IO.FileStream(shpFileName, mode);

                    shpWriter.WriteTo(stream);

                    stream.Close();

                    shpWriter.Close();

                    featureWriter.Close();
                }
            }

            ShxWriter.Write(Shapefile.GetShxFileName(shpFileName), shapes, shapeType, overwrite);

            if (createDbf)
            {
                Dbf.DbfFile.Write(Shapefile.GetDbfFileName(shpFileName), shapes.Count, overwrite);
            }
        }
예제 #4
0
        //Write
        public static void Write(string fileName, System.Data.DataTable table, Encoding encoding, bool overwrite = false)
        {
            var mode = Shapefile.GetMode(fileName, overwrite);

            System.IO.Stream stream = new System.IO.FileStream(fileName, mode);

            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

            List <DbfFieldDescriptor> columns = MakeDbfFields(table.Columns);

            DbfHeader header = new DbfHeader(table.Rows.Count, table.Columns.Count, GetRecordLength(columns), encoding);

            writer.Write(IRI.Msh.Common.Helpers.StreamHelper.StructureToByteArray(header));

            foreach (var item in columns)
            {
                writer.Write(IRI.Msh.Common.Helpers.StreamHelper.StructureToByteArray(item));
            }

            //Terminator
            writer.Write(byte.Parse("0D", System.Globalization.NumberStyles.HexNumber));

            for (int i = 0; i < table.Rows.Count; i++)
            {
                // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                writer.Write(byte.Parse("20", System.Globalization.NumberStyles.HexNumber));

                for (int j = 0; j < table.Columns.Count; j++)
                {
                    // 1400.02.03-comment
                    //byte[] temp = new byte[columns[j].Length];

                    string value = table.Rows[i][j].ToString().Trim();

                    ////encoding.GetBytes(value, 0, value.Length, temp, 0);
                    ////writer.Write(temp);

                    // 1400.02.03-comment
                    //writer.Write(GetBytes(value, temp, encoding));

                    writer.Write(DbfFieldMappings.Encode(value, columns[j].Length, encoding));
                }
            }

            //End of file
            writer.Write(byte.Parse("1A", System.Globalization.NumberStyles.HexNumber));

            writer.Close();

            stream.Close();
        }
예제 #5
0
        public static void Write <T>(string dbfFileName,
                                     IEnumerable <T> values,
                                     List <ObjectToDbfTypeMap <T> > mapping,
                                     Encoding encoding,
                                     bool overwrite = false)
        {
            var columns = mapping.Select(m => m.FieldType).ToList();

            int control = 0;

            try
            {
                //if (columns.Count != mapping.Count)
                //{
                //    throw new NotImplementedException();
                //}

                var mode = Shapefile.GetMode(dbfFileName, overwrite);

                System.IO.Stream stream = new System.IO.FileStream(dbfFileName, mode);

                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

                DbfHeader header = new DbfHeader(values.Count(), mapping.Count, GetRecordLength(columns), encoding);

                writer.Write(IRI.Msh.Common.Helpers.StreamHelper.StructureToByteArray(header));

                foreach (var item in columns)
                {
                    writer.Write(IRI.Msh.Common.Helpers.StreamHelper.StructureToByteArray(item));
                }

                //Terminator
                writer.Write(byte.Parse("0D", System.Globalization.NumberStyles.HexNumber));

                for (int i = 0; i < values.Count(); i++)
                {
                    control = i;
                    // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                    writer.Write(byte.Parse("20", System.Globalization.NumberStyles.HexNumber));

                    for (int j = 0; j < mapping.Count; j++)
                    {
                        // 1400.02.03-comment
                        //byte[] temp = new byte[columns[j].Length];

                        object value = mapping[j].MapFunction(values.ElementAt(i));

                        var temp = DbfFieldMappings.Encode(value, columns[j].Length, encoding);

                        // 1400.02.03-comment
                        //if (value is DateTime dt)
                        //{
                        //    value = dt.ToString("yyyyMMdd");
                        //}

                        //if (value != null)
                        //{
                        //    //encoding.GetBytes(value.ToString(), 0, value.ToString().Length, temp, 0);
                        //    temp = GetBytes(value.ToString(), temp, encoding);
                        //}

                        ////string tt = encoding.GetString(temp);
                        ////var le = tt.Length;
                        writer.Write(temp);
                    }
                }

                //End of file
                writer.Write(byte.Parse("1A", System.Globalization.NumberStyles.HexNumber));

                writer.Close();

                stream.Close();

                System.IO.File.WriteAllText(Shapefile.GetCpgFileName(dbfFileName), encoding.BodyName);
            }
            catch (Exception ex)
            {
                string message = ex.Message;

                string m2 = message + " " + control.ToString();
            }
        }
예제 #6
0
        internal static void Write <T>(string fileName,
                                       List <T> values,
                                       List <Func <T, object> > mapping,
                                       List <DbfFieldDescriptor> columns,
                                       Encoding encoding,
                                       bool overwrite = false)
        {
            int control = 0;

            try
            {
                if (columns.Count != mapping.Count)
                {
                    throw new NotImplementedException();
                }

                //var mode = overwrite ? System.IO.FileMode.Create : System.IO.FileMode.CreateNew;
                var mode = Shapefile.GetMode(fileName, overwrite);

                System.IO.Stream stream = new System.IO.FileStream(fileName, mode);

                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

                DbfHeader header = new DbfHeader(values.Count, mapping.Count, GetRecordLength(columns), encoding);

                writer.Write(IRI.Ket.Common.Helpers.StreamHelper.StructureToByteArray(header));

                foreach (var item in columns)
                {
                    writer.Write(IRI.Ket.Common.Helpers.StreamHelper.StructureToByteArray(item));
                }

                //Terminator
                writer.Write(byte.Parse("0D", System.Globalization.NumberStyles.HexNumber));

                for (int i = 0; i < values.Count; i++)
                {
                    control = i;
                    // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                    writer.Write(byte.Parse("20", System.Globalization.NumberStyles.HexNumber));

                    for (int j = 0; j < mapping.Count; j++)
                    {
                        byte[] temp = new byte[columns[j].Length];

                        object value = mapping[j](values[i]);

                        if (value != null)
                        {
                            //encoding.GetBytes(value.ToString(), 0, value.ToString().Length, temp, 0);
                            temp = GetBytes(value.ToString(), temp, encoding);
                        }

                        //string tt = encoding.GetString(temp);
                        //var le = tt.Length;
                        writer.Write(temp);
                    }
                }

                //End of file
                writer.Write(byte.Parse("1A", System.Globalization.NumberStyles.HexNumber));

                writer.Close();

                stream.Close();

                System.IO.File.WriteAllText(Shapefile.GetCpgFileName(fileName), encoding.BodyName);
            }
            catch (Exception ex)
            {
                string message = ex.Message;

                string m2 = message + " " + control.ToString();
            }
        }