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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#MongoDb api - Geojson几何存储成一个类相关的知识,希望对你有一定的参考价值。

这是我第一次接近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; }
       }

谢谢你的帮助!

答案

在这里你有我的助手方法

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; }

以上是关于C#MongoDb api - Geojson几何存储成一个类的主要内容,如果未能解决你的问题,请参考以下文章

就 mongoDb 而言,GeoJSON 和 Legacy 坐标对有啥区别?

城市 Geojson 多边形数据库/库或 API

GIS开发:GeoJSON文件压缩

如何使用 python 从 geojson 创建 GeometryCollection?

如何使用 Hibernate 在 PostGIS 中将 geojson(在 javascript 中)保存到几何(MultiPolygon,4326)

cesium 何如加载大数据量的geojson格式的数据,geojson有切片吗