Ejemplo n.º 1
1
        public void BuildStradeFixed()
        {
            string path = "strade" + shp;
            Assert.IsTrue(File.Exists(path));
            
            ShapefileDataReader reader = new ShapefileDataReader(path, factory);
            List<Feature> features = new List<Feature>(reader.RecordCount);
            while (reader.Read())
            {
                Feature feature = new Feature(reader.Geometry, new AttributesTable());
                object[] values = new object[reader.FieldCount - 1];
                reader.GetValues(values);
                for (int i = 0; i < values.Length; i++)
                {
                    string name = reader.GetName(i + 1);
                    object value = values[i];
                    feature.Attributes.AddAttribute(name, value);
                }
                features.Add(feature);
            }
            Assert.AreEqual(703, features.Count);

            string shapepath = "strade_fixed";
            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));

            DbaseFileHeader header = reader.DbaseHeader;
            
            ShapefileDataWriter writer = new ShapefileDataWriter(shapepath, factory);
            writer.Header = header;
            writer.Write(features);

            Assert.IsTrue(File.Exists(shapepath + shp));
            Assert.IsTrue(File.Exists(shapepath + shx));
            Assert.IsTrue(File.Exists(shapepath + dbf));
        }       
Ejemplo n.º 2
0
 /// <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;
 }
Ejemplo n.º 3
0
        private void TestShapeCreation()
        {
            ICoordinate[] points = new ICoordinate[3];
            points[0] = new Coordinate(0, 0);
            points[1] = new Coordinate(1, 0);
            points[2] = new Coordinate(1, 1);

            LineString line_string = new LineString(points);

            AttributesTable attributes = new AttributesTable();
            attributes.AddAttribute("FOO", "FOO");

            Feature feature = new Feature(Factory.CreateMultiLineString(new ILineString[] { line_string }), attributes);
            Feature[] features = new Feature[1];
            features[0] = feature;

            ShapefileDataWriter shp_writer = new ShapefileDataWriter("C:\\line_string");
            shp_writer.Header = ShapefileDataWriter.GetHeader(features[0], features.Length);
            shp_writer.Write(features);             
        }
Ejemplo n.º 4
0
        private void SaveGraphResult(IGeometry path)
        {
            if (path == null) 
                throw new ArgumentNullException("path");

            var 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));

            var 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<Feature>(new[] { feature, }));

            Assert.IsTrue(File.Exists(shapepath + shp));
            Assert.IsTrue(File.Exists(shapepath + shx));
            Assert.IsTrue(File.Exists(shapepath + dbf));
        }
Ejemplo n.º 5
0
        public static Feature ReadFeature(JsonTextReader jreader)
        {
            if (jreader == null)
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");

            Feature feature = new Feature();
            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                jreader.ReadToken(JsonTokenClass.Object);

                //Read the 'Feature' as the type
                jreader.ReadMember(); //reads 'type'
                jreader.ReadString(); //reads 'Feature'

                //Read the 'geometry'
                jreader.ReadMember(); //reads 'geometry'
                feature.Geometry = Read(jreader); //reads the geometry value

                //Read the 'properties'
                jreader.ReadMember(); //reads 'properties'
                feature.Attributes = ReadAttributesTable(jreader);

                jreader.ReadToken(JsonTokenClass.EndObject);
            }
            return feature;
        }
Ejemplo n.º 6
0
        public static void Write(Feature feature, TextWriter writer)
        {
            if (feature == null)
                return;
            if (writer == null)
                throw new ArgumentNullException("writer", "A valid text writer object is required.");

            JsonTextWriter jwriter = new JsonTextWriter(writer);
            Write(feature, jwriter);
        }
