Exemplo n.º 1
0
        public void ReadDbfDate()
        {
            string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                       string.Format("..{0}..{0}..{0}NetTopologySuite.Samples.Shapefiles{0}date.dbf", Path.DirectorySeparatorChar));

            if (!File.Exists(file))
            {
                throw new FileNotFoundException("file not found at " + Path.GetDirectoryName(file));
            }

            DbaseFileReader reader = new DbaseFileReader(file);
            DbaseFileHeader header = reader.GetHeader();
            IEnumerator     ienum  = reader.GetEnumerator();

            ienum.MoveNext();
            ArrayList items = ienum.Current as ArrayList;

            Assert.IsNotNull(items);
            Assert.AreEqual(2, items.Count);

            foreach (Object item in items)
            {
                Assert.IsNotNull(item);
            }

            DateTime date = (DateTime)items[1];

            Assert.AreEqual(10, date.Day);
            Assert.AreEqual(3, date.Month);
            Assert.AreEqual(2006, date.Year);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Test getting and setting the properties
        /// </summary>
        public void Test_TestProperties()
        {
            DbaseFileReader dbfReader = new DbaseFileReader(Global.GetUnitTestRootDirectory() + @"\IO\Shapefile\Testfiles\statepop.dbf");
            DbaseFileHeader dbfHeader = dbfReader.GetHeader();

            Assertion.AssertEquals("Dbase header: Num records", 49, dbfHeader.NumRecords);
            Assertion.AssertEquals("Dbase header: Num fields", 252, dbfHeader.NumFields);

            Assertion.AssertEquals("Field 0: name", "STATE_NAME", dbfHeader.Fields[0].Name);
            Assertion.AssertEquals("Field 0: name", 'C', dbfHeader.Fields[0].DbaseType);
            Assertion.AssertEquals("Field 0: name", typeof(string), dbfHeader.Fields[0].Type);
            Assertion.AssertEquals("Field 0: name", 25, dbfHeader.Fields[0].Length);

            Assertion.AssertEquals("Field 251: name", "SAMP_POP", dbfHeader.Fields[251].Name);
            Assertion.AssertEquals("Field 251: name", 'N', dbfHeader.Fields[251].DbaseType);
            Assertion.AssertEquals("Field 251: name", typeof(double), dbfHeader.Fields[251].Type);
            Assertion.AssertEquals("Field 251: name", 19, dbfHeader.Fields[251].Length);

            // note alaska and hawaii are missing - hence 48 states not 50.
            int i = 0;

            foreach (ArrayList columnValues in dbfReader)
            {
                if (i == 0)
                {
                    Assertion.AssertEquals("Row1: STATE_NAME:", "Illinois", columnValues[0]);
                    Assertion.AssertEquals("Row1: STATE_FIPS:", "17", columnValues[1].ToString());
                    Assertion.AssertEquals("Row1: SAMP_POP", 1747776.0, columnValues[251]);
                }
                if (i == 48)
                {
                    Assertion.AssertEquals("Row1: STATE_NAME:", "Washington", columnValues[0]);
                    Assertion.AssertEquals("Row1: STATE_FIPS:", "53", columnValues[1].ToString());
                    Assertion.AssertEquals("Row1: SAMP_POP", 736744.0, columnValues[251]);
                }
                i++;
            }
            Assertion.AssertEquals("48 Records", 48, i - 1);

            DbaseFileReader dbfReader2 = new DbaseFileReader(Global.GetUnitTestRootDirectory() + @"\IO\Shapefile\Testfiles\statepop.dbf");

            i = 0;
            foreach (ArrayList columnValues in dbfReader2)
            {
                if (i == 0)
                {
                    Assertion.AssertEquals("Row1: STATE_NAME:", "Illinois", columnValues[0]);
                    Assertion.AssertEquals("Row1: STATE_FIPS:", "17", columnValues[1].ToString());
                    Assertion.AssertEquals("Row1: SAMP_POP", 1747776.0, columnValues[251]);
                }
                if (i == 48)
                {
                    Assertion.AssertEquals("Row1: STATE_NAME:", "Washington", columnValues[0]);
                    Assertion.AssertEquals("Row1: STATE_FIPS:", "53", columnValues[1].ToString());
                    Assertion.AssertEquals("Row1: SAMP_POP", 736744.0, columnValues[251]);
                }
                i++;
            }
            Assertion.AssertEquals("48 Records", 48, i - 1);
        }
        public void ReadDbfDate()
        {
            string file = Path.Combine(CommonHelpers.TestShapefilesDirectory, "date.dbf");

            if (!File.Exists(file))
            {
                throw new FileNotFoundException("file not found at " + Path.GetDirectoryName(file));
            }

            var reader = new DbaseFileReader(file);
            var header = reader.GetHeader();
            var ienum  = reader.GetEnumerator();

            ienum.MoveNext();
            var items = ienum.Current as ArrayList;

            Assert.IsNotNull(items);
            Assert.AreEqual(2, items.Count);

            foreach (object item in items)
            {
                Assert.IsNotNull(item);
            }

            var date = (DateTime)items[1];

            Assert.AreEqual(10, date.Day);
            Assert.AreEqual(3, date.Month);
            Assert.AreEqual(2006, date.Year);
        }
        public ShapefileDataReader(IStreamProviderRegistry streamProviderRegistry, IGeometryFactory geometryFactory)
        {
            if (streamProviderRegistry == null)
            {
                throw new ArgumentNullException("streamProviderRegistry");
            }
            if (geometryFactory == null)
            {
                throw new ArgumentNullException("geometryFactory");
            }
            _open = true;

            _dbfReader = new DbaseFileReader(streamProviderRegistry);
            _shpReader = new ShapefileReader(streamProviderRegistry, geometryFactory);

            _dbfHeader   = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields    = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            for (int i = 0; i < _dbfHeader.Fields.Length; i++)
            {
                _dbaseFields[i + 1] = _dbfHeader.Fields[i];
            }

            _shpHeader     = _shpReader.Header;
            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();
            _moreRecords   = true;
        }
        /// <summary>
        /// Initializes a new instance of the ShapefileDataReader class.
        /// </summary>
        /// <param name="filename">The shapefile to read (minus the .shp extension)</param>
        ///<param name="geometryFactory">The GeometryFactory to use.</param>
        public ShapefileDataReader(string filename, IGeometryFactory geometryFactory)
        {
            if (String.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }
            if (geometryFactory == null)
            {
                throw new ArgumentNullException("geometryFactory");
            }
            _open = true;

            string dbfFile = Path.ChangeExtension(filename, "dbf");

            _dbfReader = new DbaseFileReader(dbfFile);
            string shpFile = Path.ChangeExtension(filename, "shp");

            _shpReader = new ShapefileReader(shpFile, geometryFactory);

            _dbfHeader   = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            // copy dbase fields to our own array. Insert into the first position, the shape column
            _dbaseFields    = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1];
            _dbaseFields[0] = DbaseFieldDescriptor.ShapeField();
            for (int i = 0; i < _dbfHeader.Fields.Length; i++)
            {
                _dbaseFields[i + 1] = _dbfHeader.Fields[i];
            }

            _shpHeader     = _shpReader.Header;
            _dbfEnumerator = _dbfReader.GetEnumerator();
            _shpEnumerator = _shpReader.GetEnumerator();
            _moreRecords   = true;
        }
