1

I'm a beginner in GIS developing and this might be a simple question. I'm using Python Django equipped with GeoDjango. Where I use Django-leaflet and database PostGreSQL with PostGIS.

I already have data in my model (Model A) where each element is a PointField. I now want to create a table that has a LineString. How do I create Model B that refers to my Model A?

class ModelA(models.Model):
   pointid = models.CharField(max_length=25)
   name = models.CharField(max_length=200)
   geom - gismodels.PointField()
   object = gismodels.GeoManager()

class ModelB(models.Model):
   name = models.CharField(max_length=200)
   line_geom = gismodels.LineStringField(null=True)
   color = RGBColorField(null=True)
   object = gismodels.GeoManager()

I assume it is mapped by FK, but I'm quite perplexed as how this must be created given that in a linestring it is a group of PointField from Model A?

The sequence of points are important. What I currently have is a file that states the sequence of these points (which refers to the id of those in Model A) and I'm not quite sure how to pass this data to the model as I read it.

Example:

id || sequence || pointFrom || pointTo

8801 || 1 || point01id || point02id

8801 || 2 || point02id || point03id

8801 || 3 || point03id

Edit: I'm looking at doing this code but yeah I still am missing what to do with the relationship on using the FK from Model A.

>>> import psycopg2
>>> conn = psycopg2.connect(dbname=..., port=..., user=...,
                            password=..., host=...)

>>> cur.execute("SELECT (???) (ST_MakeLine(ARRAY[ST_MakePoint(???)]);")
>>> cur.fetchall()
1

You can do it if they have a relation to each other.

First add a foreignkey Field to the child model. That will point to the parent model.

To update the linestring you can select the related child data, directly from the parent, making use of the related set, created automatically by Django.

Example:

class ParentModel():
    name = models.CharField()
    geom = models.LinestringField()

class ChildModel():
    parent = models.ForeignKey(ParentModel)
    order= models.IntegerField()
    geom = models.PointField()

You will need to access all instances of ChildModel that belong to a certain instance of ParentModel, iterate over them, and construct a new polyline.

Using Django's related set attribute (this is created for you, when you define a ForeignKey) on the ParentModel, you can get all childs that belong to a certain parent. Like so:

childs = parent.childmodel_set.all()

You can order the childs, like this:

childs = parent.childmodel_set.all().orderby('order')

With those in hand, you will be able to iterate over them and construct a new polyline, so you can set it on the Parent.

# there are many ways to achieve this. you can use a for, for example
# or a list comprehension, shown below
linestring = LineString([child.geom for child in childs])
parent.geom = linestring
parent.save()

You need to checkout Django's documentation about relations, on how to define ForeignKeys and other relations. For the ordering, you can order the queryset (the return of the method all) using a new method called orderby. Refer to the docs to see the full idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.