lixuliang
2024-04-18 b5c4921d0828500379502a916a2ccf2d9d4acc96
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
define(function () {
    "use strict";
    function JsonGMLParser(extractAttributes, xy, gmlnsm, wfsnsm, featureName, geometryAttribute) {
        this._extractAttributes = extractAttributes;
        this._featureName = featureName || "featureMember";
        this._xy = xy;
        this._gmlns = gmlnsm || "http://www.opengis.net/gml",
        this._wfs = wfsnsm || "http://www.opengis.net/wfs",
        this._geometryAttribute = geometryAttribute;
 
        // compile regular expressions once instead of every time they are used
        this._regExes = {
            trimSpace: (/^\s*|\s*$/g),
            removeSpace: (/\s*/g),
            splitSpace: (/\s+/),
            trimComma: (/\s*,\s*/g)
        };
    }
 
    Object.defineProperties(JsonGMLParser.prototype, {
        extractAttributes: {
            get: function () {
                return this._extractAttributes;
            },
            set: function (value) {
                this._extractAttributes = value;
            }
        },
 
        xy: {
            get: function () {
                return this._xy;
            },
        }
    });
 
    JsonGMLParser.prototype.read = function (data) {
        if (this._wfsPrefix == undefined) {
            this.detectPrefixes();
        }
 
        var documentElement = data["?xml"];
        if (documentElement == undefined)
            documentElement = data[this._wfsPrefix + "FeatureCollection"];
        else
            documentElement = data["?xml"][this._wfsPrefix + "FeatureCollection"];
 
        if (documentElement == undefined) {
            throw "Invalid GML format. Could not find root element";
        }
 
        var featureNodes = documentElement[this._gmlPrefix + this._featureName] || [];
        var features = [];
        var objName = this._featureName;
 
        if (featureNodes.length == undefined) {
            var arr = Object.keys(featureNodes).map(function (key) { return { objName: featureNodes[key] }; });
            featureNodes = arr;
        }
        for (var i = 0; i < featureNodes.length; i++) {
            var featureNode = featureNodes[i];
            var typeName = Object.getOwnPropertyNames(featureNode)[0];
            var feature = this.parseFeature(featureNode[typeName]);
            if (feature) {
                features.push(feature);
            }
        }
        return features;
    };
 
    JsonGMLParser.prototype.detectPrefixes = function (data) {
        this._wfsPrefix = "wfs:";
        this._gmlPrefix = "gml:";
    }
    JsonGMLParser.prototype.detectGeometryAttribute = function (data) {
        this._geometryAttribute = "Geom";
    }
    /**
     * Method: parseFeature
     * This function is the core of the GML parsing code in OpenLayers.
     *    It creates the geometries that are then attached to the returned
     *    feature, and calls parseAttributes() to get attribute data out.
     *    
     * Parameters:
     * node - {DOMElement} A GML feature node. 
     */
 
    function containGeometryAttribute(propName, geometryAttribute) {
 
        var index = propName.indexOf(':' + geometryAttribute);
        return (index > 0 && index == propName.length - geometryAttribute.length - 1);
    }
 
    JsonGMLParser.prototype.parseFeature = function (node) {
        // only accept one geometry per feature - look for highest "order"
        var order = ["MultiPolygon", "Polygon",
                     "MultiLineString", "LineString",
                     "MultiPoint", "Point"];
        // FIXME: In case we parse a feature with no geometry, but boundedBy an Envelope,
        // this code creates a geometry derived from the Envelope. This is not correct.
        if (this._geometryAttribute == null)
            this.detectGeometryAttribute(node);
        var geometry, type, parser;
        var attributes = {};
        var nemes = "";
        for (var propName in node) {
            if (nemes == "") {
                nemes = propName;
            }
            if (propName == this._geometryAttribute || containGeometryAttribute(propName, this._geometryAttribute)) {
                var fullType = Object.getOwnPropertyNames(node[propName])[0]
                type = fullType.replace(this._gmlPrefix, "");
                parser = this.parseGeometry[type.toLowerCase()];
                if (parser) {
                    geometry = parser.apply(this, [node[propName][fullType]]);
                }
                else {
                    console.log("unsupportedGeometryType:" + type);
                }
            }
            else if (propName != "_attributes") {
                attributes[propName] = node[propName].value;
            }
        }
 
        // TODO: optinally parse gml:boundedBy on feature
        var bounds;
 
        // construct feature (optionally with attributes)
        if (this.extractAttributes == false) {
            attributes = undefined;
        }
        var feature =
        {
            geometryType: type.toLowerCase(),
            positions: geometry,
            attributes: attributes
        };
 
        feature.bounds = bounds;
 
        // assign fid - this can come from a "fid" or "id" attribute
        // console.log(node);
        //feature.fid = node["_attributes"].fid || node["_attributes"].id;
        if (node["_attributes"] != undefined) {
            feature.fid = node["_attributes"].fid || node["_attributes"].id;
        } else {
            feature.fid = node[nemes].value;
        }
        return feature;
    };
 
    /**
     * Property: parseGeometry
     * Properties of this object are the functions that parse geometries based
     *     on their type.
     */
    JsonGMLParser.prototype.parseGeometry = {
 
        /**
         * Method: parseGeometry.point
         * Given a GML node representing a point geometry, create an OpenLayers
         *     point geometry.
         *
         * Parameters:
         * node - {DOMElement} A GML node.
         *
         * Returns:
         * {<OpenLayers_Geometry_Point>} A point geometry.
         */
        point: function (node) {
            /**
             * Three coordinate variations to consider:
             * 1) <gml:pos>x y z</gml:pos>
             * 2) <gml:coordinates>x, y, z</gml:coordinates>
             * 3) <gml:coord><gml:X>x</gml:X><gml:Y>y</gml:Y></gml:coord>
             */
            var nodeList, coordString;
            var coords = [];
 
            // look for <gml:pos>
            if (node[this._gmlPrefix + "pos"]) {
                coordString = node[this._gmlPrefix + "pos"].value;
                coordString = coordString.replace(this._regExes.trimSpace, "");
                coords = coordString.split(this._regExes.splitSpace);
            }
                // look for <gml:coordinates>
            else if (node[this._gmlPrefix + "coordinates"]) {
                coordString = node[this._gmlPrefix + "coordinates"].value;
                coordString = coordString.replace(this._regExes.removeSpace, "");
                coords = coordString.split(",");
            }
 
                // look for <gml:coord>
            else if (node[this._gmlPrefix + "coord"]) {
                var xVal = node[this._gmlPrefix + "coord"][this._gmlPrefix + "X"].value;
                var yVal = node[this._gmlPrefix + "coord"][this._gmlPrefix + "Y"].value;
                if (xVal != undefined && yVal != undefined) {
                    coords = [xVal, yVal];
                }
            }
 
            // preserve third dimension
            if (coords.length == 2) {
                coords[2] = null;
            }
 
            if (this.xy) {
                return [parseFloat(coords[0]), parseFloat(coords[1]), parseFloat(coords[2])];
            }
            else {
                return [parseFloat(coords[1]), parseFloat(coords[0]), parseFloat(coords[2])];
            }
        },
 
        /**
         * Method: parseGeometry.multipoint
         * Given a GML node representing a multipoint geometry, create an
         *     OpenLayers multipoint geometry.
         *
         * Parameters:
         * node - {DOMElement} A GML node.
         *
         * Returns:
         * {<OpenLayers.Geometry.MultiPoint>} A multipoint geometry.
         */
        multipoint: function (node) {
            var nodeList = node[this._gmlPrefix + "pointMember"];
            var components = [];
 
            if (nodeList.length == undefined)
                nodeList = [nodeList];
 
            if (nodeList.length > 0) {
                var point;
                for (var i = 0; i < nodeList.length; ++i) {
                    point = this.parseGeometry.point.apply(this, [nodeList[i][this._gmlPrefix + "Point"]]);
                    if (point) {
                        components.push(point);
                    }
                }
            }
            return components;
        },
 
        /**
         * Method: parseGeometry.linestring
         * Given a GML node representing a linestring geometry, create an
         *     OpenLayers linestring geometry.
         *
         * Parameters:
         * node - {DOMElement} A GML node.
         *
         * Returns:
         * {<OpenLayers.Geometry.LineString>} A linestring geometry.
         */
        linestring: function (node) {
            /**
             * Two coordinate variations to consider:
             * 1) <gml:posList dimension="d">x0 y0 z0 x1 y1 z1</gml:posList>
             * 2) <gml:coordinates>x0, y0, z0 x1, y1, z1</gml:coordinates>
             */
            var nodeList, coordString;
            var coords = [];
            var points = [];
 
            // look for <gml:posList>
            if (node[this._gmlPrefix + "posList"]) {
                coordString = node[this._gmlPrefix + "posList"].value;
                coordString = coordString.replace(this._regExes.trimSpace, "");
                coords = coordString.split(this._regExes.splitSpace);
                var dim = node[this._gmlPrefix + "posList"]._attributes["dimension"];
                var j, x, y, z;
                for (var i = 0; i < coords.length / dim; ++i) {
                    j = i * dim;
                    x = parseFloat(coords[j]);
                    y = parseFloat(coords[j + 1]);
                    z = (dim == 2) ? null : parseFloat(coords[j + 2]);
                    if (this.xy) {
                        points.push(x, y, z);
                    } else {
                        points.push(y, x, z);
                    }
                }
            }
 
                // look for <gml:coordinates>
            else if (node[this._gmlPrefix + "coordinates"]) {
                coordString = node[this._gmlPrefix + "coordinates"].value;
                coordString = coordString.replace(this._regExes.trimSpace, "");
                coordString = coordString.replace(this._regExes.trimComma, ",");
                var pointList = coordString.split(this._regExes.splitSpace);
                for (var i = 0; i < pointList.length; ++i) {
                    coords = pointList[i].split(",");
                    if (coords.length == 2) {
                        coords[2] = null;
                    }
                    if (this.xy) {
                        points.push(parseFloat(coords[0]),
                                              parseFloat(coords[1]),
                                              parseFloat(coords[2]));
                    } else {
                        points.push(parseFloat(coords[1]),
                                                  parseFloat(coords[0]),
                                                  parseFloat(coords[2]));
                    }
                }
            }
 
            return points;
        },
 
        /**
         * Method: parseGeometry.multilinestring
         * Given a GML node representing a multilinestring geometry, create an
         *     OpenLayers multilinestring geometry.
         *
         * Parameters:
         * node - {DOMElement} A GML node.
         *
         * Returns:
         * {<OpenLayers.Geometry.MultiLineString>} A multilinestring geometry.
         */
        multilinestring: function (node) {
            var nodeList = node[this._gmlPrefix + "lineStringMember"];
            var components = [];
 
            if (nodeList.length == undefined)
                nodeList = [nodeList];
 
            if (nodeList.length > 0) {
                var line;
                for (var i = 0; i < nodeList.length; ++i) {
                    line = this.parseGeometry.linestring.apply(this, [nodeList[i][this._gmlPrefix + "LineString"]]);
                    if (line) {
                        components.push(line);
                    }
                }
            }
            return components;
        },
 
        /**
         * Method: parseGeometry.polygon
         * Given a GML node representing a polygon geometry, create an
         *     OpenLayers polygon geometry.
         *
         * Parameters:
         * node - {DOMElement} A GML node.
         *
         * Returns:
         * {<OpenLayers_Geometry_Polygon>} A polygon geometry.
         */
        polygon: function (node) {
            var rings = [node[this._gmlPrefix + "outerBoundaryIs"][this._gmlPrefix + "LinearRing"]];
            if (node[this._gmlPrefix + "innerBoundaryIs"]) {
                for (var i = 0; i < node[this._gmlPrefix + "innerBoundaryIs"].length; i++)
                    rings.push(node[this._gmlPrefix + "innerBoundaryIs"][i][this._gmlPrefix + "LinearRing"]);
            }
            var components = [];
            if (rings.length > 0) {
                // this assumes exterior ring first, inner rings after
                var ring;
                for (var i = 0; i < rings.length; ++i) {
                    ring = this.parseGeometry.linestring.apply(this, [rings[i]]);
                    if (ring) {
                        components.push(ring);
                    }
                }
            }
            return components;
        },
 
        /**
         * Method: parseGeometry.multipolygon
         * Given a GML node representing a multipolygon geometry, create an
         *     OpenLayers multipolygon geometry.
         *
         * Parameters:
         * node - {DOMElement} A GML node.
         *
         * Returns:
         * {<OpenLayers_Geometry_MultiPolygon>} A multipolygon geometry.
         */
        multipolygon: function (node) {
            var nodeList = node[this._gmlPrefix + "polygonMember"];
            var components = [];
 
            if (nodeList.length == undefined)
                nodeList = [nodeList];
 
            if (nodeList.length > 0) {
                var polygon;
                for (var i = 0; i < nodeList.length; ++i) {
                    polygon = this.parseGeometry.polygon.apply(this, [nodeList[i][this._gmlPrefix + "Polygon"]]);
                    if (polygon) {
                        components.push(polygon);
                    }
                }
            }
            return components;
        },
    };
 
    return JsonGMLParser;
});