-
Notifications
You must be signed in to change notification settings - Fork 0
/
projection.jsx
154 lines (138 loc) · 4.63 KB
/
projection.jsx
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
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import XYZ from 'ol/source/XYZ.js';
import Overlay from 'ol/Overlay.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import {Circle, Fill, Stroke, Style} from 'ol/style.js';
import {toStringHDMS} from 'ol/coordinate.js';
import { Vector as VectorLayer } from 'ol/layer';
import { Vector as VectorSource } from 'ol/source';
import { GeoJSON } from 'ol/format';
import proj4 from 'proj4';
import {get as getProjection, transformExtent, fromLonLat, toLonLat} from 'ol/proj.js';
import {register} from 'ol/proj/proj4.js';
let displayExtent;
// Mollweide Equi-distant
// const projCode = 'EPSG:54009'
// const projDefs = '+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 ' + '+units=m +no_defs'
// displayExtent = [-18e6, -9e6, 18e6, 9e6]
// Equidistant Cylindrical
const projCode = 'ESRI:53002'
const projDefs = '+proj=eqc +lat_ts=60 +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R=6371000 +units=m +no_defs +type=crs'
displayExtent = [-18e6, -9e6, 18e6, 9e6]
// Behrmann Equal Area
// const projCode = 'ESRI:54017'
// const projDefs = '+proj=cea +lat_ts=30 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs'
// Albers Equal Area
// const projCode = 'EPSG:9822'
// const projDefs = '+proj=lcc +lat_0=42 +lon_0=3 +lat_1=41.25 +lat_2=42.75 +x_0=1700000 +y_0=1200000 +ellps=GRS80+towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs'
// displayExtent = [-10000000, -10000000, 10000000, 10000000]
proj4.defs(
projCode,
projDefs
);
register(proj4);
const displayProj = getProjection(projCode);
if (!Array.isArray(displayExtent)) {
const worldExtent4326 = [-180, -90, 180, 90];
displayExtent = transformExtent(worldExtent4326, 'EPSG:4326', displayProj);
}
// const layer = new TileLayer({
// source: new OSM(),
// });
const layer = new TileLayer({
source: new XYZ({
url: 'https://cawm.lib.uiowa.edu/tiles/{z}/{x}/{y}.png',
}),
});
const map = new Map({
layers: [layer],
target: 'map',
view: new View({
projection: displayProj,
center: fromLonLat([0, 0]),
zoom: 0,
extent: displayExtent
}),
});
const fill = new Fill({
color: 'rgba(0,0,0,0.4)',
});
const stroke = new Stroke({
color: 'black',
width: 1.25,
});
const markerStyle = [
new Style({
image: new Circle({
fill: fill,
stroke: stroke,
radius: 4,
}),
fill: fill,
stroke: stroke,
}),
];
// Load GeoJSON file
fetch('/data/hacked_places.json')
.then(response => response.json())
.then(data => {
var features = new GeoJSON().readFeatures(data, {
// featureProjection: 'EPSG:3857'
featureProjection: projCode
});
var vectorSource = new VectorSource({
features: features
});
// Create a vector layer to hold the markers
var vectorLayer = new VectorLayer({
source: vectorSource,
style:markerStyle
});
map.addLayer(vectorLayer);
const container = document.getElementById('popup');
const content = document.getElementById('popup-content');
const closer = document.getElementById('popup-closer');
const overlay = new Overlay({
element: container,
positioning: 'center-center',
autoPan: {
animation: {
duration: 250,
},
},
});
closer.onclick = function () {
overlay.setPosition(undefined);
closer.blur();
return false;
};
map.addOverlay(overlay);
map.on('click', function(event) {
var feature = map.forEachFeatureAtPixel(event.pixel,
function(feature, layer) {
return feature;
});
if (feature) {
if (overlay.getPosition() !== undefined) {
overlay.setPosition(undefined);
}
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates();
var placeName = feature.get('title') || ''
var placeNameUrl = ''
if (placeName !== '') {
var placeNameUrl = `https://knowledgebase.sloanelab.org/resource/?uri=http%3A%2F%2Fsloanelab.org%2FE53%2F${placeName}`
}
var value = feature.get('value') || ''
var type = feature.get('type') || ''
var contentHtml = `<p><strong>${placeName}</strong></p><p>${value}</p><p>${type}</p>
<p><a href="${placeNameUrl}" target="_blank">Click to access ${placeName}</a>`
content.innerHTML =contentHtml
overlay.setPosition(coord)
} else {
overlay.setPosition(undefined)
}
});
});