Ejemplo n.º 7
0
        public static void Write(Feature feature, JsonTextWriter jwriter)
        {
            if (feature == null)
                return;
            if (jwriter == null)
                throw new ArgumentNullException("jwriter", "A valid JSON writer object is required.");

            jwriter.WriteStartObject();

            jwriter.WriteMember("type");
            jwriter.WriteString("Feature");

            jwriter.WriteMember("geometry");
            Write(feature.Geometry, jwriter);

            jwriter.WriteMember("properties");
            Write(feature.Attributes, jwriter);

            jwriter.WriteEndObject();
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a new row in the given <see cref="SharpMap.Data.FeatureDataTable"/> <paramref name="dataTable"/>
 /// using data in <see cref="GisSharpBlog.NetTopologySuite.Features.Feature"/> <paramref name="feature"/>.
 /// </summary>
 /// <param name="dataTable">The <see cref="SharpMap.Data.FeatureDataTable"/> to fill.</param>
 /// <param name="feature">Data to insert in the <see cref="SharpMap.Data.FeatureDataTable"/>.</param>
 private void CreateNewRow(FeatureDataTable dataTable, Feature feature)
 {
     FeatureDataRow dataRow = dataTable.NewRow();
     dataRow.Geometry =
         GeometryConverter.ToSharpMapGeometry(
             feature.Geometry as GisSharpBlog.NetTopologySuite.Geometries.Geometry);
     foreach (string columnName in feature.Attributes.GetNames())
         dataRow[columnName] = feature.Attributes[columnName];
     dataTable.AddRow(dataRow);
 }
Ejemplo n.º 9
0
        private void ReadFromShapeFile()
        {
            ArrayList featureCollection = new ArrayList();
            string filename = @"country";
            if (!File.Exists(filename + ".dbf"))
                throw new FileNotFoundException(filename + " not found at " + Environment.CurrentDirectory);
            ShapefileDataReader dataReader = new ShapefileDataReader(filename, new GeometryFactory());                        
            while (dataReader.Read())
            {
                Feature feature = new Feature();                
                feature.Geometry = dataReader.Geometry;                
                                
                int length = dataReader.DbaseHeader.NumFields;
                string[] keys = new string[length];
                for (int i = 0; i < length; i++)                
                    keys[i] = dataReader.DbaseHeader.Fields[i].Name;                

                feature.Attributes = new AttributesTable();
                for (int i = 0; i < length; i++)
                {                                        
                    object val = dataReader.GetValue(i);
                    feature.Attributes.AddAttribute(keys[i], val);
                }
               
                featureCollection.Add(feature);
            }

            int index = 0;
            Console.WriteLine("Elements = " + featureCollection.Count);
            foreach (Feature feature in featureCollection)
            {
                Console.WriteLine("Feature " + index++);                
                AttributesTable table = feature.Attributes as AttributesTable;
                foreach (string name in table.GetNames())
                    Console.WriteLine(name + ": " + table[name]);
            }
            
            // Test write with stub header            
			string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../../NetTopologySuite.Samples.Shapefiles/testWriteStubHeader");
			if (File.Exists(file + ".shp")) File.Delete(file + ".shp");
            if (File.Exists(file + ".shx")) File.Delete(file + ".shx");
            if (File.Exists(file + ".dbf")) File.Delete(file + ".dbf");

            ShapefileDataWriter dataWriter = new ShapefileDataWriter(file);
            dataWriter.Header = ShapefileDataWriter.GetHeader(featureCollection[0] as Feature, featureCollection.Count);
            dataWriter.Write(featureCollection);

            // Test write with header from a existing shapefile
			file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../../NetTopologySuite.Samples.Shapefiles/testWriteShapefileHeader");
			if (File.Exists(file + ".shp")) File.Delete(file + ".shp");
            if (File.Exists(file + ".shx")) File.Delete(file + ".shx");
            if (File.Exists(file + ".dbf")) File.Delete(file + ".dbf");

            dataWriter = new ShapefileDataWriter(file);
            dataWriter.Header = ShapefileDataWriter.GetHeader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../../NetTopologySuite.Samples.Shapefiles/country.dbf"));
            dataWriter.Write(featureCollection);
        }
Ejemplo n.º 10
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));
        }