/
srgspec.go
456 lines (420 loc) · 13.5 KB
/
srgspec.go
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
Copyright (C) 2012-2019 the InMAP authors.
This file is part of InMAP.
InMAP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
InMAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with InMAP. If not, see <http://www.gnu.org/licenses/>.
*/
package aep
import (
"context"
"encoding/csv"
"fmt"
"io"
"log"
"math"
"path/filepath"
"strconv"
"strings"
"github.com/ctessum/geom"
"github.com/ctessum/geom/encoding/shp"
"github.com/ctessum/geom/index/rtree"
"github.com/ctessum/requestcache/v2"
)
type SrgSpec interface {
getSrgData(gridData *GridDef, loc *Location, tol float64) (*rtree.Rtree, error)
backupSurrogateNames() []string
region() Country
code() string
name() string
mergeNames() []string
mergeMultipliers() []float64
}
// SrgSpecSMOKE holds SMOKE-formatted spatial surrogate specification information.
// See the SMOKE emissions model technical documentation for additional information.
type SrgSpecSMOKE struct {
Region Country
Name string
Code string
DATASHAPEFILE string
DATAATTRIBUTE string
WEIGHTSHAPEFILE string
Details string
// BackupSurrogateNames specifies names of surrogates to use if this
// one doesn't have data for the desired location.
BackupSurrogateNames []string
// WeightColumns specify the fields of the surogate shapefile that
// should be used to weight the output locations.
WeightColumns []string
// WeightFactors are factors by which each of the WeightColumns should
// be multiplied.
WeightFactors []float64
// FilterFunction specifies which rows in the surrogate shapefile should
// be used to create this surrogate.
FilterFunction *SurrogateFilter
// MergeNames specify names of other surrogates that should be combined
// to create this surrogate.
MergeNames []string
// MergeMultipliers specifies multipliers associated with the surrogates
// in MergeNames.
MergeMultipliers []float64
cache *requestcache.Cache
}
const none = "NONE"
// ReadSrgSpecSMOKE reads a SMOKE formatted spatial surrogate specification file.
// Results are returned as a map of surrogate specifications as indexed by
// their unique ID, which is Region+SurrogateCode. shapefileDir specifies the
// location of all the required shapefiles, and checkShapeFiles specifies whether
// to check if the required shapefiles actually exist. If checkShapeFiles is
// true, then it is okay for the shapefiles to be in any subdirectory of
// shapefileDir, otherwise all shapefiles must be in shapefileDir itself and
// not a subdirectory.
// diskCachePath specifies a path to a directory where an on-disk cache should
// be created (if "", no cache will be created), and memCacheSize specifies the
// number of surrogate data entries to hold in an in-memory cache.
func ReadSrgSpecSMOKE(fid io.Reader, shapefileDir string, checkShapefiles bool, diskCachePath string, memCacheSize int) (*SrgSpecs, error) {
srgs := NewSrgSpecs()
reader := csv.NewReader(fid)
reader.Comment = '#'
reader.TrailingComma = true
records, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("in ReadSrgSpec: %v", err)
}
cache, err := newCache(diskCachePath, memCacheSize, marshalSrgHolders, unmarshalSrgHolders)
if err != nil {
return nil, err
}
for i := 1; i < len(records); i++ {
record := records[i]
srg := new(SrgSpecSMOKE)
srg.Region, err = countryFromName(record[0])
if err != nil {
return nil, fmt.Errorf("in ReadSrgSpec: %v", err)
}
srg.Name = strings.TrimSpace(record[1])
srg.Code = record[2]
srg.DATASHAPEFILE = record[3]
srg.DATAATTRIBUTE = strings.TrimSpace(record[4])
srg.WEIGHTSHAPEFILE = record[5]
WEIGHTATTRIBUTE := record[6]
WEIGHTFUNCTION := record[7]
FILTERFUNCTION := record[8]
MERGEFUNCTION := record[9]
for i := 10; i <= 12; i++ {
if len(record[i]) != 0 {
srg.BackupSurrogateNames = append(srg.BackupSurrogateNames, record[i])
}
}
srg.Details = record[13]
// Parse weight function
if WEIGHTATTRIBUTE != none && WEIGHTATTRIBUTE != "" {
srg.WeightColumns = append(srg.WeightColumns,
strings.TrimSpace(WEIGHTATTRIBUTE))
srg.WeightFactors = append(srg.WeightFactors, 1.)
}
if WEIGHTFUNCTION != "" {
weightfunction := strings.Split(WEIGHTFUNCTION, "+")
for _, wf := range weightfunction {
mulFunc := strings.Split(wf, "*")
if len(mulFunc) == 1 {
srg.WeightColumns = append(srg.WeightColumns,
strings.TrimSpace(mulFunc[0]))
srg.WeightFactors = append(srg.WeightFactors, 1.)
} else if len(mulFunc) == 2 {
v, err2 := strconv.ParseFloat(mulFunc[0], 64)
if err2 != nil {
return nil, fmt.Errorf("srgspec weight function: %v", err2)
}
srg.WeightColumns = append(srg.WeightColumns,
strings.TrimSpace(mulFunc[1]))
srg.WeightFactors = append(srg.WeightFactors, v)
} else {
return nil, fmt.Errorf("invalid value %s in srgspec "+
"weighting function", wf)
}
}
}
// Parse filter function
srg.FilterFunction = ParseSurrogateFilter(FILTERFUNCTION)
// Parse merge function
if MERGEFUNCTION != none && MERGEFUNCTION != "" {
s := strings.Split(MERGEFUNCTION, "+")
for _, s2 := range s {
s3 := strings.Split(s2, "*")
srg.MergeNames = append(srg.MergeNames, strings.TrimSpace(s3[1]))
val, err2 := strconv.ParseFloat(strings.TrimSpace(s3[0]), 64)
if err2 != nil {
return nil, err2
}
srg.MergeMultipliers = append(srg.MergeMultipliers, val)
}
}
// Set up the shapefile paths and
// optionally check to make sure the shapefiles exist.
if checkShapefiles {
if srg.DATASHAPEFILE != "" {
srg.DATASHAPEFILE, err = findFile(shapefileDir, srg.DATASHAPEFILE+".shp")
if err != nil {
return nil, err
}
}
if srg.WEIGHTSHAPEFILE != "" {
srg.WEIGHTSHAPEFILE, err = findFile(shapefileDir, srg.WEIGHTSHAPEFILE+".shp")
if err != nil {
return nil, err
}
}
} else {
if srg.DATASHAPEFILE != "" {
srg.DATASHAPEFILE = filepath.Join(shapefileDir, srg.DATASHAPEFILE+".shp")
}
if srg.WEIGHTSHAPEFILE != "" {
srg.WEIGHTSHAPEFILE = filepath.Join(shapefileDir, srg.WEIGHTSHAPEFILE+".shp")
}
}
if checkShapefiles {
if srg.DATASHAPEFILE != "" {
shpf, err := shp.NewDecoder(srg.DATASHAPEFILE)
if err != nil {
return nil, err
}
shpf.Close()
}
if srg.WEIGHTSHAPEFILE != "" {
shpf, err := shp.NewDecoder(srg.WEIGHTSHAPEFILE)
if err != nil {
return nil, err
}
shpf.Close()
}
}
srg.cache = cache
srgs.Add(srg)
}
return srgs, nil
}
func (srg *SrgSpecSMOKE) backupSurrogateNames() []string { return srg.BackupSurrogateNames }
func (srg *SrgSpecSMOKE) region() Country { return srg.Region }
func (srg *SrgSpecSMOKE) code() string { return srg.Code }
func (srg *SrgSpecSMOKE) name() string { return srg.Name }
func (srg *SrgSpecSMOKE) mergeNames() []string { return srg.MergeNames }
func (srg *SrgSpecSMOKE) mergeMultipliers() []float64 { return srg.MergeMultipliers }
func (srg *SrgSpecSMOKE) dataShapefile() string { return srg.DATASHAPEFILE }
func (srg *SrgSpecSMOKE) dataAttribute() string { return srg.DATAATTRIBUTE }
// InputShapes returns the input shapes associated with the receiver.
func (srg *SrgSpecSMOKE) InputShapes() (map[string]*Location, error) {
inputShp, err := shp.NewDecoder(srg.DATASHAPEFILE)
if err != nil {
return nil, err
}
defer inputShp.Close()
inputSR, err := inputShp.SR()
if err != nil {
return nil, err
}
inputData := make(map[string]*Location)
for {
g, fields, more := inputShp.DecodeRowFields(srg.dataAttribute())
if !more {
break
}
inputID := fields[srg.DATAATTRIBUTE]
ggeom := g.(geom.Polygon)
// Extend existing polygon if one already exists for this InputID
if _, ok := inputData[inputID]; !ok {
inputData[inputID] = &Location{
Geom: ggeom,
SR: inputSR,
Name: srg.region().String() + inputID,
}
} else {
inputData[inputID].Geom = append(inputData[inputID].Geom.(geom.Polygon), ggeom...)
}
}
if inputShp.Error() != nil {
return nil, fmt.Errorf("in file %s, %v", srg.dataShapefile(), inputShp.Error())
}
return inputData, nil
}
// get surrogate shapes and weights. tol is a geometry simplification tolerance.
func (srg *SrgSpecSMOKE) getSrgData(gridData *GridDef, inputLoc *Location, tol float64) (*rtree.Rtree, error) {
// Calculate the area of interest for our surrogate data.
inputShapeT, err := inputLoc.Reproject(gridData.SR)
if err != nil {
return nil, err
}
inputShapeBounds := inputShapeT.Bounds()
srgBounds := inputShapeBounds.Copy()
for _, cell := range gridData.Cells {
b := cell.Bounds()
if b.Overlaps(inputShapeBounds) {
srgBounds.Extend(b)
}
}
in := &readSrgDataSMOKEInput{gridData: gridData, tol: tol, srg: srg}
request := srg.cache.NewRequest(context.TODO(), in)
srgs, err := request.Result()
if err != nil {
return nil, err
}
return srgs.(readSrgDataOutput).index, nil
}
type readSrgDataSMOKEInput struct {
gridData *GridDef
tol float64
srg *SrgSpecSMOKE
}
func (s *readSrgDataSMOKEInput) Key() string {
return fmt.Sprintf("smoke_srgdata_%s%s_%s_%g", s.srg.region(), s.srg.code(), s.gridData.SR.Name, s.tol)
}
type readSrgDataOutput struct {
srgs []*srgHolder
index *rtree.Rtree
}
// Run returns all of the spatial surrogate information for this
// surrogate definition.
func (input *readSrgDataSMOKEInput) Run(ctx context.Context) (interface{}, error) {
srg := input.srg
log.Printf("processing surrogate `%s` spatial data", srg.Name)
srgShp, err := shp.NewDecoder(srg.WEIGHTSHAPEFILE)
if err != nil {
return nil, err
}
defer srgShp.Close()
srgSR, err := srgShp.SR()
if err != nil {
return nil, err
}
srgCT, err := srgSR.NewTransform(input.gridData.SR)
if err != nil {
return nil, err
}
var fieldNames []string
if srg.FilterFunction != nil {
fieldNames = append(fieldNames, srg.FilterFunction.Column)
}
if srg.WeightColumns != nil {
fieldNames = append(fieldNames, srg.WeightColumns...)
}
srgs := readSrgDataOutput{
index: rtree.NewTree(25, 50),
}
var recGeom geom.Geom
var data map[string]string
var keepFeature bool
var featureVal string
var size float64
var more bool
for {
recGeom, data, more = srgShp.DecodeRowFields(fieldNames...)
if !more {
break
}
if srg.FilterFunction == nil {
keepFeature = true
} else {
// Determine whether this feature should be kept according to
// the filter function.
keepFeature = false
featureVal = strings.TrimSpace(fmt.Sprintf("%v", data[srg.FilterFunction.Column]))
for _, filterVal := range srg.FilterFunction.Values {
switch srg.FilterFunction.EqualNotEqual {
case "NotEqual":
if featureVal != filterVal {
keepFeature = true
}
default:
if featureVal == filterVal {
keepFeature = true
}
}
}
}
if keepFeature && recGeom != nil {
srgH := new(srgHolder)
srgH.Geom, err = recGeom.Transform(srgCT)
if err != nil {
return nil, err
}
if input.tol > 0 {
switch srgH.Geom.(type) {
case geom.Simplifier:
srgH.Geom = srgH.Geom.(geom.Simplifier).Simplify(input.tol)
}
}
if len(srg.WeightColumns) != 0 {
weightval := 0.
for i, name := range srg.WeightColumns {
var v float64
vstring := data[name]
if strings.Contains(vstring, "\x00\x00\x00\x00\x00\x00") || strings.Contains(vstring, "***") || vstring == "" {
// null value
v = 0.
} else {
v, err = strconv.ParseFloat(data[name], 64)
if err != nil {
return nil, fmt.Errorf("aep.getSrgData: shapefile %s column %s, %v", srg.WEIGHTSHAPEFILE, name, err)
}
v = math.Max(v, 0) // Get rid of any negative weights.
}
weightval += v * srg.WeightFactors[i]
}
switch srgH.Geom.(type) {
case geom.Polygonal:
size = srgH.Geom.(geom.Polygonal).Area()
if size == 0. {
if input.tol > 0 {
// We probably simplified the shape down to zero area.
continue
} else {
// TODO: Is it okay for input shapes to have zero area? Probably....
continue
//err = fmt.Errorf("Area should not equal "+
// "zero in %v", srg.WEIGHTSHAPEFILE)
//return srgData, err
}
} else if size < 0 {
panic(fmt.Errorf("negative area: %g, geom:%#v", size, srgH.Geom))
}
srgH.Weight = weightval / size
case geom.Linear:
size = srgH.Geom.(geom.Linear).Length()
if size == 0. {
err = fmt.Errorf("Length should not equal "+
"zero in %v", srg.WEIGHTSHAPEFILE)
return nil, err
}
srgH.Weight = weightval / size
case geom.Point:
srgH.Weight = weightval
default:
err = fmt.Errorf("aep: in file %s, unsupported geometry type %#v",
srg.WEIGHTSHAPEFILE, srgH.Geom)
return nil, err
}
} else {
srgH.Weight = 1.
}
if srgH.Weight < 0. || math.IsInf(srgH.Weight, 0) ||
math.IsNaN(srgH.Weight) {
err = fmt.Errorf("Surrogate weight is %v, which is not acceptable.", srgH.Weight)
return nil, err
} else if srgH.Weight != 0. {
srgs.srgs = append(srgs.srgs, srgH)
srgs.index.Insert(srgH)
}
}
}
if srgShp.Error() != nil {
return nil, fmt.Errorf("in file %s, %v", srg.WEIGHTSHAPEFILE, srgShp.Error())
}
return srgs, nil
}