将LineString geom转储到Django中的Point geom(与ST_DumpPoints()一样)

时间:2013-05-28 20:39:27

标签: django geometry postgis geos

摘要问题:如何将LineString geom转储到Django中的Point geom?或者是原始SQL唯一的方法吗?

想要了解更多详情......请点击此处:

假设我有以下型号,确切的格式并不重要。

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)

在填充视图(填充数据库)中,line_path被插入到line_paths表中并最终看起来像这样(给定以下查询)

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

Query Results

现在我想从此表中获取LineString GEOM(line_paths)并将这些点转储到新表(point_paths)中。在原始SQL中,我做了类似的事情:

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

然后我可以将这些点插入新表中。有没有在Django框架内执行此操作的方法? (不会回到原始SQL)

1 个答案:

答案 0 :(得分:0)

我相信我找到了解决方案。这么简单。

请参阅Django文档

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

由于geom对象是pythonic,我不需要访问模型/数据库来转储LineString。而是将其分解为协调点并批量插入那些。

如果我有一个LineString:

geom = GEOSGeometry(geojson.dumps(path))

该LineString的坐标为:

geom.coords

返回一个元组元组((x,y),(x,y),...)