Reputation: 93
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?
Upvotes: 4
Views: 3336
Reputation: 6865
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
Upvotes: 2
Reputation: 30532
Use LineString
and MultiLineString
classes from the GEOS API to create lines and multi-lines in memory, and assign them to your model's fields:
>>> from django.contrib.gis.geos import LineString, MultiLineString
>>> DATA = [[36.66678428649903, -1.5474249907643578], [36.670904159545906, -1.542620219636788], [36.66635513305665,-1.5353272427374922],[36.662406921386726, -1.5403894293513378]]
>>> line = LineString(DATA)
>>> line
<LineString object at 0x7f96538f49a0>
>>> line.length
0.02134446840090856
>>> line.geojson
'{ "type": "LineString", "coordinates": [ [ 36.666784286499031, -1.547424990764358 ], [ 36.670904159545906, -1.542620219636788 ], [ 36.666355133056648, -1.535327242737492 ], [ 36.662406921386726, -1.540389429351338 ] ] }'
>>> multi_line = MultiLineString(line)
>>> multi_line
<MultiLineString object at 0x7f96536ccbc0>
>>> multi_line[0]
<LineString object at 0x7f964187b3c8>
>>> multi_line.length
0.02134446840090856
>>> multi_line.geojson
'{ "type": "MultiLineString", "coordinates": [ [ [ 36.666784286499031, -1.547424990764358 ], [ 36.670904159545906, -1.542620219636788 ], [ 36.666355133056648, -1.535327242737492 ], [ 36.662406921386726, -1.540389429351338 ] ] ] }'
>>> from myapp.models import LogsUpload
>>> o = LogsUpload(name="foo", geom=multi_line)
>>> o.full_clean()
>>> o.save()
Tested with Django 2.0 and PostGIS.
Upvotes: 3