Entity Framework Openlayers Geometry 对象的 CRUD 操作

Entity Framework CRUD operations with Openlayers Geometry object

我正在使用 Openlayers 并尝试将几何信息保存在我的数据库中。

当我在openlayers地图上绘制多边形时,生成的对象(特征)是这种格式

{
 "type": "Polygon",
    "coordinates": [
      [
        [
          54.86572265625,
          74.0013854318592
        ],
        [
          53.59130859375,
          73.62159408606237
        ],
        [
          53.96484375,
          73.16953636227885
        ],
        [
          55.986328125,
          73.59679245247814
        ]
      ]
    ]
  }

以上对象有 2 个属性。 {type: string, coordinates: someNestedArray}

我正在将此对象传递给我的 API 以将其保存在数据库中。但是我面临着定义坐标类型的问题 属性.

基本上它是 float[][][] 类型所以我创建了我的 EF 模型 class 如下

public class Geometry
  {
     public string Type { get; set; }

     public float[][][] Coordinates { get; set; }
  }

当我尝试 get/update 时,EF 抛出错误

{"The property 'Geometry.Coordinates' could not be mapped, because it is of type 'float[][][]'
 which is not a supported primitive type or a valid entity type. Either explicitly map this property, 
or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."}

错误很明显。 EF 无法自行映射这些不受支持的类型。我如何明确定义映射?或者有什么办法可以让它工作吗? float[][][] 类型是否正确?

提前致谢。

您始终可以将坐标保存为字符串,其中坐标以逗号分隔。

比如在FE部分你可以这样处理:

        const someLine =  feature.getGeometry().transform("EPSG:4326", 
                        "EPSG:3857");
   
        const geometryLine = someLine
          .clone()
          .getGeometry()
          .transform("EPSG:3857", "EPSG:4326");

        const geometryCoords = geometryLine.getCoordinates().join(";");

然后你会得到这样的东西:“43.520548594674132,26.565803087473146;....”可以将其作为字符串保存到数据库中。 (还可以用更少的小数点等进行调整。)

之后,如果您想通过 API handle/fetch 数据并通过自动映射器(或某些自定义实现)将其映射到坐标列表(例如类似这样的东西)

 public class Coordinates
    {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
    }

要将以前保存的数据映射到 DTO,您可以使用类似这样的东西

 public class GeneralProfile : Profile
   {
        public GeneralProfile()
        {
            CreateMap<Route, GetSavedRouteDTO>()
                .ForMember(x => x.TripLength, options => options.MapFrom(x => x.Length))
                .ForMember(x => x.RouteCoordinates, options => options.MapFrom(MapFromStringCoordinates));
            CreateMap<Route, RouteCreateDTO>().ReverseMap();
        }


        private List<Coordinates> MapFromStringCoordinates(Route route, GetSavedRouteDTO getSavedRouteDTO)
        {

            var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
            var numberFormat = (System.Globalization.NumberFormatInfo)currentCulture.NumberFormat.Clone();
            numberFormat.NumberDecimalSeparator = ".";

            var coordinates = new List<Coordinates>();
            var coordinatesSplit = route.Coordinates.Split(";");

            foreach (var coord in coordinatesSplit)
            {
                var currentCoord = coord.Split(","); 
                if (currentCoord.Length > 1)
                {
                    var latitude = double.Parse(currentCoord[0], numberFormat);
                    var longitude = double.Parse(currentCoord[1], numberFormat);
                    var coords= new Coordinates { Latitude = latitude, Longitude = longitude };

                    coordinates.Add(coords);
                }
            }

            return coordinates;
        }
    } 

有了它,您将获得包含纬度和经度的坐标列表,您将从中创建几何实体。