Esempio 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));
        }       
Esempio n. 2
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);             
        }
Esempio n. 3
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));
        }
Esempio n. 4
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);
        }
        /// <summary>
        /// Retrieves the contents of the specified table, and writes them to a shapefile
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public bool ExportGriddedShapefile(string tableName)
        {
            try
            {
                _log.DebugFormat("Retrieving contents of job {0}", tableName);
                //don't filter out geometries, we'll do that at the cell level
                var exportFeatures = GetShapeFeaturesToExport(tableName, false);
                if ((exportFeatures == null) || (exportFeatures.Count == 0))
                {
                    _log.ErrorFormat("Export of Job \"{0}\" failed, no features to export!", tableName);
                    return false;
                }

                ICoordinateSystem outputCrs = GeographicCoordinateSystem.WGS84;
                if (!string.IsNullOrEmpty(this.OutputProjectionFilename))
                {
                    outputCrs = Utilities.GetCoordinateSystemByWKTFile(this.OutputProjectionFilename);
                }

                //if we need to reproject:
                List<IGeometry> filteringGeoms = GetFilteringGeometries(this.ExportFilterFilename, outputCrs);

                //put everything into a basic spatial index
                Envelope env = new Envelope();
                var index = new GisSharpBlog.NetTopologySuite.Index.Strtree.STRtree();
                for (int i = 0; i < exportFeatures.Count; i++)
                {
                    Feature f = exportFeatures[i];
                    index.Insert(f.Geometry.EnvelopeInternal, f);
                    env.ExpandToInclude(f.Geometry.EnvelopeInternal);
                }
                if (IsCancelled()) { _log.Debug("Job Cancelled..."); return false; }
                index.Build();

                //adjust envelope to only scan area inside filtering geometries
                if (!string.IsNullOrEmpty(this.GridEnvelopeFilename))
                {
                    //a specified envelope file overrides the envelope of the filtering geometries
                    env = GetGridEnvelope();
                }
                else if ((filteringGeoms != null) && (filteringGeoms.Count > 0))
                {
                    //in the absence ... //TODO: finish--
                    env = new Envelope();
                    for (int i = 0; i < filteringGeoms.Count; i++)
                    {
                        env.ExpandToInclude(filteringGeoms[i].EnvelopeInternal);
                    }
                }

                //progress tracking
                DateTime start = DateTime.Now, lastCheck = DateTime.Now;
                int lastProgress = 0;

                var features = new List<Feature>(exportFeatures.Count);

                double cellWidth = GridCellWidth;
                double cellHeight = GridCellHeight;
                bool discardEmptyGridCells = !IncludeEmptyGridCells;

                int numRows = (int)Math.Ceiling(env.Height / cellHeight);
                int numCols = (int)Math.Ceiling(env.Width / cellWidth);
                int expectedCells = numRows * numCols;

                if (expectedCells > 1000000)
                {
                    _log.Warn("**********************");
                    _log.Warn("Your selected area will produce a shapefile with over a million cells, is that a good idea?");
                    _log.WarnFormat("Area of {0}, Expected Cell Count of {1}", env.Area, expectedCells);
                    _log.Warn("**********************");
                }

                DbaseFileHeader header = null;
                using (var conn = DbClient.GetConnection())
                {
                    //Dictionary<string, DataRow> shapeDict = GetShapeRowsByLOGRECNO(conn);
                    var variablesDT = DataClient.GetMagicTable(conn, DbClient, string.Format("SELECT * FROM \"{0}\" where 0 = 1 ", tableName));
                    header = ShapefileHelper.SetupHeader(variablesDT);
                }
                ShapefileHelper.AddColumn(header, "CELLID", typeof(string));
                ShapefileHelper.AddColumn(header, "GEOID", typeof(string));
                if (this.AddStrippedGEOIDcolumn)
                {
                    ShapefileHelper.AddColumn(header, "GEOID_STRP", typeof(string));
                }

                //lets not add these to the fishnet exports just yet
                //if (this.AddGeometryAttributesToOutput)
                //{
                //    ShapefileHelper.AddColumn(header, "AREA", typeof(double));
                //    ShapefileHelper.AddColumn(header, "PERIMETER", typeof(double));
                //    ShapefileHelper.AddColumn(header, "CENTROID", typeof(double));
                //}

                int cellCount = 0;
                int xidx = 0;
                for (double x = env.MinX; x < env.MaxX; x += cellWidth)
                {
                    xidx++;
                    int yidx = 0;
                    for (double y = env.MinY; y < env.MaxY; y += cellHeight)
                    {
                        yidx++;
                        cellCount++;
                        string cellID = string.Format("{0}_{1}", xidx, yidx);

                        Envelope cellEnv = new Envelope(x, x + cellWidth, y, y + cellHeight);
                        IGeometry cellCenter = new Point(cellEnv.Centre);
                        IGeometry cellGeom = Utilities.IEnvToIGeometry(cellEnv);

                        Feature found = null;
                        IList mightMatch = index.Query(cellGeom.EnvelopeInternal);
                        foreach (Feature f in mightMatch)
                        {
                            if (f.Geometry.Contains(cellCenter))
                            {
                                found = f;
                                break;
                            }
                        }

                        if ((found == null) && (discardEmptyGridCells))
                        {
                            //_log.DebugFormat("No feature found for cell {0}", cellID);
                            continue;
                        }

                        //if we have filtering geometries, skip a cell if it isn't included
                        if (!IsIncluded(cellGeom, filteringGeoms))
                        {
                            continue;
                        }

                        if ((cellCount % 1000) == 0)
                        {
                            int step = (int)((((double)cellCount) / ((double)expectedCells)) * 100.0);
                            TimeSpan elapsed = (DateTime.Now - lastCheck);
                            if ((step != lastProgress) && (elapsed.TotalSeconds > 1))
                            {
                                _log.DebugFormat("{0:###.##}% complete, {1:#0.0#} seconds, {2} built, {3} checked, {4} left",
                                   step, (DateTime.Now - start).TotalSeconds,
                                   features.Count,
                                   cellCount,
                                   expectedCells - cellCount
                                   );
                                lastCheck = DateTime.Now;
                                lastProgress = step;

                                if (IsCancelled()) { _log.Debug("Job Cancelled..."); return false; }
                            }
                        }

                        //this is a lot of work just to add an id...
                        AttributesTable attribs = new AttributesTable();
                        if (found != null)
                        {
                            //if (!found.Attributes.GetNames().Contains("GEOID"))
                            //    throw new Exception("GEOID NOT FOUND!!!!");
                            foreach (string name in found.Attributes.GetNames())
                            {
                                attribs.AddAttribute(name, found.Attributes[name]);
                            }
                            attribs.AddAttribute("CELLID", cellID);
                        }
                        else
                        {
                            foreach (var field in header.Fields)
                            {
                                attribs.AddAttribute(field.Name, null);
                            }
                            attribs["CELLID"] = cellID;
                        }

                        features.Add(new Feature(cellGeom, attribs));
                    }
                }
                _log.Debug("Done building cells, Saving Shapefile...");
                header.NumRecords = features.Count;

                if (features.Count == 0)
                {
                    _log.Error("No features found, exiting!");
                    return false;
                }

                string newShapefilename = Path.Combine(Environment.CurrentDirectory, tableName);
                if (!string.IsNullOrEmpty(OutputFolder))
                {
                    newShapefilename = Path.Combine(OutputFolder, tableName);
                }

                if (IsCancelled()) { _log.Debug("Job Cancelled..."); return false; }
                var writer = new ShapefileDataWriter(newShapefilename, ShapefileHelper.GetGeomFactory());
                writer.Header = header;
                writer.Write(features);

                if (!string.IsNullOrEmpty(this.OutputProjectionFilename))
                {
                    //Reproject everything in this file to the requested projection...
                    ShapefileHelper.MakeOutputProjFile(this.OutputProjectionFilename, newShapefilename);
                }
                else
                {
                    ShapefileHelper.MakeCensusProjFile(newShapefilename);
                }

                _log.Debug("Done! Shapefile exported successfully");
                return true;
            }
            catch (FileNotFoundException notFound)
            {
                string msg = "A needed file couldn't be found: " + notFound.FileName;
                _log.Error(msg);
                _log.Fatal("The export cannot continue.  Exiting...");
                throw new ApplicationException(msg);
            }
            catch (Exception ex)
            {
                _log.Error("Error while exporting shapefile", ex);
            }
            return false;
        }
        /// <summary>
        /// Retrieves the contents of the specified table, and writes them to a shapefile
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public bool ExportShapefile(string tableName)
        {
            try
            {
                _log.DebugFormat("Retrieving contents of job {0}", tableName);

                var exportFeatures = GetShapeFeaturesToExport(tableName, true);
                if ((exportFeatures == null) || (exportFeatures.Count == 0))
                {
                    _log.ErrorFormat("Export of Job \"{0}\" failed, no features to export!", tableName);
                    return false;
                }

                DbaseFileHeader header = null;
                using (var conn = DbClient.GetConnection())
                {
                    var variablesDT = DataClient.GetMagicTable(
                        conn,
                        DbClient,
                        string.Format("SELECT * FROM \"{0}\" where 0 = 1 ", tableName));
                    header = ShapefileHelper.SetupHeader(variablesDT);
                }

                ShapefileHelper.AddColumn(header, "GEOID", typeof(string));
                if (this.AddStrippedGEOIDcolumn)
                {
                    ShapefileHelper.AddColumn(header, "GEOID_STRP", typeof(string));
                }

                if (this.AddGeometryAttributesToOutput)
                {
                    ShapefileHelper.AddColumn(header, "AREA", typeof(double));
                    ShapefileHelper.AddColumn(header, "PERIMETER", typeof(double));
                    ShapefileHelper.AddColumn(header, "CENTROID_X", typeof(double));
                    ShapefileHelper.AddColumn(header, "CENTROID_Y", typeof(double));
                }

                header.NumRecords = exportFeatures.Count;

                string newShapefilename = Path.Combine(Environment.CurrentDirectory, tableName);
                if (!string.IsNullOrEmpty(OutputFolder))
                {
                    newShapefilename = Path.Combine(OutputFolder, tableName);
                }
                string destPath = Path.GetDirectoryName(newShapefilename);
                FileUtilities.SafePathEnsure(destPath);

                var writer = new ShapefileDataWriter(newShapefilename, ShapefileHelper.GetGeomFactory());
                writer.Header = header;

                if (!string.IsNullOrEmpty(this.OutputProjectionFilename))
                {
                    ShapefileHelper.MakeOutputProjFile(this.OutputProjectionFilename, newShapefilename);
                }
                else
                {
                    ShapefileHelper.MakeCensusProjFile(newShapefilename);
                }
                writer.Write(exportFeatures);

                _log.Debug("Shapefile exported successfully");

                if (Settings.ShowFilePaths)
                {
                    _log.InfoFormat("Shapefile saved as \"{0}.shp\"", newShapefilename);
                }

                return true;
            }
            catch (FileNotFoundException notFound)
            {
                string msg = "A needed file couldn't be found: " + notFound.FileName;
                _log.Error(msg);
                _log.Fatal("The export cannot continue.  Exiting...");
                throw new ApplicationException(msg);
            }
            catch (Exception ex)
            {
                _log.Error("Error while exporting shapefile", ex);
            }
            return false;
        }
Esempio n. 7
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));
        }