|
1 |
| -/** |
2 |
| -Handle multiple geometries that may be found in the CPAD return |
3 |
| -**/ |
4 |
| -var Terraformer = require('terraformer'); |
5 |
| -var WKT = require('terraformer-wkt-parser'); |
6 |
| - |
7 |
| -// packFeatures operates over a larger geoJsonObject and operates |
8 |
| -// row by row, replacing the array of temporary WKT geometries |
9 |
| -// that were inserted by the appender with a geometry collection |
10 |
| -// object. This effectively creates a proper GeoJSON object |
11 |
| -// During the operation the tempFeatures element is deleted and |
12 |
| -// replaced by the geometries element. |
13 |
| -exports.packFeatures = function (geoJsonObj) { |
14 |
| - for (var row in geoJsonObj) { |
15 |
| - var geoJsonRow = geoJsonObj[row] |
16 |
| - // array to hold all the possible multipolygons |
17 |
| - var geometries = [] |
18 |
| - for (var feature in geoJsonRow.tempFeatures) { |
19 |
| - // convert the WKT to GeoJSON |
20 |
| - var polygon = WKT.parse(geoJsonRow.tempFeatures[feature]) |
21 |
| - // push the polygon into the geometries array |
22 |
| - geometries.push(polygon) |
23 |
| - } |
24 |
| - // create the geometry collectioh |
25 |
| - var geometryCollection = new Terraformer.GeometryCollection(geometries) |
26 |
| - |
27 |
| - // Remove the tempFeatures row now that we're done |
28 |
| - delete geoJsonRow.tempFeatures; |
29 |
| - // Combine the properties with the geometryCollection we created |
30 |
| - geoJsonObj[row] = Object.assign({},geoJsonRow,geometryCollection) |
31 |
| - } |
32 |
| - return geoJsonObj |
33 |
| - |
34 |
| -}, |
35 |
| - |
36 |
| -// Append names and geometries to a geoJsonObject. |
37 |
| -// This function takes parameters geoJsonObj, name, geometry |
38 |
| -// Returns the geoJsonObj with name and a tempGeometry object apended |
39 |
| -exports.appender = function (geoJsonObj, name, geometry) { |
40 |
| - // Search for element in the geoJsonObj with a name value equal to the name parameter |
41 |
| - var found = geoJsonObj.find(value => value.properties.name === name) |
42 |
| - |
43 |
| - // if found is not undefined we push the new geometry onto the geometries stack |
44 |
| - if (found) { |
45 |
| - found.tempFeatures.push(geometry) |
46 |
| - // If not found then we create a new object and push the new geometry onto the stack |
47 |
| - } else { |
48 |
| - var l = {} |
49 |
| - l.tempFeatures = [geometry] |
50 |
| - l.properties = {name:name} |
51 |
| - geoJsonObj.push(l) |
52 |
| - } |
53 |
| - return geoJsonObj; |
54 |
| -} |
55 |
| - |
| 1 | +/** |
| 2 | +Handle multiple geometries that may be found in the CPAD return |
| 3 | +**/ |
| 4 | +var Terraformer = require('terraformer'); |
| 5 | +var WKT = require('terraformer-wkt-parser'); |
| 6 | + |
| 7 | +// packFeatures operates over a larger geoJsonObject and operates |
| 8 | +// row by row, replacing the array of temporary WKT geometries |
| 9 | +// that were inserted by the appender with a geometry collection |
| 10 | +// object. This effectively creates a proper GeoJSON object |
| 11 | +// During the operation the tempFeatures element is deleted and |
| 12 | +// replaced by the geometries element. |
| 13 | +exports.packFeatures = function (geoJsonObj) { |
| 14 | + for (var row in geoJsonObj) { |
| 15 | + var geoJsonRow = geoJsonObj[row] |
| 16 | + // array to hold all the possible multipolygons |
| 17 | + var geometries = [] |
| 18 | + for (var feature in geoJsonRow.tempFeatures) { |
| 19 | + // convert the WKT to GeoJSON |
| 20 | + var polygon = WKT.parse(geoJsonRow.tempFeatures[feature]) |
| 21 | + // push the polygon into the geometries array |
| 22 | + geometries.push(polygon) |
| 23 | + } |
| 24 | + // create the geometry collectioh |
| 25 | + var geometryCollection = new Terraformer.GeometryCollection(geometries) |
| 26 | + |
| 27 | + // Remove the tempFeatures row now that we're done |
| 28 | + delete geoJsonRow.tempFeatures; |
| 29 | + // Combine the properties with the geometryCollection we created |
| 30 | + geoJsonObj[row] = Object.assign({},geoJsonRow,geometryCollection) |
| 31 | + } |
| 32 | + return geoJsonObj |
| 33 | + |
| 34 | +}, |
| 35 | + |
| 36 | +// Append names and geometries to a geoJsonObject. |
| 37 | +// This function takes parameters geoJsonObj, name, geometry |
| 38 | +// Returns the geoJsonObj with name and a tempGeometry object apended |
| 39 | +exports.appender = function (geoJsonObj, name, geometry) { |
| 40 | + // Search for element in the geoJsonObj with a name value equal to the name parameter |
| 41 | + var found = geoJsonObj.find(value => value.properties.name === name) |
| 42 | + |
| 43 | + // if found is not undefined we push the new geometry onto the geometries stack |
| 44 | + if (found) { |
| 45 | + found.tempFeatures.push(geometry) |
| 46 | + // If not found then we create a new object and push the new geometry onto the stack |
| 47 | + } else { |
| 48 | + var l = {} |
| 49 | + l.tempFeatures = [geometry] |
| 50 | + l.properties = {name:name} |
| 51 | + geoJsonObj.push(l) |
| 52 | + } |
| 53 | + return geoJsonObj; |
| 54 | +} |
| 55 | +
|
0 commit comments