簡體   English   中英

Openlayers - 根據屬性設置矢量圖層的樣式

[英]Openlayers - Set style of a vector layer according to an attribute

我創建了一個具有不同矢量圖層的 openlayers 地圖。 現在我想根據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 and <=1 因為它是最大可能值)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM