2

What i receive from a DbGeography data type is in Location.

{
    "@odata.context":"http://subdomain.mydomain.me/odata/$metadata#Pois","value":[
      {
          "Id":"d57e6603-5530-4457-b041-6398b7182173","Name":"Demo Home","Address":"Zuidstraat 8 8630 Veurne","Location":{
              "Geography":{
                  "CoordinateSystemId":4326,"WellKnownText":"POINT (51.515574 2.025285)","WellKnownBinary":null
              }
          },"TagId":"ec32e8f3-c0b8-4bcc-af6c-7059b1ef1a65","RouteId":null
      }
    ]
}

I have tried multiple POSTS, but none of them seem to succeed in adding a DbGeography Point.

Any thoughts on how to do this? I have tried adding the same as what is returned ( doesn't work) and using type + coordinates as properties ( as defined in the odata standard .

In another question, i have found this Error getting value from 'WellKnownValue' on 'System.Data.Entity.Spatial.DbGeography , but this solution seems insufficent.

If i'd add Location : "POINT(lat long)" i could use the following method to generate the data ( but i don't know how)

DbGeography.PointFromText(textValue, 4326)
Community
  • 1
  • 1
NicoJuicy
  • 3,435
  • 4
  • 40
  • 66

1 Answers1

1

I send the coordinates as longitude, latitude then converts to DbGeography at server side like this:

Model

public class MyModel
{
    [Required]
    public  double Longitude { get; set; }
    [Required]
    public double Latitude { get; set; }
}

Controller

public IHttpActionResult Post(MyModel model)
{
    if (!ModelState.IsValid || model == null)
    {
        // return error
    }

    var coordinates = DbGeography.FromText($"POINT({model.Longitude} {model.Latitude})");

    // save coordinates to database

    return Ok();
}
Niels R.
  • 7,260
  • 5
  • 32
  • 44
Mohamed Badr
  • 2,562
  • 2
  • 26
  • 43
  • Is this not double-handling though, requiring three times as much space in the database (by storing Point, longitude and latitude all at the same time)? Is there another method where you could send lat/long within the request, and create the .FromText object on the serverside, but avoid storing the raw lat/long values alongside? – kdpnz Jan 31 '21 at 09:32
  • I'm not sure if I understand what you exactly mean, but lat/long are already posted within the request to server-side and you don't have to save lat/long in the database, – Mohamed Badr Jan 31 '21 at 10:50
  • That's my problem - I don't know how to read the lat/long posted values without expliciately declaring 'longitude' and 'latitude' in my model. In my situation, adding them to my model is automatically creating DB columns for them. I'd like to just read their posted values from the request body. But, am currently unsure where to start on that. – kdpnz Feb 01 '21 at 00:34
  • This answer https://stackoverflow.com/a/18215561/3026149 saved my situation. I was able to integrate your code, and also avoid saving Latitude and Longitude as seperate DB columns. Basically, I didn't know about the [NotMapped] attribute before, so my class was able to expect data which wouldn't get saved. Thank you for your great code example. – kdpnz Feb 01 '21 at 08:32
  • I see, I'm using a model class just to post the data to the server-side and the entire class is not mapped to the database, that's why I wasn't able to understand your issue, thanks for sharing – Mohamed Badr Feb 02 '21 at 05:33