如何从ASP.Net核心中的Json文件播种NetTopologySuite.Geometries.Point数据 [英] How to seed NetTopologySuite.Geometries.Point data from a Json file in ASP.Net core
问题描述
我想从种子文件中为用户对象的位置"数据注入种子
I want to seed "Location" data for my user object from my seed file
其中Point是NetTopologySuite.Geometries.Point
的c#对象是我的用户对象的一部分
The c# object, where Point is a NetTopologySuite.Geometries.Point
is part of my user object
public class User: IdentityUser<int> {
// member data here
public Point Location { get; set; } // has lat/lng data points
}
在启动时,我通过执行类似的操作将数据播种到数据库中
I seed data to my db on startup by doing something like this
public void SeedUsers()
{
if (!_userManager.Users.Any())
{
var userData = System.IO.File.ReadAllText("Data/UserSeedData.json");
var users = JsonConvert.DeserializeObject<List<User>>(userData);
var roles = new List<Role>
{
new Role{Name = "Member"},
new Role{Name = "Admin"},
new Role{Name = "Moderator"},
new Role{Name = "VIP"},
};
foreach (var role in roles)
{
_roleManager.CreateAsync(role).Wait();
}
foreach (var user in users)
{
user.Photos.SingleOrDefault().IsApproved = true;
_userManager.CreateAsync(user, "password").Wait();
_userManager.AddToRoleAsync(user, "Member").Wait();
}
}
}
具有这样的json数组的json文件"UserSeedData.json",我希望能够在其中粘贴表示lng/lat数据点的某种位置"数据.
with a json file "UserSeedData.json" of json arrays like this and I want to be able to stick some kind of 'Location' data in there that is representative of lng/lat data points.
{
"Email": "myemailaddress@gmail.com",
"Username": "Lola",
"Gender": "female",
"DateOfBirth": "1994-02-21",
"Password": "password",
"Created": "2017-08-02",
"LastActive": "2017-08-02",
"Introduction": "blah blah blah",
"LookingFor": "blah blah blah",
"City": "San Francisco",
"Country": "United States",
"Longitude": -122.431297,
"Latitude": 37.773972,
"Location": // something here!!!
"Photos": [{
"url": "https://randomuser.me/api/portraits/women/3.jpg",
"isMain": true,
"description": "Non deserunt labore sunt ex laboris et adipisicing ullamco officia minim."
}]
}
现在,我知道我的种子方法可以做到这一点,但是我正在寻找一种将其包含在.json文件中的方法,以便可以使用不同的数据点
Now I know in my seed method I could do something like this, but I'm looking for a way to include it in my .json file, so I can use different data points
foreach (var user in users)
{
user.Photos.SingleOrDefault().IsApproved = true;
user.Location = new Point(-122.4194155, 37.7749295) { SRID = 4326 };
_userManager.CreateAsync(user, "password").Wait();
_userManager.AddToRoleAsync(user, "Member").Wait();
}
推荐答案
您可以将NetTopologySuite.Geometries.Point
子类化,并添加[JsonConstructor]
来解析您的json文件.它应该是其余代码的直接替代.
You could subclass NetTopologySuite.Geometries.Point
and add a [JsonConstructor]
to parse your json file. It should be a straightforward substitution for the rest of your code.
public class MyPoint : Point
{
[JsonConstructor]
public MyPoint(double latitude, double longitude, int srid)
:base(new GeoAPI.Geometries.Coordinate(longitude, latitude))
{
SRID = srid;
}
}
请注意,纬度= y,经度= x,因此顺序相反.
Note that latitude = y and longitude = x so the order is reversed.
在User
类中将MyPoint
替换为Point
public class User: IdentityUser<int> {
// member data here
public MyPoint Location { get; set; }
}
它应该可以与您的json一起使用.
And it should work with your json as is.
这篇关于如何从ASP.Net核心中的Json文件播种NetTopologySuite.Geometries.Point数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!