Beispiel #1
0
        static async Task Main(string[] args)
        {
            var options = new DbContextOptionsBuilder <SpatialDbContext>().UseNpgsql("connectionstring").Options;

            using (var context = new SpatialDbContext(options))
            {
                // Mayestik Market
                Point query = new Point(106.7888256, -6.2408434)
                {
                    SRID = 4326
                };

                // Tanjung Priok
                //Point query = new Point(106.8811533, -6.1076897) { SRID = 4326 };

                // RSUP Dr.Sitanala
                //Point query = new Point(106.6381633, -6.1514835) { SRID = 4326 };

                // Tanjung Perak Surabaya
                //Point query = new Point(112.7233752, -7.2219498) { SRID = 4326 };

                // multiply by 1000M
                const double radiusMeters = 100 * 1000;
                var          places       = await context
                                            .Places
                                            .Where(x => x.Point.Distance(query) <= radiusMeters)
                                            .OrderBy(x => x.Point.Distance(query))
                                            .Select(t => new
                {
                    t.Name,
                    Distance = t.Point.Distance(query) / 1000
                })
                                            .ToListAsync();

                foreach (var place in places)
                {
                    Console.WriteLine($"{place.Name}, {Math.Round(place.Distance)} KM");
                }
            }

            Console.ReadLine();
        }
        public void CanCreateGeogrametryColumn()
        {
            using (SpatialDbContext ctx = new SpatialDbContext())
              {
            ctx.Database.Delete();
            ctx.Database.Create();
            var dis = new SPoint { point = DbGeometry.PointFromText(string.Format("POINT({0} {1})", 47.37, -122.21), 4326) };
            ctx.SPoints.Add(dis);

            var dis_2 = new SPoint { point = DbGeometry.PointFromText(string.Format("POINT({0} {1})", 147.37, -122.21), 4326) };
            ctx.SPoints.Add(dis_2);

            ctx.SaveChanges();

            var points = ctx.SPoints.First();
            Assert.AreEqual("POINT (47.37 -122.21)", points.point.AsText());
              }
        }