コード例 #1
0
        public void FixtureSetup()
        {
            var connStrBuilder = new NpgsqlConnectionStringBuilder(Properties.Settings.Default.PostGis);
            if (string.IsNullOrEmpty(connStrBuilder.Host) || string.IsNullOrEmpty(connStrBuilder.Database))
            {
                Assert.Ignore("Requires PostgreSQL connectionstring");
            }


            GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();

            try
            {
                // Set up sample table
                using (var conn = new NpgsqlConnection(Properties.Settings.Default.PostGis))
                {
                    conn.Open();
                    // Load data
                    using (var shapeFile = new SharpMap.Data.Providers.ShapeFile(GetTestFile()))
                    {
                        shapeFile.SRID = 4326;
                        shapeFile.Open();

                        using (var cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = "DROP TABLE IF EXISTS roads_ugl";
                            cmd.ExecuteNonQuery();

                            // The ID column cannot simply be int, because that would cause GetObjectIDsInView to fail. The provider internally works with uint
                            cmd.CommandText =
                                "CREATE TABLE roads_ugl(id integer primary key, name character varying(100), geog geography);";
                            cmd.ExecuteNonQuery();
                        }


                        IEnumerable<uint> indexes = shapeFile.GetObjectIDsInView(shapeFile.GetExtents());

                        _insertedIds = new List<uint>(indexes.Take(100));
                        using (NpgsqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText =
                                "INSERT INTO roads_ugl(id, name, geog) VALUES (@PId, @PName, ST_GeogFromWKB(@PGeom));";
                            var @params = cmd.Parameters;
                            @params.AddRange(
                                new[]
                                    {
                                        new NpgsqlParameter("PId", NpgsqlDbType.Integer),
                                        new NpgsqlParameter("PName", NpgsqlDbType.Varchar, 100),
                                        new NpgsqlParameter("PGeom", NpgsqlDbType.Bytea)
                                    });

                            var writer = new PostGisWriter();

                            foreach (var idx in _insertedIds)
                            {
                                var feature = shapeFile.GetFeature(idx);

                                @params["PId"].NpgsqlValue = (int)idx;
                                @params["PName"].NpgsqlValue = feature["NAME"];
                                @params["PGeom"].NpgsqlValue = writer.Write(feature.Geometry);
                                cmd.ExecuteNonQuery();
                            }
                        }

                        // Verify
                        foreach (var pgp in GetTestProvider())
                        {
                            foreach (var idx in _insertedIds)
                            {
                                var g1 = pgp.GetGeometryByID(idx);
                                var g2 = shapeFile.GetGeometryByID(idx);
                                Assert.AreEqual(g1, g2);
                            }
                        }
                    }

                }
            }
            catch (Exception ee)
            {
                Assert.Ignore("Failed to connect to PostgreSQL/PostGIS Server");
            }
        }
