zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React from 'react';
import './css/MapView.scss';
import Map from './components/Map';
import $ from 'jquery';
import PropTypes from 'prop-types';
import { _, interpolate } from './classes/gettext';
 
class MapView extends React.Component {
  static defaultProps = {
    mapItems: [],
    selectedMapType: 'auto',
    title: "",
    public: false,
    publicEdit: false,
    shareButtons: true,
    permissions: ["view"]
  };
 
  static propTypes = {
      mapItems: PropTypes.array.isRequired, // list of dictionaries where each dict is a {mapType: 'orthophoto', url: <tiles.json>},
      selectedMapType: PropTypes.oneOf(['auto', 'orthophoto', 'plant', 'dsm', 'dtm']),
      title: PropTypes.string,
      public: PropTypes.bool,
      publicEdit: PropTypes.bool,
      shareButtons: PropTypes.bool,
      permissions: PropTypes.array
  };
 
  constructor(props){
    super(props);
 
    let selectedMapType = props.selectedMapType;
 
    // Automatically select type based on available tiles
    // and preference order (below)
    if (props.selectedMapType === "auto"){
      let preferredTypes = ['orthophoto', 'dsm', 'dtm'];
      if (this.isThermalMap()) preferredTypes = ['plant'].concat(preferredTypes);
 
      for (let i = 0; i < this.props.mapItems.length; i++){
        let mapItem = this.props.mapItems[i];
        for (let j = 0; j < preferredTypes.length; j++){
          if (mapItem.tiles.find(t => t.type === preferredTypes[j])){
            selectedMapType = preferredTypes[j];
            break;
          }
        }
        if (selectedMapType !== "auto") break;
      }
    }
 
    if (selectedMapType === "auto") selectedMapType = "orthophoto"; // Hope for the best
 
    this.state = {
      selectedMapType,
      tiles: this.tilesFromMapType(selectedMapType)
    };
 
    this.tilesFromMapType = this.tilesFromMapType.bind(this);
    this.handleMapTypeButton = this.handleMapTypeButton.bind(this);
    this.hasTilesOfType = this.hasTilesOfType.bind(this);
  }
 
  isThermalMap = () => {
    let thermalCount = 0;
    for (let item of this.props.mapItems){
      if (item.meta && item.meta.task && item.meta.task.orthophoto_bands){
        if (item.meta.task.orthophoto_bands.length === 2 && item.meta.task.orthophoto_bands &&
            item.meta.task.orthophoto_bands[0] && typeof(item.meta.task.orthophoto_bands[0].description) === "string" &&
            item.meta.task.orthophoto_bands[0].description.toLowerCase() === "lwir"){
          thermalCount++;
        }
      }
    }
 
    return thermalCount === this.props.mapItems.length;
  }
 
  tilesFromMapType(type){
    // Go through the list of map items and return 
    // only those that match a particular type (in tile format)
    const tiles = [];
 
    this.props.mapItems.forEach(mapItem => {
      mapItem.tiles.forEach(tile => {
        tiles.push({
          url: tile.url,
          meta: mapItem.meta,
          type: tile.type,
          selected: tile.type === type
        });
      });
    });
 
    return tiles;
  }
 
  hasTilesOfType(type){
    for (let i = 0; i < this.props.mapItems.length; i++){
      let mapItem = this.props.mapItems[i];
      for (let j = 0; j < mapItem.tiles.length; j++){
        let tile = mapItem.tiles[j];
        if (tile.type === type) return true;
      }
    }
    return false;
  }
 
  handleMapTypeButton(type){
    return () => {
      this.setState({
        selectedMapType: type,
        tiles: this.tilesFromMapType(type)
      });
    };
  }
 
  render(){
    const isThermal = this.isThermalMap();
 
    let mapTypeButtons = [
      {
        label: _("Orthophoto"),
        type: "orthophoto",
        icon: "far fa-image"
      },
      {
        label: isThermal ? _("Thermal") : _("Plant Health"),
        type: "plant",
        icon: isThermal ? "fa fa-thermometer-half" : "fa fa-seedling"
      },
      {
        label: _("Surface Model"),
        type: "dsm",
        icon: "fa fa-chart-area"
      },
      {
        label: _("Terrain Model"),
        type: "dtm",
        icon: "fa fa-chart-area"
      }
    ].filter(mapType => this.hasTilesOfType(mapType.type));
 
    // If we have only one button, hide it...
    if (mapTypeButtons.length === 1) mapTypeButtons = [];
 
    return (<div className="map-view">
        <div className="map-view-header">
          {this.props.title ?
            <h3 className="map-title" title={this.props.title}><i className="fa fa-globe"></i> {this.props.title}</h3>
          : ""}
 
          <div className="map-type-selector btn-group" role="group">
            {mapTypeButtons.map(mapType =>
              <button
                key={mapType.type}
                onClick={this.handleMapTypeButton(mapType.type)}
                title={mapType.label}
                className={"btn btn-sm " + (mapType.type === this.state.selectedMapType ? "btn-primary" : "btn-default")}><i className={mapType.icon + " fa-fw"}></i><span className="hidden-sm hidden-xs"> {mapType.label}</span></button>
            )}
          </div>
        </div>
      
        <div className="map-container">
            <Map 
                tiles={this.state.tiles} 
                showBackground={true} 
                mapType={this.state.selectedMapType} 
                public={this.props.public}
                publicEdit={this.props.publicEdit}
                shareButtons={this.props.shareButtons}
                permissions={this.props.permissions}
                thermal={isThermal}
            />
        </div>
      </div>);
  }
}
 
$(function(){
    $("[data-mapview]").each(function(){
        let props = $(this).data();
        delete(props.mapview);
        window.ReactDOM.render(<MapView {...props}/>, $(this).get(0));
    });
});
 
export default MapView;