javascript - Openlayers - 根据属性设置矢量图层的样式

标签 javascript json openlayers geojson layer

我创建了一个具有不同矢量图层的 openlayers map 。现在我想根据 geojson 中属性表的属性“AVG_UHVI_A”设置“hitzeLayer”的样式。 “AVG_UHVI_A”列中的属性具有0-1范围内的不同值,描述了多个位置的热指数。我用下面的代码尝试过,但没有成功。我对任何形式的支持都感到非常高兴。 :)

import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import Stamen from 'ol/source/Stamen';
import VectorLayer from 'ol/layer/Vector';
import Vector from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import Style from 'ol/style/Style';
import Circle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Overlay from 'ol/Overlay';
import {
  fromLonLat,
  toLonLat
} from 'ol/proj';
import sync from 'ol-hashed';
import OSM from 'ol/source/OSM';
import Feature from 'ol/Feature';
import {
  circular
} from 'ol/geom/Polygon';
import Point from 'ol/geom/Point';
import Control from 'ol/control/Control';
import * as olProj from 'ol/proj';
import XYZ from 'ol/source/XYZ';

// define the map
const map = new Map({
  target: 'map',
  view: new View({
    center: fromLonLat([16.37, 48.2]),
    zoom: 13
  })
});

sync(map);

//Adresssuche
const searchResultSource = new Vector();
const searchResultLayer = new VectorLayer({
  source: searchResultSource
});

searchResultLayer.setStyle(new Style({
  image: new Circle({
    fill: new Fill({
      color: 'rgba(0, 128, 0, 1)'
    }),
    stroke: new Stroke({
      color: '#000000',
      width: 1.25
    }),
    radius: 15
  })
}));

var element = document.getElementById('search');
element.addEventListener('keydown', listenerFunction);

function listenerFunction(event) {
  console.log(event);
  console.log(event.keyCode);
  if (event.keyCode === 13) {

    const xhr = new XMLHttpRequest;
    xhr.open('GET', 'https://photon.komoot.de/api/?q=' + element.value + '&limit=3');
    xhr.onload = function () {
      const json = JSON.parse(xhr.responseText);
      const geoJsonReader = new GeoJSON({
        featureProjection: 'EPSG:3857'
      });
      searchResultSource.clear();
      const features = geoJsonReader.readFeatures(json);
      console.log(features);
      searchResultSource.addFeatures(features);
      if (!searchResultSource.isEmpty()) {
        map.getView().fit(searchResultSource.getExtent(), {
          maxZoom: 18,
          duration: 500
        });
      }
    };
    xhr.send();


  }
}

//OpenStreetMap
const OSMbaseLayer = new TileLayer({
  type: 'base',
  source: new OSM()
});

// Statellit
const satellitLayer = new TileLayer({
  source: new XYZ({
    attributions: ['Powered by Esri', 'Source: Esri, DigitalGlobe, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community'],
    attributionsCollapsible: false,
    url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
    maxZoom: 30
  })
});

//shape
const parkLayer = new VectorLayer({
  source: new Vector({
    name: 'park',
    url: 'data/park1.geojson',
    format: new GeoJSON()
  })
});

parkLayer.setStyle(new Style({
  fill: new Fill({
    color: 'green'
  }),
  stroke: new Stroke({
    color: 'green',
    width: 1.25
  }),
}));

const hitzeLayer = new VectorLayer({
  source: new Vector({
    name: 'hitze',
    url: 'data/hitze.geojson',
    format: new GeoJSON()
  })
});

hitzeLayer.setStyle(function(feature) {
  let fillColor;
  const hitzeindex = feature.get('AVG_UHVI_A');
  if (hitzeindex <= 1) {
    fillColor = 'rgba(238, 233, 233, 0.7';
  } else if (hitzeindex < 0.75) {
    fillColor = 'rgba(205, 201, 201, 0.7)';
  } else {
    fillColor = 'rgba(139, 137, 137, 0.7)';
  }
  return new Style({
    fill: new Fill({
      color: fillColor
    }),
    stroke: new Stroke({
      color: 'rgba(4, 4, 4, 1)',
      width: 1
    })
  });
});

最佳答案

你说数据在0到1之间。

第一 if样式函数中的语句是 if value <= 1 ,因此您的所有功能都将采用第一种样式。

你会想要相反的方式:

if value < 0.5 then ..., else if value < 0.75 then ... else ... (else 为 >=0.75 且 <=1,因为它是最大可能值)

关于javascript - Openlayers - 根据属性设置矢量图层的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59601332/

相关文章:

javascript - 相对于窗口缩放 CSS

javascript - 如何从 JSON、PHP 或 javascript 中删除方括号?

javascript - 从 URL 显示由 decodeURIComponent 解码的 HTML 代码是否安全?

javascript - 如何在Leaflet中绘制天梭椭圆(天梭指示线)

javascript - 如何在javascript字符串之间附加一些东西?

javascript - 手动编辑嵌套数组

java - 在 Jackson 中将 4Mb JSON 转换为 java 对象需要 1500 毫秒

java - 如何根据屏幕可见情况将长 JSON 响应划分为模块以在 ListView 或 RecyclerView 中显示?

javascript - ol-ext Controller 中的控制栏和功能

javascript - Angular 总是选择 select 元素中的第一个选项