コード例 #2
0
ファイル: PostGisTests.cs プロジェクト: geobabbler/SharpMap
        public void FixtureSetup()
        {
            var connStrBuilder = new NpgsqlConnectionStringBuilder(Properties.Settings.Default.PostGis);
            if (string.IsNullOrEmpty(connStrBuilder.Host) || string.IsNullOrEmpty(connStrBuilder.Database))
            {
                Assert.Ignore("Requires PostgreSQL connectionstring");
            }

            GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
            try
            {
                // Set up sample table
                using (var conn = new NpgsqlConnection(Properties.Settings.Default.PostGis))
                {
                    conn.Open();
                    // Load data
                    using (var shapeFile = new SharpMap.Data.Providers.ShapeFile(GetTestFile(), false, false, 4326))
                    {
                        shapeFile.Open();

                        using (var cmd = conn.CreateCommand())
                        {
                            cmd.CommandText =
                                "SELECT COUNT(*) FROM \"geometry_columns\" WHERE \"f_table_name\" = 'roads_ugl_g' AND \"f_geometry_column\"='geom';";
                            var count = cmd.ExecuteScalar();
                            if (Convert.ToInt32(count) > 0)
                            {
                                cmd.CommandText = "SELECT DropGeometryColumn('roads_ugl_g', 'geom');";
                                cmd.ExecuteNonQuery();
                                cmd.CommandText = "DROP TABLE roads_ugl_g";
                                cmd.ExecuteNonQuery();
                            }

                            // The ID column cannot simply be int, because that would cause GetOidsInView to fail. The provider internally works with uint
                            cmd.CommandText =
                                "CREATE TABLE roads_ugl_g(id integer primary key, name character varying(100));";
                            cmd.ExecuteNonQuery();
                            cmd.CommandText = "SELECT AddGeometryColumn('roads_ugl_g', 'geom', " + shapeFile.SRID +
                                              ", 'GEOMETRY', 2);";
                            cmd.ExecuteNonQuery();
                        }

                        IEnumerable<uint> indexes = shapeFile.GetOidsInView(shapeFile.GetExtents());

                        _insertedIds = new List<uint>(indexes.Take(100));
                        using (NpgsqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText =
                                "INSERT INTO roads_ugl_g(id, name, geom) VALUES (@PId, @PName, @PGeom);";
                            var @params = cmd.Parameters;
                            @params.AddRange(
                                new[]
                                    {
                                        new NpgsqlParameter("PId", NpgsqlDbType.Integer),
                                        new NpgsqlParameter("PName", NpgsqlDbType.Varchar, 100),
                                        new NpgsqlParameter("PGeom", NpgsqlDbType.Bytea)
                                    });

                            var writer = new PostGisWriter();

                            foreach (var idx in _insertedIds)
                            {
                                var feature = shapeFile.GetFeatureByOid(idx);

                                @params["PId"].NpgsqlValue = (int) idx;
                                @params["PName"].NpgsqlValue = feature.Attributes["NAME"];
                                @params["PGeom"].NpgsqlValue = writer.Write(feature.Geometry);
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }

                }
            }
            catch
            {
                Assert.Ignore("Failed to connect to PostgreSQL/PostGIS Server");
            }
        }
コード例 #3
0
		public void Point3D()
		{
			// Warm up assertions:
			IPoint point2D = new Point(1, 2);
			Assert.IsTrue(Double.IsNaN(point2D.Z));

			IPoint point3D = new Point(1, 2, 3);
			Assert.IsFalse(Double.IsNaN(point3D.Z));

			// The real thing:
			IPoint source = new Point(123, 456, 789);
		    var pgWriter = new PostGisWriter { HandleOrdinates = Ordinates.XYZ};
			byte[] bytes = pgWriter.Write(source);
			IPoint target = (IPoint)new PostGisReader().Read(bytes);
			Assert.AreEqual(source.X, target.X);
			Assert.AreEqual(source.Y, target.Y);
			Assert.AreEqual(source.Z, target.Z);
		}
コード例 #4
0
		public void LineString3D()
		{
			const int size = 10;
			Coordinate[] points = new Coordinate[size];
			for (int i = 0; i < size; i++)
			{
				// just some arbitrary values
				points[i] = new Coordinate(100*Math.Sin(i), 200*Math.Cos(i), 300*Math.Tan(i));
			}
			ILineString source = new LineString(points);
		    var pgWriter = new PostGisWriter() {HandleOrdinates = Ordinates.XYZ};
            byte[] bytes = pgWriter.Write(source);
			ILineString target = (ILineString)new PostGisReader().Read(bytes);
			for (int i = 0; i < size; i++)
			{
				Assert.AreEqual(source.Coordinates[i].X, target.Coordinates[i].X);
				Assert.AreEqual(source.Coordinates[i].Y, target.Coordinates[i].Y);
				Assert.AreEqual(source.Coordinates[i].Z, target.Coordinates[i].Z);
			}
		}
コード例 #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wkt"></param>
        /// <param name="srid"></param>
		private static void General(string wkt, int srid)
		{
			var geom = wr.Read(wkt);
			var parsed = geom.AsText();
			var regeom = wr.Read(parsed);
			var reparsed = regeom.AsText();

			geom.SRID = srid;
			regeom.SRID = srid;

            Assert.IsTrue(geom.EqualsExact(regeom));
			Assert.AreEqual(parsed, reparsed);

			var bytesB = new PostGisWriter(ByteOrder.BigEndian).Write(regeom);
			var regeom2 = br.Read(bytesB);
			Assert.IsTrue(geom.EqualsExact(regeom2));

			byte[] bytesL = new PostGisWriter(ByteOrder.LittleEndian).Write(regeom);
			var regeom3 = br.Read(bytesL);
            Assert.IsTrue(geom.EqualsExact(regeom3));
            Assert.IsTrue(regeom2.EqualsExact(regeom3));
			

			Assert.AreEqual(bytesB.Length, bytesL.Length);
		}