0

Summary Question: How can I dump a LineString geom to a Point geom in Django? Or is raw SQL the only way?

Want more details ... here ya go:

Say I have the following models, the exact format isn't important.

line_paths: 1 row = 1 linestring 
point_paths: 1 row = 1 point from 1 linestring in line_paths

##################################################
# LINE_PATHS TABLE
##################################################

class line_paths(models.Model):
    line_path_id = models.AutoField(primary_key=True)
    yyyymmdd = models.CharField(max_length=8)
    season = models.ForeignKey('seasons')
    line_path = models.LineStringField(dim=3)
    objects = models.GeoManager()
    location = models.CharField(choices=LOCATION,max_length=20)

    def __unicode__(self):
        return "%s-%d"%(self.location, self.line_path_id)

##################################################
# POINT_PATHS TABLE
##################################################

class point_paths(models.Model):
    point_path_id = models.AutoField(primary_key=True)
    season = models.ForeignKey('seasons')
    line_path = models.ForeignKey('line_paths')
    gps_time = models.FloatField()
    roll = models.FloatField()
    pitch = models.FloatField()
    heading = models.FloatField()
    point = models.PointField(dim=3)
    objects = models.GeoManager()
    location = models.CharField(choices=LOCATION,max_length=20)

    def __unicode__(self):
        return "%s-%d"%(self.location, self.point_path_id)

In the population view (filling the databse) the line_path is inserted into the line_paths table and ends up looking something like this (given the following query)

SELECT line_path_id,season_id,ST_AsText(line_path) FROM rds_line_paths;

Query Results

Now I want to take the LineString GEOM from this table (line_paths) and dump the points into a new table (point_paths). In raw SQL i'd did something like this:

SELECT ST_AsText(geom) AS points FROM ST_DumpPoints((SELECT fl_line FROM greenland.line_paths WHERE ...));

Then I could just insert the points into the new table. Is there a method for doing this within the Django framework? (Not falling back to raw SQL)

1 Answers1

0

I believe I found my solution. So simple indeed.

See the Django documentation

https://docs.djangoproject.com/en/1.3/ref/contrib/gis/geos/#geometries-are-pythonic

Since the geom object is pythonic I don't need to access the model/database to dump the LineString. Rather just break it into the point coordinated and bulk insert those.

If I have a LineString:

geom = GEOSGeometry(geojson.dumps(path))

The coordinates of that LineString are:

geom.coords

This returns a tuple of tuples ((x,y),(x,y),...)