Exemplo n.º 6
0
        private void NewCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Layer currentLayer;

            switch ((string)e.Parameter)
            {
            case "layer":
                NewLayerDialog newLayerDialog = new NewLayerDialog();
                if (newLayerDialog.ShowDialog() == true)
                {
                    Layer           newLayer = new Layer();
                    DbaseFileReader reader   = new DbaseFileReader(
                        new NetTopologySuite.IO.Streams.ByteStreamProvider(
                            NetTopologySuite.IO.Streams.StreamTypes.Data,
                            new MemoryStream(Properties.Resources.EmptyDbaseHeader1)));
                    newLayer.Header    = reader.GetHeader();
                    newLayer.Name      = newLayerDialog.LayerName;
                    newLayer.ShapeType = newLayerDialog.ShapeType;
                    Layers.Add(newLayer);
                }
                break;

            case "field":
                currentLayer = (Layer)CollectionViewSource.GetDefaultView(Layers).CurrentItem;
                currentLayer.Header.AddColumn(newFieldName.Text, ((string)((ComboBoxItem)newFieldType.SelectedItem).Tag)[0], newFieldLength.Value ?? 0, newFieldDecimals.Value ?? 0);
                DbaseFieldDescriptor descriptor = currentLayer.Header.Fields.Last();
                currentLayer.Attributes.Add(descriptor);
                object newValue = null;
                var    @switch  = new Dictionary <Type, Action> {
                    { typeof(Int32), () => newValue = 0 },
                    { typeof(Double), () => newValue = 0.0 },
                    { typeof(String), () => newValue = "" },
                    { typeof(DateTime), () => newValue = DateTime.UtcNow },
                    { typeof(Boolean), () => newValue = false },
                };
                @switch[descriptor.Type]();
                foreach (ShapefileShape shape in currentLayer.Shapes)
                {
                    shape.Attributes.Add(new ShapefileAttributeEntry(descriptor, newValue));
                }
                break;

            case "shape":
                currentLayer = (Layer)CollectionViewSource.GetDefaultView(Layers).CurrentItem;
                ShapefileShape newShape = new ShapefileShape(currentLayer.ShapeType);
                newShape.CreateAttributes(currentLayer.Attributes);
                currentLayer.Shapes.Add(newShape);
                break;

            default:
                break;
            }
        }
