2013-05-28 47 views
0

摘要問題:如何將一個LineString geom轉儲到Django中的Point geom?或者是原始SQL唯一的方法?將一個LineString geom轉儲到Django中的一個Point geom(與ST_DumpPoints())相似

想要了解更多詳細信息...這裏亞去:

說我有以下的車型,確切的格式並不重要。

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) 

在人口視圖(填充DATABSE)的line_path插入line_paths表,並最終看起來是這樣的(假設下面的查詢)

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

Query Results

現在我想從這個表(line_paths)中取出LineString GEOM並將這些點轉儲到一個新表(point_paths)中。在原始的SQL我做了這樣的事情:

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

然後,我可以只插入到新表中的點。有沒有在Django框架內做這個的方法? (不退回原始SQL)

回答

0

我相信我找到了我的解決方案。的確很簡單。

見Django文檔

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

由於GEOM對象是Python的,我不需要訪問模型/數據庫轉儲LineString的。相反,只要把它分成協調點和批量插入點即可。

如果我有一個線段形式:

geom = GEOSGeometry(geojson.dumps(path)) 

該線段形式的座標是:

geom.coords 

這會返回一個元組的元組((X,Y),(X,Y), ...)