[Answer]-GeoDjango create model with LineStringField with buffer

1👍

So after digging and rethinking all day long I’ve come up with some implementation ideas:

  1. Instead of adding buffer to each LineString which will make it a
    Polygon (waste of data space?) and perform a point search for a relevant Polygon with a
    given Point, after going over again on PostGIS ST_Buffer’s docs
    (Geom.buffer on GeoDjango) I understood a better (and faster) use for future queries would be
    with PostGIS ST_DWithin ( <field>__<distance
    lookup>=(<geometry>, <distance value>[, 'spheroid'])
    on
    GeoDjango GeoQuerySets) and by that storing a Segment or street in this case would be with LineString.
  2. Thanks to this post, I’ve come up with this piece of code:

    class Sign(models.Model):
        name = models.CharField(max_length=10)
        gps_point = models.PointField()
        objects = models.GeoManager()
    
        def __unicode__(self):
            return self.name
    
    class Segment(models.Model):
        start_sign = models.ForeignKey(Sign, related_name='seg_start')
        end_sign = models.ForeignKey(Sign, related_name='seg_end')
        line = models.LineStringField(blank=True, null=True)
        objects = models.GeoManager()
    
        def save(self):
            super(Segment,self).save()
            if not self.line and self.start_sign and self.end_sign:
                self.line = LineString(self.start_sign.gps_point,self.end_sign.gps_point)
                self.save()
    

    so by overriding save() I could assign LineString(self.start_sign.gps_point,self.end_sign.gps_point) to LineStringField once save is done.
    Now all I have to deal with is assigning a start sign for segment’s start point and end sign for segment’s ending and the LineString object will be created for me 🙂

  3. So now I can just filter out the relevant street/segment just by Segment.objects.filter(line__distance_lte=(Point(lat,lon),D(m=required_distance)).

Cheers

👤devdc

Leave a comment