0

I have a webapp in django 3 & ol6.

My views.py is like this

   def highlightboundary(request):  
    searchArr = []
    output = {}
        
    dist = District.objects.filter(created_on__year__in=[2020,2021])
    
    for d in dist:
        searchArr.append({'type': 'Feature',
                  'properties': {'id': d.id, 'code': d.code},
                  'geometry': {'type': d.the_geom.geom_type, 'coordinates': d.the_geom.geojson}}) 
                      
    output = {'type': 'FeatureCollection', 'features': searchArr}
    return JsonResponse(output) 

And my openlayer code is:

window.highltSubmit = function highltSubmit(event) {
  $.post('/webapp/highlight/',function(data){  //Returns data from the above view
     dist_highlt = new VectorLayer({
          title:'Boundary Highlight',
          source: new VectorSource({
            features: new GeoJSON().readFeatures(data, {
                dataProjection: 'EPSG:32643',
                featureProjection: 'EPSG:32643',
            }),
          }),
          style: new Style({
            stroke: new Stroke({
                color: '#319FD3',
                width: 2,
              }),
          fill: new Fill({
            color: 'rgba(255, 255, 255, 0.6))',
          }),
          }),
          opacity: 0.5,
        });
     map.addLayer(dist_highlt);
  });
 }
}

The view highlightboundary return data to the above post function. But my problem is the polygon(multipolygon) is not hightlighting or color is not filling inside the polygon. if its a point field,it works fine. Whats wrong in my code.

And I am getting error

Uncaught TypeError: f.getLayout is not a function MultiPolygon.js:94:27

Adithya
  • 1,687
  • 3
  • 19
  • 34
  • 1
    What is `d.the_geom.geojson`? coordinates should be a coordinates array, not a geojson abject. – Mike Feb 20 '21 at 14:13
  • @Mike ***the_geom*** is the multipolygon geometry coordinate. I tried to append to array like this ***list(d.the_geom)*** but getting error ***TypeError: Object of type MultiPolygon is not JSON serializable***. How should I return it & read the data in ol – Adithya Feb 24 '21 at 04:20
  • If `data` is valid GeoJSON, snd consistent with the non-highlighted data, the highlight openlayers code posted above works for me: [mcve](http://www.geocodezip.com/OL_6.5.0_geojson_multiPolygon_highlight.html) – geocodezip Feb 27 '21 at 05:03
  • @geocodezip this openlayer code works but there is some problem with my python-django return format. **boundary = serialize('geojson', dist) return HttpResponse(boundary, content_type='JSON')** . If I return in this format,the above ol code works fine. – Adithya Mar 01 '21 at 06:44

0 Answers0