-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (79 loc) · 3.16 KB
/
index.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
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
const parser = require('fast-xml-parser')
const proj4 = require('proj4')
const turf = require('@turf/helpers')
const utils = require('./src/utils.js')
// configure proj4 in order to convert GIS coordinates to web mercator
proj4.defs('EPSG:25832', '+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs')
const fromETRS89 = new proj4.Proj('EPSG:25832')
const toWGS84 = new proj4.Proj('WGS84')
module.exports = {
parseXML(xml) {
if (!parser.validate(xml)) throw new Error('Invalid XML structure.')
const json = parser.parse(xml)
const basis = {
'applicationYear': utils.getSafe(() => json.nn.antragsjahr),
'farmId': utils.getSafe(() => json.nn.bnrzd),
'state': utils.getSafe(() => json.nn.land.bezeichnung),
'fieldBlockConstant': utils.getSafe(() => json.nn.land.feldblockkonstante),
'stateNo': utils.getSafe(() => json.nn.land.nummer)
}
if (utils.getSafe(() => json.nn.land.parzelle)) {
if (!Array.isArray(json.nn.land.parzelle)) {
json.nn.land.parzelle = [json.nn.land.parzelle]
}
return json.nn.land.parzelle.map(field => {
return Object.assign(JSON.parse(JSON.stringify(basis)),field)
})
} else {
throw new Error('No fields found in XML.')
}
},
parseGML(gml) {
if (!parser.validate(gml)) throw new Error ('Invalid GML structure.')
function Plot(properties) {
this.schlag = properties.schlag
this.schlag.nummer = properties.schlag.nummer
this.geometry = properties.geometry
}
const json = parser.parse(gml)
let features = json['wfs:FeatureCollection']['gml:featureMember']
if (utils.getSafe(() => features)) {
let results = []
if (!Array.isArray(features)) {
features = [features]
}
features.forEach(field => {
const id = utils.getSafe(() => field['elan:tschlag']['elan:SCHLAGNR'])
const year = utils.getSafe(() => field['elan:tschlag']['elan:WIRTSCHAFTSJAHR'])
let coordinates = utils.getSafe(() => field['elan:tschlag']['elan:GEO_COORD_']['gml:Polygon']['gml:outerBoundaryIs']['gml:LinearRing']['gml:coordinates'])
if (!coordinates) return
// split coordinate string into array of strings
coordinates = coordinates.split(' ')
// then into array of arrays and transform string values to numbers
coordinates = coordinates.map(pair => {return pair.split(',').map(coord => { return Number(coord)})})
coordinates = coordinates.map(latlng => {
return proj4(fromETRS89, toWGS84, latlng)
})
const polygon = turf.polygon([coordinates], {number: id, year: year})
const plot = new Plot({
schlag: {
nummer: id
},
geometry: polygon
})
results.push(JSON.parse(JSON.stringify(plot)))
})
return results
} else {
throw new Error('No fields found in GML.')
}
},
join(xml, gml) {
return xml.map(field => {
const geometry = gml.find(o => o.schlag.nummer === field.schlag.nummer)
if (!geometry) return JSON.parse(JSON.stringify(field))
field.geometry = geometry.geometry
return JSON.parse(JSON.stringify(field))
})
}
}