2012-09-10 35 views
0

我正在使用Django,geodjango和postgresql的字段数据构建数据库。数据包括lats和lons。我的任务之一是摄取已收集的数据。我想使用.json文件来定义元数据并编写一些代码来批处理一些json文件。json to geoDjango model

我有什么到目前为止,模型:

class deployment(models.Model):                   
    '''                         
    @brief This is the abstract deployment class.               
     '''                         
     startPosition=models.PointField()                  
     startTimeStamp=models.DateTimeField()                 
     endTimeStamp=models.DateTimeField()                 
     missionAim=models.TextField()                   
     minDepth=models.FloatField() # IT seems there is no double in Django         
     maxDepth=models.FloatField()                   



class auvDeployment(deployment):                   
    '''                         
    @brief AUV meta data                     
    '''                         
    #==================================================#             
    # StartPosition : <point>                    
    # distanceCovered : <double>                   
    # startTimeStamp : <dateTime>                   
    # endTimeStamp : <dateTime>                   
    # transectShape : <>                     
    # missionAim : <Text>                     
    # minDepth : <double>                     
    # maxDepth : <double>                     
    #--------------------------------------------------#             
    # Maybe need to add unique AUV fields here later when             
    # we have more deployments                   
    #==================================================#             

    transectShape=models.PolygonField()                 
    distanceCovered=models.FloatField()  

我的功能我想用摄取数据

@staticmethod                       
def importDeploymentFromFile(file):                 
    '''                        
    @brief This function reads in a metadta file that includes campaign information. Destinction between deployment types is made on the fine name. <type><deployment>.<supported text> auvdeployment.json 
    @param file The file that holds the metata data. formats include .json todo:-> .xml .yaml  
    '''                        
    catamiWebPortal.logging.info("Importing metadata from " + file)         
    fileName, fileExtension = os.path.splitext(file)             

    if fileExtension == '.json':                  
     if os.path.basename(fileName.upper()) == 'AUVDEPLOYMENT':          
      catamiWebPortal.logging.info("Found valid deployment file")        
      data = json.load(open(file))                
      Model = auvDeployment(**data)                
      Model.save()   

,我试图在这个

读取文件
{ 
"id":1, 
"startTimeStamp":"2011-09-09 13:20:00", 
"endTimeStamp":"2011-10-19 14:23:54", 
"missionAim":"for fun times, call luke", 
"minDepth":10.0, 
"maxDepth":20.0, 
"startPosition":{{"type": "PointField", "coordinates": [ 5.000000, 23.000000 ] }}, 
"distanceCovered":20.0 
} 

的错误,我得到的是这种

TypeError: cannot set auvDeployment GeometryProxy with value of type: <type 'dict'> 

如果我从模型和文件中删除地理类型。它将读取文件并填充数据库表。

我将不胜感激任何建议之一,我如何解析数据文件与geotypes。

感谢

回答

0

我建议你使用默认的命令加载夹具:loaddata

python manage.py loaddata path/to/myfixture.json ... 

您的JSON结构将不得不略低调整,但你可以做一个简单的dumpdata怎么看结构应该看起来像。

0

好的解决方案如下。文件格式不是geoJSON文件格式,而是geos格式。 .json文件应如下所示。

{ 
"id": 1, 
"startTimeStamp": "2011-10-19 10:23:54", 
"endTimeStamp":"2011-10-19 14:23:54", 
"missionAim": "for fun times, call luke", 
"minDepth":10.0, 
"maxDepth":20.0, 
"startPosition":"POINT(-23.15 113.12)", 
"distanceCovered":20, 
"transectShape":"POLYGON((-23.15 113.12, -23.53 113.34, -23.67 112.9, -23.25 112.82, -23.15 113.12))" 
} 

不是中StartPosition语法发生了变化。

1

在保存模型之前,快速解决方法是使用geoDjango中的GEO API将geoJson格式的startPosition字段更改为GEOSGeometry对象。这应该允许它通过验证。

包括从Django中与GEOSGeometry功能:

from django.contrib.gis.geos import GEOSGeometry 
... 

Model = auvDeployment(**data) 
Model.startPosition = GEOSGeometry(str(Model.startPosition)) 
Model.save() 

GEOS API cant construct objects from a GeoJSON format,只要你把它的字符串第一。就目前而言,您正在将其作为字典类型而非字符串加载。