-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCountiesModel.js
46 lines (41 loc) · 1.48 KB
/
CountiesModel.js
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
import * as util from 'https://code.agentscript.org/src/utils.js'
import HelloModel from 'https://code.agentscript.org/models/HelloModel.js'
import { booleanPointInPolygon } from '../vendor/turfImports.js'
import counties from './data/nmcounties.json' with { type: 'json' }
class CountiesModel extends HelloModel {
linksToo = false // don't include links between turtles
// Note these options cause HelloModel => Model to use GeoWorld
static defaultOptions() {
return { bbox: counties, patchesWidth: 100 }
}
constructor(options = CountiesModel.defaultOptions()) {
super(options)
}
setup() {
super.setup()
this.patches.ask(p => {
const pt = this.world.toGeo(p.x, p.y)
// note: the geoworld has to be constructed with geojson for bbox
util.forLoop(this.world.geojson.features, f => {
if (booleanPointInPolygon(pt, f)) {
if (p.feature) console.log('p.feature exists', p)
p.feature = f
}
})
})
this.turtles.ask(t => {
t.feature = t.patch.feature
t.county = null
})
}
step() {
super.step()
this.turtles.ask(t => {
if (t.feature !== t.patch.feature || !t.county) {
t.feature = t.patch.feature
t.county = t.feature ? Number(t.feature.properties.COUNTY) : 0
}
})
}
}
export default CountiesModel