If I already have existing MultiString cordinates in a file(as a list) or memory or in a json format, How do I override the save function to save these points instead of the user creating the object on a map? Or perhaps more precisely, how do i load these points to a model and the database?
Coordinates in memory/file
[[36.66678428649903, -1.5474249907643578], [36.670904159545906, -1.542620219636788], [36.66635513305665,-1.5353272427374922],[36.662406921386726, -1.5403894293513378]]
models.py
class LogsUpload(models.Model):
name = models.CharField(max_length=240)
geom = gis_models.MultiLineStringField(blank=True, null=True)
How do I use the cordinates and save them in the geom field?
Use PointField
of gis.db for storing [lat, long]
data.
from django.contrib.gis.db import models as gis_model
geom = gis_model.PointField(null=True, spatial_index=True, geography=True)
PointField accept GEOSGeometry object, so you can create your data like.
import json
from django.contrib.gis.geos import GEOSGeometry
data_coordinates = [[36.66678428649903, -1.5474249907643578], [36.670904159545906, -1.542620219636788], [36.66635513305665,-1.5353272427374922],[36.662406921386726, -1.5403894293513378]]
for coordinate in data_coordinates:
point = {
"type": "Point",
"coordinates": coordinate
}
LogsUpload.objects.create(name="your location name", geom=GEOSGeometry(json.dumps(point)))
Read more: Storing, querying & serializing location data with Django using PostGIS
UPDATE
If you want to save all of them in one object of LogsUpload
model, you can simply use ArrayField
or JSONField
of django.db.models