How can I convert latitude and longitude to Point

Hello

I hope you cna help. I spend two days trying to solve the problem Grr

I have a table with information about a station. There is two fields : station_lat and station_lng.

I am trying to add a leaflet map to my page follwing this tutorial.
First I follow step by step and I success. Then I adapt to my application and there is some problem.

The first problem I have to solve is the type of field.
In the tuto here, he created a models.py and the type of the field of the database is Point. The field name is location.
If now I look into the value, the value is:

‘POINT(6.000251769184164 46.19337852885697)’,0

And my problem is here

  1. I do not have a location field, but instead I have to field: station_lat (6.000251769184164) station_lng(46.19337852885697)

Then if you continue reading the turorial, he create an API here which will call a viewsets here

Observe (location is the name of the field of the table)

bbox_filter_field = “location”

At some point, the script call the file serializer and you can see again the line

geo_field = “location”

The problem here, is that I first do not have a location field, and even if I can change the value to ‘station_lat’, the station_lat field is not a type of Point

To try, I created a field location and I tried to save the value ‘POINT(6.000251769184164 46.19337852885697)’,0 but MySQL refused it :frowning:

Do you understand my problem?

My first quetsion, if you look at this

from rest_framework_gis import serializers

from .models import Stations

class StationsSerializer(serializers.GeoFeatureModelSerializer):
    """Marker GeoJSON serializer."""
    class Meta:
        """Marker serializer meta class."""
        #fields = ("id", "station_name", "station_lat", "station_lng")
        fields = ("id_station", "station_name", "station_lat", "station_lng")
        geo_field = "location"
        model = Stations

Is there a way to convert the value of my fileds station_lat and station_lng to what the function is expceted to received?

Second question
If you look at that script

class MarkerViewSet(viewsets.ReadOnlyModelViewSet):
    """Marker view set."""
    bbox_filter_field = "location"
    filter_backends = (filters.InBBoxFilter,)
    queryset = Stations.objects.all()
    serializer_class = StationsSerializer

Apparently bbox_filter_field = "location" is filtering location filed name but honnestly, I do understand what is exactely the role of bbox_filter_field. But how can adapt is as location does not exist and as I have the geolocation values saved in to two fields (station_lat and station_lng)

I hope you can help, at least for the first question

Many many thanks

I wrote before, I tried first with a version which work. Here is how I did.

Here is how I am trying to adapt. And more in detail here
api, viewsets and serializer
I hope you can help. Have a good week-end

Looking it at it from a “pure Django” / non-gis perspective, I’d be tempted to try this from a different direction.

It looks to me that what you’re needing to do is serialize your lat/lon as a 2-element list. What I would try then would be to add a property to my model that builds that list from the fields:

@property
def location(self):
    return [station_lat, station_lng]

and hope that the serializer “does the right thing” with it…

Dear @KenWhitesell

I found a way to do that. In my models.py I added the following

I had to create the filed ‘location’ but honestly I do not rember if I add the value 6.000251769184164, 46.19337852885697 or 0.0,0.0

Now, if I look at the retirn value of my serializers.py, the return value is

