C#MongoDb api - Geojson几何存储成一个类

问题描述 投票:0回答:1

这是我第一次接近mongodb api。这是我到目前为止编写的代码。我被卡住了,因为我无法在我的类中保存多边形类型坐标,我无法运行GeoWithin函数。

        //Connection
        MongoClient client = new MongoClient(path);
        var db = client.GetDatabase("ProgettoADM");



        //Getting the collections
        var restaurants = db.GetCollection<Restaurants>("restaurants");
        var neighborhood = db.GetCollection<Neighborhood>("neighborhood");


        // Creating variables for the query
        var longitude = -73.93414657;
        var latitude = 40.82302903;
        var point = new GeoJson2DGeographicCoordinates(longitude, latitude);
        var pnt = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(point);
        var distance = 1609.34 * 5;


        //Index creation
        var restaurantsIndex = Builders<Restaurants>.IndexKeys.Geo2DSphere("location.coordinates");
        var neighborhoodIndex = Builders<Neighborhood>.IndexKeys.Geo2DSphere("geometry");
        restaurants.Indexes.CreateOne(restaurantsIndex);
        neighborhood.Indexes.CreateOne(neighborhoodIndex);

        //Filter creation for the NearSphere function            
        var nearSphereQuery = Builders<Restaurants>.Filter.NearSphere(p => p.location.coordinates, pnt, distance);

        //NearSphere Query execution
        List<Restaurants> nearSphereQueryResult = restaurants.Find(nearSphereQuery).ToListAsync().Result;

        //GeoIntersect Query execution
        var geoIntersectQuery = Builders<Neighborhood>.Filter.GeoIntersects("geometry", pnt);
        var c = neighborhood.Find(geoIntersectQuery).First();

        //Filter creation for the GeoWithin function
        var GeoWithinQuery = Builders<Restaurants>.Filter.GeoWithin(p => p.location.coordinates,/*data from variable c*/);

这些是我保存数据的类,餐馆类正常工作,邻居不起作用。

       [BsonIgnoreExtraElements]
       public class Restaurants 
       {
          public ObjectId Id { get; set; }        
          public string name { get; set; }
          public Location location { get; set; }
       }
       [BsonIgnoreExtraElements]
       public class Location
       {               
          public double[] coordinates { get; set; }
          public string type { get; set; }
       }

       [BsonIgnoreExtraElements]
       public class Neighborhood
       {
         public ObjectId Id { get; set; }
         public string name { get; set; }
         public Geometry geometry { get; set; }
       }

       [BsonIgnoreExtraElements]
       public class Geometry
       {
         public BsonArray[] coordinates { get; set; }
       }

谢谢你的帮助!

c# mongodb geometry geojson mongodb-.net-driver
1个回答
0
投票

在这里你有我的助手方法

public static GeoJson2DGeographicCoordinates GetCoordinates(double longitude, double latitude)
{
    return new GeoJson2DGeographicCoordinates(longitude, latitude);
}

public static GeoJsonPoint<GeoJson2DGeographicCoordinates> GetJsonPoint(double x, double y)
{
    GeoJson2DGeographicCoordinates output = GetCoordinates(x, y);
    if (output == null)
    {
        return null;
    }

    return new GeoJsonPoint<GeoJson2DGeographicCoordinates>(output);
}

您可以将它们存储在DB中,如下所示:

 public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }
© www.soinside.com 2019 - 2024. All rights reserved.