How to seed NetTopologySuite.Geometries.Point data from a Json file in ASP.Net core

user1186050

I want to seed "Location" data for my user object from my seed file

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

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": "[email protected]",
  "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."
  }]
}

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();
}
ste-fu

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

Note that latitude = y and longitude = x so the order is reversed.

Swap MyPoint for Point in your User class

public class User: IdentityUser<int> {
  // member data here
  public MyPoint Location { get; set; }
}

And it should work with your json as is.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Entity Framework Core 3.1 with NetTopologySuite.Geometries.Point: SqlException: The supplied value is not a valid instance of data type geography

How Can I Access Data From External Json File in asp.net core?

Adding Admins using Seed data Asp.net core

Asp.Net Core 3.1 Seed data with conditionally

Asp.NET Core json file or data path - where to put it

How to read AppSettings values from a .json file in ASP.NET Core

How to read connection string from appsettings.json in a DBContext file in Asp.Net Core?

How to pass a file and json object from a postman to asp.net core webapi

How to access additional form data sent from jquery file upload in asp.net core?

How to Accept File and string data from Post Request in ASP.Net Core 2.0?

How do I fill a Javascript var with Json data from a Controller in Asp.net Core

How to send Json Data from controller to javascript without in ASP.NET Core?

how to get config data from appsettings.json with asp.net core & react

How do I transfer data from userData in JSON format to a pie chart? ASP.Net Core

How can I return a list of data from my json file to my controller file in ASP.NET?

Restkit seed core-data with objects from a local file on launch

Entity Framework core seed large data from file

Add data from a JSON file to the Prisma database with a seed file

Seed test data for every test in ASP.NET Core / EF Core / xUnit.NET integration tests

How to retrieve JSON data in controller in ASP.net Core?

How to seed database with JSON in ASP.NET MVC

How to Export CSV file from ASP.NET core

How can I Extract JSON Object that Holds Base64 and File Extension from application/json body in ASP.NET Core

How to Read Data come from Angular to Asp.net Core

How to get data from PostgreSQL function in ASP.Net core

How to bind data from dropdown list in asp.net core

How do I access the returned json data from my controller from my ASP.NET Core AJAX form?

ASP.NET Core 2 Seed Database

Asp .Net Core Project.json File