77 filter,
88 reduce,
99 mergeMap,
10- tap,
1110 mergeAll,
11+ defaultIfEmpty,
12+ toArray,
13+ take,
1214 repeat,
1315} = require ( 'rxjs/operators' )
1416const Kommun = require ( '../lib/kommun' )
@@ -27,6 +29,7 @@ const Position = require('../lib/models/position')
2729const { getAddressesInArea } = require ( '../simulator/address' )
2830const { includedMunicipalities } = require ( '../config' )
2931const { info, debug } = require ( '../lib/log' )
32+ const assert = require ( 'assert' )
3033
3134const bookings = {
3235 hm : require ( '../streams/orders/hm.js' ) ,
@@ -67,47 +70,14 @@ function getMeasureStations(kommunName) {
6770 )
6871}
6972
70- async function centerPoint ( namn , retries = 0 ) {
71- try {
72- return await Pelias . search ( namn ) . then ( ( res ) => res . position )
73- } catch ( err ) {
74- if ( retries < 3 ) {
75- info (
76- "Couldn't find center point for" ,
77- namn ,
78- `retrying ${ retries + 1 } /3...`
79- )
80- return centerPoint ( namn , retries + 1 )
81- }
82- console . error ( 'Could not find center point for' , namn )
83- return { lat : 0 , lon : 0 }
84- }
85- }
86-
87- function getWorkplaces ( commercialAreas ) {
88- return commercialAreas . pipe (
89- mergeMap ( async ( commercialArea ) => {
90- const {
91- X_koord : x ,
92- Y_koord : y ,
93- AREA_HA : area ,
94- ARBST_DETH : nrOfWorkplaces ,
95- } = commercialArea . properties
96- const position = new Position (
97- convertPosition ( coords . toLatLng ( y . toString ( ) , x . toString ( ) ) )
98- )
99- const adresses = await getAddressesInArea ( position , area , nrOfWorkplaces )
100- return adresses . map ( ( a ) => ( { ...a , position : new Position ( a . position ) } ) )
101- } , 1 ) ,
102- mergeAll ( ) ,
103- shareReplay ( )
104- )
73+ async function getWorkplaces ( position , nrOfWorkplaces = 100 ) {
74+ const area = 10000
75+ const adresses = await getAddressesInArea ( position , area , nrOfWorkplaces )
76+ return adresses . map ( ( a ) => ( { ...a , position : new Position ( a . position ) } ) )
10577}
10678
10779// function read() {
10880function read ( { fleets } ) {
109- const workplaces = getWorkplaces ( commercialAreas )
110-
11181 return from ( data ) . pipe (
11282 filter ( ( { namn } ) =>
11383 includedMunicipalities . some ( ( name ) => namn . startsWith ( name ) )
@@ -133,15 +103,13 @@ function read({ fleets }) {
133103 } ) => {
134104 const squares = getPopulationSquares ( { geometry } )
135105 const commercialAreas = getCommercialAreas ( kod )
136- const center = await centerPoint ( name )
137- const nearbyWorkplaces = workplaces . pipe (
138- // TODO: calculate based on a normal distribution and size of the municipality?
139- // or other data from SCB?
140- filter (
141- ( workplace ) => workplace ?. position ?. distanceTo ( center ) < 100_000
142- ) ,
106+ const center = await Pelias . searchOne ( name ) . then ( ( res ) => res . position )
107+ const nearbyWorkplaces = from ( getWorkplaces ( center ) ) . pipe (
108+ mergeAll ( ) ,
109+ take ( 100 ) ,
143110 repeat ( )
144111 )
112+
145113 const citizens = squares . pipe (
146114 mergeMap (
147115 ( square ) => getCitizensInSquare ( square , nearbyWorkplaces , name ) ,
0 commit comments