-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
186 lines (171 loc) · 6.7 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<!-- <script src="https://api.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.js"></script>
<link
href="https://api.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.css"
rel="stylesheet"
/> -->
<!-- <script src="https://unpkg.com/@turf/turf/turf.min.js"></script> -->
<style>
body {
margin: 10;
padding: 10;
}
#map {
position: absolute;
top: 0;
bottom: 0;
/* height: 800px; */
width: 95%;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import mapboxcss from './vendor/mapboxglcss.js'
import mapboxgl from './vendor/mapbox-gl.esm.js'
// import * as turf from 'https://unpkg.com/@turf/turf/turf.es.js'
import * as turf from './node_modules/@turf/turf/turf.es.js'
import util from 'https://backspaces.github.io/agentscript/src/util.js'
import TwoView from 'https://backspaces.github.io/agentscript/src/TwoView.js'
import ColorMap from 'https://backspaces.github.io/agentscript/src/ColorMap.js'
import {
mapLoadPromise,
importModel,
bboxCenter,
bboxCoords,
} from './tools.js'
document.head.innerHTML += mapboxcss
util.toWindow({ util })
const grayMap = ColorMap.LightGray
const basicMap = ColorMap.Basic16
mapboxgl.accessToken =
'pk.eyJ1IjoiYmFja3NwYWNlcyIsImEiOiJjanVrbzI4dncwOXl3M3ptcGJtN3oxMmhoIn0.x9iSCrtm0iADEqixVgPwqQ'
async function run() {
const counties = await util.xhrPromise(
'data/nmcounties.geojson',
'json'
)
const bbox = turf.bbox(counties)
util.toWindow({ counties, bbox })
const model = await importModel(
'https://backspaces.github.io/agentscript/models/HelloModel.js',
100,
bbox
)
const xfm = model.world.bboxTransform(...bbox)
model.patches.ask(p => {
const pt = xfm.toBBox([p.x, p.y])
;[p.geoX, p.geoY] = pt
util.forLoop(counties.features, (f, i) => {
if (turf.booleanPointInPolygon(pt, f)) {
if (p.feature)
console.log('p.feature exists', p.feature, f)
p.feature = f
}
})
})
const nullFeature = {}
model.turtles.ask(t => (t.feature = t.patch.feature))
const view = new TwoView(model.world, {
div: util.createCanvas(0, 0, false), // not offscreen}
})
// view.drawPatches(model.patches, p => {
// const color = p.feature ? grayMap.randomColor() : grayMap[0]
// color.alpha = 120
// return color.pixel
// })
util.toWindow({ model, view, grayMap, xfm })
const map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v11',
center: bboxCenter(bbox),
zoom: 5.5,
renderWorldCopies: false,
})
await mapLoadPromise(map)
map.addLayer(
{
id: 'nmcounties',
type: 'line',
source: {
type: 'geojson',
data: counties,
},
paint: {
'line-color': 'blue',
'line-width': 3,
},
},
'settlement-label'
)
map.addLayer({
id: 'BBox',
type: 'line',
source: {
type: 'geojson',
data: turf.featureCollection([turf.bboxPolygon(bbox)]),
},
paint: {
'line-color': 'red',
},
})
map.addSource('canvas', {
type: 'canvas',
canvas: view.canvas,
animate: true,
coordinates: bboxCoords(bbox), // 4 [lon,lat] arrays
})
map.addLayer({
id: 'canvas',
type: 'raster',
source: 'canvas',
})
util.timeoutLoop(
() => {
model.step()
model.tick()
view.clear()
// view.drawPatches()
view.drawLinks(model.links, {
color: 'gray',
width: 1,
})
view.drawTurtles(model.turtles, t => ({
shape: 'dart',
color: basicMap.atIndex(t.id).css,
size: 3,
}))
model.turtles.ask(t => {
if (t.feature !== t.patch.feature) {
t.feature = t.patch.feature
const name = t.feature
? t.feature.properties.NAME
: 'No Where'
const population = t.feature
? t.feature.properties.population
: 0
console.log(
'new feature: turtle: ' + t.id + ',',
'county: ' + name + ',',
'population: ' + population
)
}
})
},
-1,
33
)
}
run()
</script>
</body>
</html>