Exemplo n.º 7
0
        public static List <Feature> ReadDbasefile(string dbfFilename)
        {
            var features = new List <Feature>();

            DbaseFileReader dr = new DbaseFileReader(dbfFilename);

            DbaseFileHeader header = dr.GetHeader();

            foreach (System.Collections.ArrayList atts in dr)
            {
                AttributesTable attributesTable = new AttributesTable();
                for (int i = 0; i < header.NumFields; i++)
                {
                    attributesTable.AddAttribute(header.Fields[i].Name, atts[i]);
                }

                features.Add(new Feature(null, attributesTable));
            }

            return(features);
        }
        private void TestSharcDbf()
        {
            const string filename = @"Strade.dbf";

            if (!File.Exists(filename))
            {
                throw new FileNotFoundException(filename + " not found at " + Environment.CurrentDirectory);
            }

            var reader = new DbaseFileReader(filename);
            var header = reader.GetHeader();

            Console.WriteLine("HeaderLength: " + header.HeaderLength);
            Console.WriteLine("RecordLength: " + header.RecordLength);
            Console.WriteLine("NumFields: " + header.NumFields);
            Console.WriteLine("NumRecords: " + header.NumRecords);
            Console.WriteLine("LastUpdateDate: " + header.LastUpdateDate);
            foreach (var descr in header.Fields)
            {
                Console.WriteLine("FieldName: " + descr.Name);
                Console.WriteLine("DBF Type: " + descr.DbaseType);
                Console.WriteLine("CLR Type: " + descr.Type);
                Console.WriteLine("Length: " + descr.Length);
                Console.WriteLine("DecimalCount: " + descr.DecimalCount);
                Console.WriteLine("DataAddress: " + descr.DataAddress);
            }

            var ienum = reader.GetEnumerator();

            while (ienum.MoveNext())
            {
                var objs = (ArrayList)ienum.Current;
                foreach (var obj in objs)
                {
                    Console.WriteLine(obj);
                }
            }
            Console.WriteLine();
        }
Exemplo n.º 9
0
        public static Dictionary <int, AttributesTable> ReadExtendedAttributes(string filename)
        {
            if (!File.Exists(filename))
            {
                Console.WriteLine("The file " + filename + " does not exist.");
                return(new Dictionary <int, AttributesTable>());
            }
            var result = new Dictionary <int, AttributesTable>();

            DbaseFileReader reader = TryDbaseFileReader(filename);

            if (reader == null)
            {
                return(new Dictionary <int, AttributesTable>());
            }

            var header    = reader.GetHeader();
            int linkIndex = 0;

            for (int i = 0; i < header.NumFields; ++i)
            {
                if (header.Fields[i].Name == "LNK")
                {
                    linkIndex = i;
                }
            }
            foreach (ArrayList entry in reader)
            {
                int link = (int)entry[linkIndex];
                Console.WriteLine("LNK=" + link.ToString());
                result[link] = new AttributesTable();
                for (int i = 0; i < header.NumFields; ++i)
                {
                    result[link].Add(header.Fields[i].Name, entry[i]);
                }
            }
            return(result);
        }
Exemplo n.º 10
0
        public static Dictionary <string, AttributesTable> ReadClassAttributes(string filename)
        {
            if (!File.Exists(filename))
            {
                Console.WriteLine("The file " + filename + " does not exist.");
                return(new Dictionary <string, AttributesTable>());
            }
            var             result = new Dictionary <string, AttributesTable>();
            DbaseFileReader reader = TryDbaseFileReader(filename);

            if (reader == null)
            {
                return(new Dictionary <string, AttributesTable>());
            }

            var header         = reader.GetHeader();
            int classNameIndex = 0;

            for (int i = 0; i < header.NumFields; ++i)
            {
                if (header.Fields[i].Name == "CNAM")
                {
                    classNameIndex = i;
                }
            }
            foreach (ArrayList entry in reader)
            {
                string className = (string)entry[classNameIndex];
                result[className] = new AttributesTable();
                for (int i = 0; i < header.NumFields; ++i)
                {
                    result[className].Add(header.Fields[i].Name, entry[i]);
                }
            }
            return(result);
        }