ykzz-web/src/views/Home/MapCtrl/M2D/layers/contourlayerLoop.js

184 lines
4.1 KiB
JavaScript

import clone from "clone";
import polylabel from "polylabel";
import POILayer2D from "./POILayer2D";
import Style from "ol/style/Style";
import FillStyle from "ol/style/Fill";
import {zindex2d} from "../../zindex";
import TextStyle from "ol/style/Text";
import StrokeStyle from "ol/style/Stroke";
const SourceName = '等值线';
const LabelSourceName = '等值线Label'
const ShapeStyle = {
id: SourceName,
type: 'fill-extrusion',
source: SourceName,
layout: {
},
paint: {
/*
'fill-extrusion-color': [
'case',
['>', ['get', 'z1'], 0], ['get', 'fill'],
'#020315'
],
*/
'fill-extrusion-color': ['get', 'fill'],
'fill-extrusion-height': ['*', 50, ['get', 'z1']],
'fill-extrusion-opacity': 0.8,
},
};
const ShapeLabelStyle = {
id: LabelSourceName,
type: 'symbol',
source: LabelSourceName,
layout: {
'text-size': 12,
'text-font': ['Roboto Black'],
'text-field': ['get', 'value'],
// 'text-offset': [0, 0.6],
'text-anchor': 'center',
'text-max-width': 6
},
'paint': {
'text-color': '#000',
'text-halo-color': '#fff',
'text-halo-width': 1
}
};
function calLabel(f, field, geojson) {
const geometry = f.geometry;
const label = f?.properties?.[field];
if (!label) {
return;
}
let coords = [];
if (geometry.type === 'Polygon') {
if (geometry.coordinates?.length) {
coords.push(geometry.coordinates);
}
} else if (geometry.type === 'MultiPolygon') {
for (const p of geometry.coordinates) {
if (p?.length) {
coords.push(p);
}
}
}
for (const c of coords) {
var p = polylabel(c, 1.0);
if (Array.isArray(p) && p[0] && p[1]) {
geojson.features.push({
type: 'Feature',
properties: { value: label },
geometry: {
type: 'Point',
coordinates: p,
}
})
}
}
}
export default class ContourlayerLoop extends POILayer2D {
static LayerName = 'ContourLayerLoop';
static SourceName = SourceName;
static LabelSourceName = LabelSourceName;
constructor(props, map) {
super(props);
this._style1 = new Style({
fill: new FillStyle({
color: '#C7DFBB',
}),
zIndex: zindex2d.sx,
});
this._styleLabel = {};
this._styleC = {};
this._data = props.setting;
}
getLayerName() {
return ContourlayerLoop.LayerName;
}
async dataPromise() {
/*const { shp, shps, index, label } = this._data?.contour || {};
let data = shp || (shps || [])[index];
if (!data) {
data = { type: 'FeatureCollection', features: [] };
}
const features = data.features || [];
if(features.length>0){
features.forEach(o => {
this._styleC[o.properties.z1] = new Style({
fill: new FillStyle({
color: o.properties.fill,
}),
//zIndex: zindex2d.sx,
});
});
}
const labelObj = { type: 'FeatureCollection', features: [] };
for (const f of features) {
calLabel(f, label, labelObj);
}
let newArray = [];
if(labelObj.features.length>0){
labelObj.features.map(o=>{
newArray.push(
{
"NAME": o.properties.value,
"level": 3,
"lgtd": o.geometry.coordinates[0],
"lttd": o.geometry.coordinates[1],
"type": "label"
}
);
});
}
const labelData = newArray;
if(labelData.length>0){
labelData.forEach(o => {
this._styleLabel[o.NAME] = new Style({
text: new TextStyle({
text: o.NAME,
font: 'bold 14px 微软雅黑',
fill: new FillStyle({ color: '#000' }),
stroke: new StrokeStyle({ color: '#fff', width: 1 }),
overflow: true,
zIndex: o.level,
}),
})
});
};
return [...features,...labelData];*/
}
styleFunc = ({ props }) => {
if (!this.isVisible()) {
return null;
}
if (props.type === 'label') {
return this._styleLabel[props.NAME];
}else{
return this._styleC[props.properties.z1];
}
}
}