/// <summary>
        /// Helper function for SetupHeader, correctly maps C# data types to DbaseFileHeader types
        /// </summary>
        /// <param name="header"></param>
        /// <param name="columnName"></param>
        /// <param name="t"></param>
        public static void AddColumn(DbaseFileHeader header, string columnName, Type t)
        {
            columnName = Utilities.EnsureMaxLength(columnName, 10);

            if (t == typeof(bool))
            {
                header.AddColumn(columnName, 'L', 1, 0);
            }
            else if (t == typeof(string))
            {
                header.AddColumn(columnName, 'C', 254, 0);
            }
            else if (t == typeof(DateTime))
            {
                header.AddColumn(columnName, 'C', 22, 0);
            }
            else if (t == typeof(float) || t == typeof(double) || t == typeof(decimal))
            {
                header.AddColumn(columnName, 'N', 18, 10);
            }
            else if (t == typeof(short) || t == typeof(int) || t == typeof(long) ||
                     t == typeof(ushort) || t == typeof(uint) || t == typeof(ulong))
            {
                header.AddColumn(columnName, 'N', 18, 0);
            }
        }
        /// <summary>
        /// Method to write a dummy dbase file
        /// </summary>
        /// <param name="dbfWriter">The dbase file writer</param>
        /// <param name="recordCount">The number of records</param>
        public static void WriteDummyDbf(DbaseFileWriter dbfWriter, int recordCount)
        {
            // Create the dummy header
            var dbfHeader = new DbaseFileHeader {
                NumRecords = recordCount
            };

            // add some dummy column
            dbfHeader.AddColumn("Description", 'C', 20, 0);

            // Write the header
            dbfWriter.Write(dbfHeader);
            // Write the features
            for (var i = 0; i < recordCount; i++)
            {
                var columnValues = new List <double> {
                    i
                };
                dbfWriter.Write(columnValues);
            }

            // End of file flag (0x1A)
            dbfWriter.WriteEndOfDbf();

            dbfWriter.Close();
        }
        /// <summary>
        /// Gets the stub header.
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        public static DbaseFileHeader GetHeader(Feature feature, int count)
        {
            IAttributesTable attribs = feature.Attributes;

            string[]        names  = attribs.GetNames();
            DbaseFileHeader header = new DbaseFileHeader();

            header.NumRecords = count;
            foreach (string name in names)
            {
                Type type = attribs.GetType(name);
                if (type == typeof(double) || type == typeof(float))
                {
                    header.AddColumn(name, 'N', DoubleLength, DoubleDecimals);
                }
                else if (type == typeof(short) || type == typeof(ushort) ||
                         type == typeof(int) || type == typeof(uint) ||
                         type == typeof(long) || type == typeof(ulong))
                {
                    header.AddColumn(name, 'N', IntLength, IntDecimals);
                }
                else if (type == typeof(string))
                {
                    header.AddColumn(name, 'C', StringLength, StringDecimals);
                }
                else if (type == typeof(bool))
                {
                    header.AddColumn(name, 'L', BoolLength, BoolDecimals);
                }
                else if (type == typeof(DateTime))
                {
                    header.AddColumn(name, 'D', DateLength, DateDecimals);
                }
                else
                {
                    throw new ArgumentException("Type " + type.Name + " not supported");
                }
            }
            return(header);
        }
        public static DbaseFileHeader GetHeader(DbaseFieldDescriptor[] dbFields, int count)
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.NumRecords = count;

            foreach (DbaseFieldDescriptor dbField in dbFields)
            {
                header.AddColumn(dbField.Name, dbField.DbaseType, dbField.Length, dbField.DecimalCount);
            }

            return(header);
        }
        public void Setup()
        {
            var sfdr = new ShapefileDataWriter("encoding_sample");
            var h    = new DbaseFileHeader();

            h.AddColumn("id", 'n', 8, 0);
            h.AddColumn("Test", 'C', 15, 0);
            h.AddColumn("Ålder", 'N', 8, 0);
            h.AddColumn("Ödestext", 'C', 254, 0);
            h.NumRecords = 1;
            sfdr.Header  = h;

            var feats = new List <IFeature>();
            var at    = new AttributesTable();

            at.Add("id", "0");
            at.Add("Test", "Testar");
            at.Add("Ålder", 10);
            at.Add("Ödestext", "Lång text med åäö etc");
            feats.Add(new Feature(new Point(0, 0), at));
            sfdr.Write(feats);
        }
        public void ok_when_writing_shapefile_with_no_features()
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.AddColumn("X", 'C', 10, 0);
            ShapefileDataWriter writer = new ShapefileDataWriter(@"issue36")
            {
                Header = header
            };

            IList <IFeature> features = new List <IFeature>();

            writer.Write(features);
        }
Example #7
0
        public void ok_when_writing_shapefile_with_no_features()
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.AddColumn("X", 'C', 10, 0);
            ShapefileDataWriter writer = new ShapefileDataWriter(@"issue36")
            {
                Header = header
            };

            IList <IFeature> features = new List <IFeature>();

            Assert.DoesNotThrow(() => writer.Write(features));

            _numPassed++;
        }
Example #8
0
        private void SaveGraphResult(IGeometry path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            const string shapepath = "graphresult";

            if (File.Exists(shapepath + shp))
            {
                File.Delete(shapepath + shp);
            }
            Assert.IsFalse(File.Exists(shapepath + shp));
            if (File.Exists(shapepath + shx))
            {
                File.Delete(shapepath + shx);
            }
            Assert.IsFalse(File.Exists(shapepath + shx));
            if (File.Exists(shapepath + dbf))
            {
                File.Delete(shapepath + dbf);
            }
            Assert.IsFalse(File.Exists(shapepath + dbf));

            const string field1  = "OBJECTID";
            var          feature = new Feature(path, new AttributesTable());

            feature.Attributes.AddAttribute(field1, 0);

            var header = new DbaseFileHeader {
                NumRecords = 1, NumFields = 1
            };

            header.AddColumn(field1, 'N', 5, 0);

            var writer = new ShapefileDataWriter(shapepath, factory)
            {
                Header = header
            };

            writer.Write(new List <IFeature>(new[] { feature, }));

            Assert.IsTrue(File.Exists(shapepath + shp));
            Assert.IsTrue(File.Exists(shapepath + shx));
            Assert.IsTrue(File.Exists(shapepath + dbf));
        }
        /// <summary>
        /// Write the enumeration of features to shapefile (shp, shx and dbf)
        /// </summary>
        /// <param name="filename">Filename to create</param>
        /// <param name="features">Enumeration of features to write, features will be enumerated once</param>
        /// <param name="fields">Fields that should be written, only those attributes specified here will be mapped from the feature attributetable while writing</param>
        /// <param name="shapeGeometryType">Type of geometries shapefile</param>
        /// <param name="dbfEncoding">Optional Encoding to be used when writing the DBF-file (default Windows-1252)</param>
        public static void WriteFeatures(string filename, IEnumerable <IFeature> features, DbaseFieldDescriptor[] fields, ShapeGeometryType shapeGeometryType,
                                         Encoding dbfEncoding = null)
        {
            // Set default encoding if not specified
            if (dbfEncoding == null)
            {
                dbfEncoding = Encoding.GetEncoding(1252);
            }

            // Open shapefile and dbase stream writers
            using (var shpWriter = new ShapefileWriter(Path.ChangeExtension(filename, ".shp"), shapeGeometryType))
            {
                using (var dbfWriter = new DbaseFileWriter(Path.ChangeExtension(filename, ".dbf"), dbfEncoding))
                {
                    var dbfHeader = new DbaseFileHeader(dbfEncoding);
                    foreach (var field in fields)
                    {
                        dbfHeader.AddColumn(field.Name, field.DbaseType, field.Length, field.DecimalCount);
                    }
                    dbfWriter.Write(dbfHeader);

                    var numFeatures = 0;
                    foreach (var feature in features)
                    {
                        shpWriter.Write(feature.Geometry);
                        var values = new object[fields.Length];
                        for (var i = 0; i < fields.Length; i++)
                        {
                            values[i] = feature.Attributes[fields[i].Name];
                        }
                        dbfWriter.Write(values);
                        numFeatures++;
                    }

                    // set the number of records
                    dbfHeader.NumRecords = numFeatures;
                    // Update the header
                    dbfWriter.Write(dbfHeader);
                    // write the end of dbase file marker
                    dbfWriter.WriteEndOfDbf();
                    // close the dbase stream
                    dbfWriter.Close();
                }
            }
        }
        public void ok_when_writing_shapefile_with_features()
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.AddColumn("X", 'C', 10, 0);
            ShapefileDataWriter writer = new ShapefileDataWriter(@"issue36")
            {
                Header = header
            };

            IAttributesTable attributesTable = new AttributesTable();

            attributesTable.AddAttribute("X", "y");
            IFeature feature = new Feature(new Point(1, 2), attributesTable);

            IList <IFeature> features = new List <IFeature>();

            features.Add(feature);
            writer.Write(features);
        }
        public void ok_when_writing_shapefile_with_features()
        {
            var header = new DbaseFileHeader();

            header.AddColumn("X", 'C', 10, 0);
            var writer = new ShapefileDataWriter(@"issue36")
            {
                Header = header
            };

            IAttributesTable attributesTable = new AttributesTable();

            attributesTable.Add("X", "y");
            IFeature feature = new Feature(new Point(1, 2), attributesTable);

            IList <IFeature> features = new List <IFeature>();

            features.Add(feature);

            Assert.DoesNotThrow(() => writer.Write(features));

            _numPassed++;
        }
