Skip to content

Commit dd7d143

Browse files
authored
use /getbounds endpoint (#23)
1 parent 6e249f2 commit dd7d143

2 files changed

Lines changed: 82 additions & 12 deletions

File tree

docs/static/js/index.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,28 @@ function init()
1616
const property_id = form.querySelector('#property_id');
1717
console.log('User entered:', token.value, property_id.value);
1818

19-
// create map and zoom in on farm
20-
// TODO: can we get extents programmatically?
21-
let map = L.map('map').setView([-31.349912533186938, 150.13404649761717], 14);
19+
// create map
20+
let map = L.map('map')
21+
22+
// get the bounds of the entered farm
23+
fetch(g_tileurl + '/getbounds/' + property_id.value, {
24+
headers: {
25+
'Authorization': 'Bearer ' + token.value,
26+
'Accept': 'application/json'
27+
},
28+
method: "GET"})
29+
.then(response => {
30+
if (!response.ok) {
31+
throw new Error(`HTTP error! status: ${response.status}`);
32+
}
33+
return response.json();
34+
})
35+
.then(function(data) {
36+
map.fitBounds([[data.ymin, data.xmin], [data.ymax, data.xmax]]);
37+
})
38+
.catch(error => {
39+
console.error('Fetch error:', error);
40+
});
2241

2342
// background layer
2443
const OSMLayer = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
@@ -34,9 +53,14 @@ function init()
3453
'Authorization': 'Bearer ' + token.value,
3554
'Accept': 'application/json'
3655
},
37-
method: "GET"})
38-
.then(response => response.json())
39-
.then(function(data) {
56+
method: "GET"})
57+
.then(response => {
58+
if (!response.ok) {
59+
throw new Error(`HTTP error! status: ${response.status}`);
60+
}
61+
return response.json();
62+
})
63+
.then(function(data) {
4064
const last_date = data.dates[data.dates.length - 1];
4165
g_tsdmlayer = new L.TileLayerHeaders(g_tileurl + '/tsdm/' + last_date + '/' + property_id.value + '/{z}/{x}/{y}', {
4266
attribution: '&copy; <a href="https://www.cibolabs.com.au/">CiboLabs</a>',
@@ -63,11 +87,20 @@ function init()
6387
'Authorization': 'Bearer ' + token.value,
6488
'Accept': 'application/json'
6589
},
66-
method: "POST"})
67-
.then(response => response.json())
68-
.then(function(data) {
90+
method: "POST"})
91+
.then(response => {
92+
if (!response.ok) {
93+
throw new Error(`HTTP error! status: ${response.status}`);
94+
}
95+
return response.json();
96+
})
97+
.then(function(data) {
6998
geojsonLayer.addData(data);
70-
});
99+
})
100+
.catch(error => {
101+
console.error('Fetch error:', error);
102+
});
103+
71104

72105
// dates - make a slider
73106
let slider = L.control.slider(function(value) {
@@ -101,7 +134,12 @@ function init()
101134
'Accept': 'application/json'
102135
},
103136
method: "POST"})
104-
.then(response => response.json())
137+
.then(response => {
138+
if (!response.ok) {
139+
throw new Error(`HTTP error! status: ${response.status}`);
140+
}
141+
return response.json();
142+
})
105143
.then(function(data) {
106144

107145
// pull out the data for every paddock
@@ -163,8 +201,13 @@ function init()
163201
}
164202
}
165203
});
166-
204+
})
205+
.catch(error => {
206+
console.error('Fetch error:', error);
167207
});
208+
})
209+
.catch(error => {
210+
console.error('Fetch error:', error);
168211
});
169212
});
170213
myDialog.showModal();

pasturekey.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ curl -s -X GET \
4747
}
4848
```
4949

50+
### /getbounds
51+
52+
**Request**
53+
54+
GET https://tiles.pasturekey.cibolabs.com/getbounds/e354f641-fce2-4299-a7d4-561dc31597d2
55+
56+
```bash
57+
farmid="e354f641-fce2-4299-a7d4-561dc31597d2"
58+
59+
curl -s -X GET \
60+
--output data.json \
61+
-H "Content-Type: application/json" \
62+
-H "Authorization: Bearer ${TOKEN}" \
63+
"https://tiles.pasturekey.cibolabs.com/getbounds/${farmid}"
64+
```
65+
66+
**Response**
67+
68+
```json
69+
{
70+
"xmin": 149.768297,
71+
"xmax": 149.905016937,
72+
"ymin": -26.840771,
73+
"ymax": -26.668700369
74+
}
75+
```
76+
5077
### /getpaddockinfo
5178

5279
Get basic information about the farm's paddocks.

0 commit comments

Comments
 (0)