예제 #1
0
 private static IGeometry SqlGeometryToGeometryMultiPoint(SqlGeometry geometry, GeometryFactory factory)
 {
     IPoint[] points = new IPoint[geometry.STNumGeometries().Value];
     for (int i = 1; i <= points.Length; i++)
         points[i - 1] = SqlGeometryToGeometryPoint(geometry.STGeometryN(i), factory);
     return factory.CreateMultiPoint(points);
 }
예제 #2
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));
            }
        }
예제 #3
0
        public static IGeometry SqlGeometryToGeometry(SqlGeometry geom, GeometryFactory factory)
        {
            if (geom.STIsEmpty())
                return factory.CreateGeometryCollection(null);

            OpenGisGeometryType geometryType = (OpenGisGeometryType)Enum.Parse(typeof(OpenGisGeometryType), geom.STGeometryType().Value);
            switch (geometryType)
            {
                case OpenGisGeometryType.Point:
                    return SqlGeometryToGeometryPoint(geom, factory);
                case OpenGisGeometryType.LineString:
                    return SqlGeometryToGeometryLineString(geom, factory);
                case OpenGisGeometryType.Polygon:
                    return SqlGeometryToGeometryPolygon(geom, factory);
                case OpenGisGeometryType.MultiPoint:
                    return SqlGeometryToGeometryMultiPoint(geom, factory);
                case OpenGisGeometryType.MultiLineString:
                    return SqlGeometryToGeometryMultiLineString(geom, factory);
                case OpenGisGeometryType.MultiPolygon:
                    return SqlGeometryToGeometryMultiPolygon(geom, factory);
                case OpenGisGeometryType.GeometryCollection:
                    return SqlGeometryToGeometryGeometryCollection(geom, factory);
            }

            throw new ArgumentException(string.Format("Cannot convert SqlServer '{0}' to Geometry", geom.STGeometryType()), "geom");
        }
예제 #4
0
        public static void Convert(ShapefileGeometryType type, SqlDataReader reader, string shapefile)
        {
            if (!reader.Read())
                throw new Exception("No Results found");

            int geomOrdinal, colCount;
            ShapefileHeader shapeHeader = ShapefileHeader.CreateEmpty(type);
            DbfHeader dbfHeader = BuildHeader(reader, out geomOrdinal, out colCount);
            GeometryFactory factory = new GeometryFactory();
            Envelope env = shapeHeader.Bounds;

            using (ShapefileDataWriter writer = ShapefileDataWriter.Create(shapefile, dbfHeader, shapeHeader))
            {
                do
                {
                    SqlGeometry geom = reader[geomOrdinal] as SqlGeometry;
                    if (!geom.STIsValid())
                        geom = geom.MakeValid();

                    for (int i = 0, offset = 0; i < colCount; i++)
                    {
                        if (i == geomOrdinal)
                            offset++;

                        writer.Record.SetRaw(i, reader[i + offset]);
                    }

                    ExpandEnv(env, geom.STBoundary());
                    writer.Write(ConvertToGeometry.SqlGeometryToGeometry(geom, factory));
                }
                while (reader.Read());
            }
        }
예제 #5
0
        private static IMultiLineString SqlGeometryToGeometryMultiLineString(SqlGeometry geometry, GeometryFactory factory)
        {
            ILineString[] lineStrings = new ILineString[geometry.STNumGeometries().Value];
            for (int i = 1; i <= lineStrings.Length; i++)
                lineStrings[i - 1] = SqlGeometryToGeometryLineString(geometry.STGeometryN(i), factory);

            return factory.CreateMultiLineString(lineStrings);
        }
예제 #6
0
        private static IGeometryCollection SqlGeometryToGeometryGeometryCollection(SqlGeometry geometry, GeometryFactory factory)
        {
            IGeometry[] geoms = new IGeometry[geometry.STNumGeometries().Value];
            for (int i = 1; i <= geoms.Length; i++)
                geoms[i - 1] = SqlGeometryToGeometry(geometry.STGeometryN(i), factory);

            return factory.CreateGeometryCollection(geoms);
        }
        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();
                    }
                }
            }
        }
예제 #8
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();
                    }
                }
            }
        }
예제 #9
0
 /// <summary>
 /// Creates a sort of default geometry object from the default geometry factory, or creates
 /// a new one if one doesn't exist yet.
 /// </summary>
 protected Geometry()
 {
     if (DefaultFactory == null)
     {
         DefaultFactory = new GeometryFactory(new PrecisionModel(PrecisionModelType.Floating));
     }
     _factory = DefaultFactory;
     _srid = _factory.Srid;
 }
예제 #10
0
 private static IPoint SqlGeometryToGeometryPoint(SqlGeometry geometry, GeometryFactory factory)
 {
     return factory.CreatePoint(new Coordinate(geometry.STX.Value, geometry.STY.Value));
 }
예제 #11
0
        private static IMultiPolygon SqlGeometryToGeometryMultiPolygon(SqlGeometry geometry, GeometryFactory factory)
        {
            IPolygon[] polygons = new IPolygon[geometry.STNumGeometries().Value];
            for (var i = 1; i <= polygons.Length; i++)
                polygons[i - 1] = SqlGeometryToGeometryPolygon(geometry.STGeometryN(i), factory);

            return factory.CreateMultiPolygon(polygons);
        }
예제 #12
0
 private static ILineString SqlGeometryToGeometryLineString(SqlGeometry geometry, GeometryFactory factory)
 {
     return factory.CreateLineString(GetPoints(geometry));
 }
예제 #13
0
        private static IPolygon SqlGeometryToGeometryPolygon(SqlGeometry geometry, GeometryFactory factory)
        {
            ILinearRing exterior = factory.CreateLinearRing(GetPoints(geometry.STExteriorRing()));

            ILinearRing[] interior = null;
            if (geometry.STNumInteriorRing() > 0)
            {
                interior = new ILinearRing[geometry.STNumInteriorRing().Value];
                for (int i = 1; i <= interior.Length; i++)
                    interior[i - 1] = factory.CreateLinearRing(GetPoints(geometry.STInteriorRingN(i)));
            }

            return factory.CreatePolygon(exterior, interior);
        }
예제 #14
0
 public GeometryTest(int srid = 0)
 {
     _factory = new GeometryFactory(new PrecisionModel(), srid);
 }