Example #12
0
        public void BuildShapefilesFromGraphBinary()
        {
            int       index = 0;
            IGeometry edges;
            WKBReader reader = new WKBReader(factory);

            using (FileStream stream = new FileStream("graph", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                edges = reader.Read(stream);
                index++;
            }
            Assert.AreEqual(1, index);
            Assert.IsNotNull(edges);
            Assert.IsInstanceOfType(typeof(MultiLineString), edges);
            Assert.AreEqual(1179, edges.NumGeometries);

            string field1   = "OBJECTID";
            string field2   = "DESCRIPTION";
            IList  features = new List <Feature>(edges.NumGeometries);

            for (int i = 0; i < edges.NumGeometries; i++)
            {
                IGeometry ls = edges.GetGeometryN(i);
                Assert.IsInstanceOfType(typeof(LineString), ls);

                Feature f = new Feature(ls, new AttributesTable());
                f.Attributes.AddAttribute(field1, i);
                f.Attributes.AddAttribute(field2, String.Format("length: {0}", Convert.ToInt64(ls.Length)));
                features.Add(f);
            }
            Assert.IsNotEmpty(features);
            Assert.AreEqual(edges.NumGeometries, features.Count);

            DbaseFileHeader header = new DbaseFileHeader();

            header.NumRecords = edges.NumGeometries;
            header.NumFields  = 1;
            header.AddColumn(field1, 'N', 5, 0);
            header.AddColumn(field2, 'C', 254, 0);

            string path = "graph";

            if (File.Exists(path + shp))
            {
                File.Delete(path + shp);
            }
            Assert.IsFalse(File.Exists(path + shp));
            if (File.Exists(path + shx))
            {
                File.Delete(path + shx);
            }
            Assert.IsFalse(File.Exists(path + shx));
            if (File.Exists(path + dbf))
            {
                File.Delete(path + dbf);
            }
            Assert.IsFalse(File.Exists(path + dbf));

            ShapefileDataWriter writer = new ShapefileDataWriter(path, factory);

            writer.Header = header;
            writer.Write(features);

            Assert.IsTrue(File.Exists(path + shp));
            Assert.IsTrue(File.Exists(path + shx));
            Assert.IsTrue(File.Exists(path + dbf));

            IList subset = new List <Feature>(15);

            for (int i = 0; i < 15; i++)
            {
                subset.Add(features[i]);
            }
            Assert.IsNotEmpty(subset);
            Assert.AreEqual(15, subset.Count);

            path = "minimalgraph";
            if (File.Exists(path + shp))
            {
                File.Delete(path + shp);
            }
            Assert.IsFalse(File.Exists(path + shp));
            if (File.Exists(path + shx))
            {
                File.Delete(path + shx);
            }
            Assert.IsFalse(File.Exists(path + shx));
            if (File.Exists(path + dbf))
            {
                File.Delete(path + dbf);
            }
            Assert.IsFalse(File.Exists(path + dbf));

            writer        = new ShapefileDataWriter(path, factory);
            writer.Header = header;
            writer.Write(subset);

            Assert.IsTrue(File.Exists(path + shp));
            Assert.IsTrue(File.Exists(path + shx));
            Assert.IsTrue(File.Exists(path + dbf));
        }
        private void WriteGridAbundances(Project currentProject, string shapefileName)
        {
            try {
                List <IPolygon> projectGrids = new List <IPolygon>();
                General_queries projq        = new General_queries(currentProject);

                foreach (Station st in currentProject.StationsList)
                {
                    IPolygon     grid             = st.Grid;
                    StationStats tempStationStats = new StationStats(st.Guid, st.StationID);

                    foreach (StationStats stst in projq.AllStatsByStation)
                    {
                        if (stst.StationGUID == st.Guid)
                        {
                            tempStationStats = stst;
                            grid.UserData    = tempStationStats;
                        }
                    }

                    projectGrids.Add(grid);
                }

                GeometryCollection gc = new GeometryCollection(projectGrids.ToArray());

                //Open Writer
                ShapefileWriter shpWriter = new ShapefileWriter();
                shpWriter.Write(shapefileName, gc);

                //Create Header & Columns for points
                DbaseFileHeader dbfHeader = new DbaseFileHeader();


                dbfHeader.AddColumn("Station_ID", 'C', 20, 0);
                //One column for each species in project
                foreach (SpeciesStats spcst in projq.AllStatsBySpecies)
                {
                    dbfHeader.AddColumn(spcst.SpeciesName, 'N', 20, 0);
                }

                dbfHeader.NumRecords = gc.Count;

                //DBF Writer
                DbaseFileWriter dbfWriter = new DbaseFileWriter(shapefileName + ".dbf");
                dbfWriter.Write(dbfHeader);

                //Loop through Business Object to get Features
                foreach (IPolygon p in gc.Geometries)
                {
                    StationStats data = (StationStats)p.UserData;


                    ArrayList columnValues = new System.Collections.ArrayList();
                    //Add Values

                    columnValues.Add(data.StationID);
                    foreach (SpeciesStats s in data.SpeciesStats)
                    {
                        columnValues.Add(s.SpeciesPictures);
                    }

                    dbfWriter.Write(columnValues);
                }

                //Close File
                dbfWriter.Close();
            } catch (Exception ex) {
                throw ex;
            }
        }
Example #14
0
        private DbaseFileHeader getTrottoirHeader()
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.AddColumn(TrottoirShapeFileConstants.StrassenabschnittID, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Strassenname, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.StrassenabschnittBezeichnungvon, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.StrassenabschnittBezeichnungbis, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Eigentuemer, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Ortsbezeichnung, 'C', StringLength, StringDecimals);

            header.AddColumn(TrottoirShapeFileConstants.ID, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.ZustandsAbschnittID, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Bezeichnungvon, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Bezeichnungbis, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Laenge, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Breite, 'N', DoubleLength, DoubleDecimals);


            header.AddColumn(TrottoirShapeFileConstants.FlaecheTrottoir, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Lage, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Aufnahmedatum, 'D', DateLength, DateDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Aufnahmeteam, 'C', StringLength, StringDecimals);

            header.AddColumn(TrottoirShapeFileConstants.Zustandsindex, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Massnahmenvorschlag, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Kosten, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Dringlichkeit, 'C', StringLength, StringDecimals);
            header.AddColumn(TrottoirShapeFileConstants.Gesamtkosten, 'N', DoubleLength, DoubleDecimals);


            return(header);
        }
