2017-02-24 70 views
0

我使用OpenLayers在地圖上創建,顯示和編輯功能。功能在Django中保存爲JSONField。後保存我添加一個關鍵字django_pk到JSON。我怎樣才能直接從該功能得到這個djanog_pk?閱讀GeoJSON的鍵值和na來自Openlayers的功能

我需要這樣做,所以當功能編輯時,我知道什麼功能在Django更新。

我的JS代碼如下:

<script> 
     var raster = new ol.layer.Tile({ 
     source: new ol.source.OSM() 
     }); 

     var source = new ol.source.Vector({wrapX: false}); 

     var vector = new ol.layer.Vector({ 
     source: source 
     }); 

     var format = new ol.format.GeoJSON(); 

     var select = new ol.interaction.Select({ 
     wrapX: false 
     }); 

     var modify = new ol.interaction.Modify({ 
     features: select.getFeatures() 
     }); 



     var map = new ol.Map({ 
     interactions: ol.interaction.defaults().extend([select, modify]), 
     layers: [raster, vector], 
     target: 'map', 
     view: new ol.View({ 
      center: [-11000000, 4600000], 
      zoom: 4 
     }) 
     }); 

     var features = new ol.source.Vector({ 
      projection: 'EPSG:4326' 
     }); 

     {% for polygon in polygons.0.gates %} 
     console.log(format.readFeature({{ polygon|safe }}).getProperties())) 
     {% endfor %} 

     features.addFeature(format.readFeature({{ polygons.0.protected_area|safe }})); 
     {% for polygon in polygons.0.gates %} 
     features.addFeature(format.readFeature({{ polygon|safe }})); 
     console.log(format.readFeature({{ polygon|safe }})) 
     {% endfor %} 

     var featureOverlay = new ol.layer.Vector({ 
     source: features, 
     style: new ol.style.Style({ 
      fill: new ol.style.Fill({ 
      color: 'rgba(255, 255, 255, 0.2)' 
      }), 
      stroke: new ol.style.Stroke({ 
      color: '#ff78d1', 
      width: 2 
      }), 
      image: new ol.style.Circle({ 
      radius: 6, 
      fill: new ol.style.Fill({ 
       color: '#4ca6b6' 
      }), 
      }) 
     }) 
     }); 
     featureOverlay.setMap(map); 


</script> 

我嘗試這樣做:Buyt它只返回幾何:

select.on( '選擇',函數(E){ 的console.log( e.selected [0] .getProperties()) });

的JSONField在Django看起來是這樣的:

{ 
    "geometry":{ 
    "type":"Polygon", 
    "coordinates":[ 
     [ 
     [ 
      -11156543.033928039, 
      6698655.0485978 
     ], 
     [ 
      -11410925.464061106, 
      5896371.999716589 
     ], 
     [ 
      -9972686.33984723, 
      5084305.011214877 
     ], 
     [ 
      -9512841.177683609, 
      6649735.350495286 
     ], 
     [ 
      -10090093.61529326, 
      6972605.357971871 
     ], 
     [ 
      -11156543.033928039, 
      6698655.0485978 
     ] 
     ] 
    ] 
    }, 
    "type":"Feature", 
    "properties":null, 
    "django_pk":10 
} 

所以我需要編輯它時,它選擇它,OT刪除它找到一個特徵django_pk。

不知道如何獲取此信息

回答

0

好的,我其實很簡單。

您可以閱讀GeoJSON格式導出的任何屬性:

select.on('select', function(e) { 
    console.log(e.selected[0].getProperties()) 
}); 

選擇之中:

var select = new ol.interaction.Select({ 
     wrapX: false 
     }); 

在我的Django代碼,我只是做這個post_save:

@receiver(post_save, sender=ProtectedArea) 
def update_json(sender, instance, created, **kwargs): 
    post_save.disconnect(update_json, sender=ProtectedArea) 
    try: 
     instance.polygone = literal_eval("%s" % instance.geojson_file.read()) 
    except: 
     pass 

    if instance.polygone['properties'] == None: 
     instance.polygone['properties'] = {} 
     instance.polygone['properties']['pk'] = instance.pk 
    else: 
     instance.polygone['properties']['pk'] = instance.pk 
    instance.save() 
    post_save.connect(update_json, sender=ProtectedArea)