Skip to content

Commit 97e4110

Browse files
authored
show coordinates in the map for projected and geographic 2D (#51)
1 parent 0b4272b commit 97e4110

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

scripts/templates/base.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ padding: 10px;
107107
max-width: 90%;
108108
}
109109

110+
#map_coord_container {
111+
display: flex;
112+
flex-direction: row;
113+
justify-content: flex-start;
114+
flex-wrap: wrap;
115+
}
116+
110117
.map-container {
111118
width:512px;
112119
height:256px;
@@ -116,6 +123,19 @@ padding: 10px;
116123
border: 2px solid darkblue;
117124
}
118125

126+
#coordinates {
127+
margin:10px;
128+
}
129+
#coordinates .number {
130+
text-align: end;
131+
}
132+
#coordinates table {
133+
padding-bottom: 1em;
134+
}
135+
#drag_marker {
136+
color: gray;
137+
}
138+
119139
li {
120140
clear:left;
121141
}

scripts/templates/base.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
function init_map(area_of_use) {
1+
async function init_map(area_of_use, crs_type, id) {
22
if (!area_of_use) return;
3-
let map = L.map('map').setView([0, 0], 1);
3+
let map = L.map('map');
44
let osm = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
55
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
66
maxZoom: 18,
77
}).addTo(map);
88
let rect = makeRectangle(area_of_use, 'green').addTo(map);
99
map.fitBounds(rect.getBounds());
10+
if (crs_type === 'PROJECTED_CRS' || crs_type === 'GEOGRAPHIC_2D_CRS') {
11+
const decimals = crs_type === 'PROJECTED_CRS' ? 1 : 6;
12+
const proj = new Proj();
13+
await proj.init();
14+
const transformer = proj.create_transformer_from_crs({source_crs: 'EPSG:4326', target_crs: id});
15+
const axes = proj.crs_axes({crs: id});
16+
document.querySelector('#coordinates table tr:nth-child(1) .name').innerText = axes[0].name + ': ';
17+
document.querySelector('#coordinates table tr:nth-child(2) .name').innerText = axes[1].name + ': ';
18+
19+
const marker = L.marker(rect.getCenter(), {draggable: true}).addTo(map);
20+
marker.on('move', (e) => {
21+
let res = [[NaN, NaN]];
22+
try {
23+
res = transformer.transform({points: [[e.latlng.lat, e.latlng.lng]]});
24+
} catch (_e) {}
25+
document.querySelector('#coordinates table tr:nth-child(1) .number').innerText = res[0][0].toFixed(decimals);
26+
document.querySelector('#coordinates table tr:nth-child(2) .number').innerText = res[0][1].toFixed(decimals);
27+
});
28+
marker.setLatLng(rect.getCenter());
29+
marker.once('move', (e) => {
30+
console.log(proj.proj_info());
31+
document.getElementById('drag_marker').classList.add('hidden');
32+
});
33+
}
1034
}
1135

1236
function makeRectangle (area_of_use, color) {

scripts/templates/crs.tmpl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,26 @@
9898
</div>
9999

100100
{%- if bounds_map %}
101+
<div id="map_coord_container">
101102
<div id="map" class="map-container"></div>
103+
{%- if crs_type in ['PROJECTED_CRS', 'GEOGRAPHIC_2D_CRS'] %}
104+
<div id="coordinates">
105+
<div>Marker coordinates <span id="drag_marker">(Drag to update)</span></div>
106+
<table>
107+
<tr>
108+
<td class="name"></td>
109+
<td class="number"></td>
110+
</tr>
111+
<tr>
112+
<td class="name"></td>
113+
<td class="number"></td>
114+
</tr>
115+
</table>
116+
<a href="https://jjimenezshaw.github.io/wasm-proj/transform.html?sh=EPSG%3A4326&th={{ authority }}%3A{{ code }}"
117+
target="_blank" >Transform coordinates</a>
118+
</div>
119+
</div>
120+
{%- endif %}
102121
{%- endif %}
103122
</div>
104123

@@ -107,8 +126,13 @@
107126
</div>
108127

109128
<script src="{{ home_dir }}/base.js"></script>
129+
{%- if crs_type in ['PROJECTED_CRS', 'GEOGRAPHIC_2D_CRS'] %}
130+
<script src="https://jjimenezshaw.github.io/wasm-proj/proj/wasm/projModule.js"></script>
131+
<script src="https://jjimenezshaw.github.io/wasm-proj/proj/projFunctions.js"></script>
132+
{%- endif %}
133+
110134
<script>
111-
init_map([{{ bounds_map }}])
135+
init_map([{{ bounds_map }}], '{{ crs_type }}', '{{ authority }}:{{ code }}')
112136
</script>
113137
</body>
114138
</html>

0 commit comments

Comments
 (0)