Example #15
0
        private DbaseFileHeader getStrabsHeader()
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.AddColumn(StrabShapeFileConstants.ID, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.Strassenname, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.Bezeichnungbis, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.Bezeichnungvon, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.Eigentuemer, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.Ortsbezeichnung, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.Belastungskategorie, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.Belag, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.BreiteFahrbahn, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.Laenge, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.FlaecheFahrbahn, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.Trottoir, 'C', StringLength, StringDecimals);
            header.AddColumn(StrabShapeFileConstants.BreiteTrottoirlinks, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.BreiteTrottoirrechts, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.FlaecheTrottoirlinks, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.FlaecheTrottoirrechts, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.FlaecheTrottoir, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.Wiederbeschaffungswert, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.AlterungsbeiwertI, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.WertverlustI, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.AlterungsbeiwertII, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(StrabShapeFileConstants.WertverlustII, 'N', DoubleLength, DoubleDecimals);
            return(header);
        }
Example #16
0
        private DbaseFileHeader getZabsHeader()
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.AddColumn(ZabShapeFileConstants.StrassenabschnittID, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.Strassenname, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.StrassenabschnittBezeichnungvon, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.StrassenabschnittBezeichnungbis, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.Eigentuemer, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.Ortsbezeichnung, 'C', StringLength, StringDecimals);

            header.AddColumn(ZabShapeFileConstants.ID, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.Bezeichnungvon, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.Bezeichnungbis, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.Laenge, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(ZabShapeFileConstants.FlaecheFahrbahn, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(ZabShapeFileConstants.FlaecheTrottoirlinks, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(ZabShapeFileConstants.FlaecheTrottoirrechts, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(ZabShapeFileConstants.Aufnahmedatum, 'D', DateLength, DateDecimals);
            header.AddColumn(ZabShapeFileConstants.Aufnahmeteam, 'C', StringLength, StringDecimals);

            header.AddColumn(ZabShapeFileConstants.FBZustandsindex, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.FBMassnahmenvorschlag, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.FBKosten, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(ZabShapeFileConstants.FBDringlichkeit, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.FBGesamtkosten, 'N', DoubleLength, DoubleDecimals);

            header.AddColumn(ZabShapeFileConstants.TRRZustandsindex, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.TRRMassnahmenvorschlag, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.TRRKosten, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(ZabShapeFileConstants.TRRDringlichkeit, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.TRRGesamtkosten, 'N', DoubleLength, DoubleDecimals);

            header.AddColumn(ZabShapeFileConstants.TRLZustandsindex, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.TRLMassnahmenvorschlag, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.TRLKosten, 'N', DoubleLength, DoubleDecimals);
            header.AddColumn(ZabShapeFileConstants.TRLDringlichkeit, 'C', StringLength, StringDecimals);
            header.AddColumn(ZabShapeFileConstants.TRLGesamtkosten, 'N', DoubleLength, DoubleDecimals);

            return(header);
        }