{
    "type": "FeatureCollection",
    "features": [
        {
            "id": 1,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-1",
                "station_lat": 46.187164,
                "station_lng": 5.997526
            }
        },
        {
            "id": 2,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-2",
                "station_lat": 46.173527,
                "station_lng": 6.003618
            }
        },
        {
            "id": 3,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-3",
                "station_lat": 46.186268,
                "station_lng": 5.997424
            }
        },
        {
            "id": 4,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-4",
                "station_lat": 46.173153,
                "station_lng": 6.003011
            }
        },
        {
            "id": 5,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-5",
                "station_lat": 46.187016,
                "station_lng": 5.997001
            }
        },
        {
            "id": 6,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-6",
                "station_lat": 46.173393,
                "station_lng": 6.004297
            }
        },
        {
            "id": 7,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-7",
                "station_lat": 46.186146,
                "station_lng": 5.997017
            }
        },
        {
            "id": 8,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-8",
                "station_lat": 46.172722,
                "station_lng": 6.00308
            }
        },
        {
            "id": 9,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-9",
                "station_lat": 46.184677,
                "station_lng": 5.997972
            }
        },
        {
            "id": 10,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-10",
                "station_lat": 46.182545,
                "station_lng": 5.999088
            }
        },
        {
            "id": 11,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-11",
                "station_lat": 46.185596,
                "station_lng": 5.996834
            }
        },
        {
            "id": 12,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-12",
                "station_lat": 46.182682,
                "station_lng": 6.003728
            }
        },
        {
            "id": 13,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-13",
                "station_lat": 46.184441,
                "station_lng": 5.999066
            }
        },
        {
            "id": 14,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-14",
                "station_lat": 46.172466,
                "station_lng": 6.00361
            }
        },
        {
            "id": 15,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-15",
                "station_lat": 46.17223,
                "station_lng": 6.002607
            }
        },
        {
            "id": 16,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-16",
                "station_lat": 46.183529,
                "station_lng": 6.006996
            }
        },
        {
            "id": 17,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-17",
                "station_lat": 46.172356,
                "station_lng": 6.004136
            }
        },
        {
            "id": 18,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-18",
                "station_lat": 46.179119,
                "station_lng": 6.004328
            }
        },
        {
            "id": 20,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-20",
                "station_lat": 46.182545,
                "station_lng": 5.999088
            }
        },
        {
            "id": 21,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-21",
                "station_lat": 46.185722,
                "station_lng": 5.997623
            }
        },
        {
            "id": 22,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-22",
                "station_lat": 46.17284,
                "station_lng": 6.002612
            }
        },
        {
            "id": 23,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-23",
                "station_lat": 46.184319,
                "station_lng": 5.998923
            }
        },
        {
            "id": 24,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-24",
                "station_lat": 46.184345,
                "station_lng": 5.998887
            }
        },
        {
            "id": 25,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-25",
                "station_lat": 46.183109,
                "station_lng": 6.006879
            }
        },
        {
            "id": 26,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-26",
                "station_lat": 46.183361,
                "station_lng": 6.006943
            }
        },
        {
            "id": 27,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-27",
                "station_lat": 46.217918,
                "station_lng": 6.016133
            }
        },
        {
            "id": 28,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-28",
                "station_lat": 46.217934,
                "station_lng": 6.014958
            }
        },
        {
            "id": 29,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-29",
                "station_lat": 46.217575,
                "station_lng": 6.01465
            }
        },
        {
            "id": 30,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-30",
                "station_lat": 46.217575,
                "station_lng": 6.01465
            }
        },
        {
            "id": 31,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-31",
                "station_lat": 46.184528,
                "station_lng": 5.999068
            }
        },
        {
            "id": 32,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-32",
                "station_lat": 46.183529,
                "station_lng": 6.006996
            }
        },
        {
            "id": 33,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-33",
                "station_lat": 46.183769,
                "station_lng": 6.007091
            }
        },
        {
            "id": 34,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-34",
                "station_lat": 46.183132,
                "station_lng": 6.006629
            }
        },
        {
            "id": 35,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-35",
                "station_lat": 46.183407,
                "station_lng": 6.006712
            }
        },
        {
            "id": 36,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-36",
                "station_lat": 46.183582,
                "station_lng": 6.006712
            }
        },
        {
            "id": 37,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-37",
                "station_lat": 46.183762,
                "station_lng": 6.006895
            }
        },
        {
            "id": 38,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-38",
                "station_lat": 46.183144,
                "station_lng": 6.006361
            }
        },
        {
            "id": 39,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-39",
                "station_lat": 46.183449,
                "station_lng": 6.006433
            }
        },
        {
            "id": 40,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-40",
                "station_lat": 46.183567,
                "station_lng": 6.006497
            }
        },
        {
            "id": 41,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-41",
                "station_lat": 46.183796,
                "station_lng": 6.006625
            }
        },
        {
            "id": 42,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },
            "properties": {
                "station_name": "st-42",
                "station_lat": 46.183132,
                "station_lng": 6.007131
            }
        }
    ]
}

That’s great because I see now all the markers, but in reallity I see only one marker because all markers are at the same place. This is because ‘geometry’ has alway the same value

"geometry": {
                "type": "Point",
                "coordinates": [
                    6.000251769184164,
                    46.19337852885697
                ]
            },

I do not understand how the following works

def save(self, *args, **kwargs):
        self.location = Point(float(self.station_lng), float(self.station_lat))
        super(Stations, self).save(*args, **kwargs)

When the save() is called? Is should update the ‘location’ field with the station_lat and station_lng value, isn’t? Apparently, that does not work.

How can I update the ‘location’ field? By running ./manager.py migrate?

The save method is called when the instance is being saved. If you’re not adding a new instance or changing an existing instance, this doesn’t get called.

You can either re-import your data, or you can re-save all your existing instances from within the Django shell. (An automatically-generated migration isn’t going to do this for you - you’re not changing the structure of the table. You could create a manual migration if you wanted to approach it that way - but that seems like more work than it’s worth.)

Ok, I have an admin page and I can edit my stations. I observed if I modify a value, the location filed us updated.

No worries, I will do for each of them. That action will be done once…

Here is the result


Great, I am very happy but still a lot to do :slight_smile:
Many thanks again