Пример #1
0
        public static void Convert(string shapefile, string connectionString, string tableName, int srid = 0, string targetProjectionWKT = null)
        {
            GeometryTransform transform = GeometryTransform.GetTransform(shapefile, targetProjectionWKT);
            GeometryFactory   factory   = new GeometryFactory(new PrecisionModel(), srid);

            using (SqlConnection conn = new SqlConnection(connectionString))
                using (SqlBulkCopy copy = new SqlBulkCopy(conn))
                    using (ShapefileDataReader reader = new ShapefileDataReader(shapefile, factory, transform))
                    {
                        conn.Open();

                        string createTableSql = GenerateCreateTableQuery(reader, tableName);
                        using (SqlCommand createTableComm = new SqlCommand(createTableSql, conn))
                            createTableComm.ExecuteNonQuery();

                        copy.SqlRowsCopied += (object sender, SqlRowsCopiedEventArgs e) =>
                        {
                            System.Console.Clear();
                            System.Console.WriteLine("Copied " + e.RowsCopied);
                        };
                        copy.NotifyAfter          = 257;
                        copy.DestinationTableName = tableName;
                        copy.WriteToServer(new ShapefileBulkSqlReader(reader, srid));
                    }
        }
Пример #2
0
        public static void Combine(string finalShape, string projection, params CombineShapefile[] combineShapes)
        {
            DbfHeader dbfHeader = new DbfHeader();

            dbfHeader.AddCharacter("Label", 80);

            ShapefileHeader shapeHeader = ShapefileHeader.CreateEmpty(ShapefileGeometryType.Polygon);
            GeometryFactory gf          = new GeometryFactory();

            using (ShapefileDataWriter writer = ShapefileDataWriter.Create(finalShape, dbfHeader, shapeHeader))
            {
                // Write the projection file.
                File.WriteAllText(Path.ChangeExtension(finalShape, ".prj"), projection);

                foreach (CombineShapefile workerShp in combineShapes)
                {
                    GeometryTransform transform = GeometryTransform.GetTransform(workerShp.FilePath, projection);

                    using (ShapefileIndexReader index = new ShapefileIndexReader(Path.ChangeExtension(workerShp.FilePath, ".shx")))
                    {
                        if (transform != null)
                        {
                            writer.Header.Bounds.ExpandToInclude(transform.Apply(index.Header.Bounds));
                        }
                        else
                        {
                            writer.Header.Bounds.ExpandToInclude(index.Header.Bounds);
                        }

                        Task[] tasks = new Task[Environment.ProcessorCount];
                        for (int i = 0; i < tasks.Length; i++)
                        {
                            tasks[i] = Task.Factory.StartNew(() =>
                            {
                                using (ShapefileBlockReader reader = new ShapefileBlockReader(workerShp.FilePath, index, gf, transform))
                                {
                                    while (reader.Read())
                                    {
                                        writer.Write(reader.Geometry, reader.Record.GetString(workerShp.Label));
                                    }
                                }
                            });
                        }

                        Task.WaitAll(tasks);

                        writer.Flush();
                    }
                }
            }
        }
        public static void Reproject(string destinationFolder, string projection, params ReprojectShapefile[] shapes)
        {
            ProjectionInfo targetProjection = ProjectionInfo.FromEsriString(projection);

            foreach (ReprojectShapefile shape in shapes)
            {
                string shapePath = Path.Combine(destinationFolder, shape.DestinationName);

                ShapefileHeader shapeHeader = ShapefileHeader.CreateEmpty(ShapefileGeometryType.Polygon);
                DbfHeader       dbfHeader   = new DbfHeader();
                dbfHeader.AddCharacter("Label", 80);
                GeometryFactory gf = new GeometryFactory();

                using (ShapefileDataWriter writer = ShapefileDataWriter.Create(shapePath + ".shp", dbfHeader, shapeHeader))
                {
                    GeometryTransform transform = null;
                    if (File.Exists(Path.ChangeExtension(shape.Source, ".prj")))
                    {
                        transform = GeometryTransform.GetTransform(shape.Source, projection);

                        if (transform != null)
                        {
                            File.WriteAllText(shapePath + ".prj", projection);
                        }
                        else
                        {
                            File.Copy(Path.ChangeExtension(shape.Source, ".prj"), shapePath + ".prj");
                        }
                    }

                    using (ShapefileIndexReader index = new ShapefileIndexReader(Path.ChangeExtension(shape.Source, ".shx")))
                    {
                        if (transform != null)
                        {
                            writer.Header.Bounds.ExpandToInclude(transform.Apply(index.Header.Bounds));
                        }
                        else
                        {
                            writer.Header.Bounds.ExpandToInclude(index.Header.Bounds);
                        }

                        Task[] tasks = new Task[Environment.ProcessorCount];
                        for (int i = 0; i < tasks.Length; i++)
                        {
                            tasks[i] = Task.Factory.StartNew(() =>
                            {
                                using (ShapefileBlockReader reader = new ShapefileBlockReader(shape.Source, index, gf, transform))
                                {
                                    while (reader.Read())
                                    {
                                        writer.Write(reader.Geometry, reader.Record.GetString(shape.Label));
                                    }
                                }
                            });
                        }

                        Task.WaitAll(tasks);

                        writer.Flush();
                    }
                }
            }
        }