|
| 1 | +// import { Parser } from "node-sql-parser"; |
| 2 | + |
| 3 | +import {Zondy} from "../../common"; |
| 4 | + |
| 5 | +class BaseParameter { |
| 6 | + static formatParam(param) { |
| 7 | + let arr = [ |
| 8 | + "IncludeAttribute", |
| 9 | + "IncludeGeometry", |
| 10 | + "IncludeWebGraphic", |
| 11 | + "isAsc", |
| 12 | + "compareRectOnly", |
| 13 | + "enableDisplayCondition" |
| 14 | + ]; |
| 15 | + for (let i = 0; i < arr.length; i++) { |
| 16 | + if (typeof param[arr[i]] === "string") { |
| 17 | + param[arr[i]] = param[arr[i]] === "true"; |
| 18 | + } |
| 19 | + } |
| 20 | + return param; |
| 21 | + } |
| 22 | + constructor() { |
| 23 | + this.layers = []; |
| 24 | + this.pageIndex = 0; |
| 25 | + this.pagination = 100; |
| 26 | + this.resultFormat = "json"; |
| 27 | + this.IncludeAttribute = true; |
| 28 | + this.IncludeGeometry = true; |
| 29 | + this.IncludeWebGraphic = false; |
| 30 | + this.orderBy = undefined; |
| 31 | + this.isAsc = undefined; |
| 32 | + this.proj = undefined; |
| 33 | + this.fields = undefined; |
| 34 | + this.coordPrecision = undefined; |
| 35 | + this.cursorType = "backward"; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +BaseParameter.prototype.formatParameter = function(layerKey, crs) { |
| 40 | + let Obj, |
| 41 | + me = this; |
| 42 | + me._crs = crs; |
| 43 | + if (layerKey === "typename") { |
| 44 | + Obj = { |
| 45 | + layers: layerKey, |
| 46 | + pagination: "maxfeatures", |
| 47 | + rectangle: "bbox", |
| 48 | + where: "filter", |
| 49 | + geometry: "filter", |
| 50 | + objectIds: "featureId" |
| 51 | + }; |
| 52 | + if (this.where && this.geometry) { |
| 53 | + delete Obj.geometry; |
| 54 | + } |
| 55 | + Object.keys(Obj).forEach(function(key) { |
| 56 | + if (me.hasOwnProperty(key) && me[key]) { |
| 57 | + if (me[key] instanceof Array) { |
| 58 | + Obj[Obj[key]] = me[key].join(","); |
| 59 | + } else { |
| 60 | + if (key === "where" || key === "geometry") { |
| 61 | + Obj[Obj[key]] = initFilter(me[key], me.geometry, me); |
| 62 | + } else { |
| 63 | + Obj[Obj[key]] = me[key]; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + delete Obj[key]; |
| 68 | + }); |
| 69 | + return Obj; |
| 70 | + } else { |
| 71 | + Obj = { |
| 72 | + layers: layerKey, |
| 73 | + pageIndex: "Page", |
| 74 | + pagination: "pageCount", |
| 75 | + resultFormat: "f", |
| 76 | + includeAttribute: "IncludeAttribute", |
| 77 | + includeGeometry: "IncludeGeometry", |
| 78 | + includeWebGraphic: "IncludeWebGraphic", |
| 79 | + orderBy: "orderField" |
| 80 | + }; |
| 81 | + Object.keys(Obj).forEach(function(key) { |
| 82 | + if (me.hasOwnProperty(key)) { |
| 83 | + me[Obj[key]] = me[key]; |
| 84 | + delete me[key]; |
| 85 | + } |
| 86 | + }); |
| 87 | + return this; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +// function geometryToXmlGeoserver(geometry, me) { |
| 92 | +// let xml = "<" + me.spatialRelationType + ">"; |
| 93 | +// xml += "<PropertyName>the_geom</PropertyName>"; |
| 94 | +// xml += "<gml:" + geometry.type + " srsName='" + me._crs + "'>"; |
| 95 | +// xml += |
| 96 | +// "<gml:pos>" + |
| 97 | +// geometry.coordinates[0] + |
| 98 | +// " " + |
| 99 | +// geometry.coordinates[1] + |
| 100 | +// "</gml:pos>"; |
| 101 | +// xml += "</gml:" + geometry.type + ">"; |
| 102 | +// xml += "</" + me.spatialRelationType + ">"; |
| 103 | +// return xml; |
| 104 | +// } |
| 105 | +// |
| 106 | +// function geometryToXml(geometry, me) { |
| 107 | +// let xml = ""; |
| 108 | +// xml += "<" + me.spatialRelationType + ">"; |
| 109 | +// xml += "<" + geometry.type + " srsName='" + me._crs + "'>"; |
| 110 | +// if (geometry.type === "Polygon") { |
| 111 | +// xml += "<outerBoundaryIs><LinearRing>"; |
| 112 | +// xml += "<coordinates>"; |
| 113 | +// for (let i = 0; i < geometry.coordinates.length; i++) { |
| 114 | +// xml += geometry.coordinates[i].join(",") + " "; |
| 115 | +// } |
| 116 | +// xml = xml.substr(0, xml.length - 1); |
| 117 | +// xml += "</coordinates>"; |
| 118 | +// xml += "</LinearRing></outerBoundaryIs>"; |
| 119 | +// } else if (geometry.type === "MultiPoint") { |
| 120 | +// xml += "<pointMember>"; |
| 121 | +// for (let i = 0; i < geometry.coordinates.length; i++) { |
| 122 | +// xml += |
| 123 | +// "<Point><coordinates>" + |
| 124 | +// geometry.coordinates[i].join(",") + |
| 125 | +// "</coordinates></Point>"; |
| 126 | +// } |
| 127 | +// xml += "</pointMember>"; |
| 128 | +// } else if (geometry.type === "Point") { |
| 129 | +// xml += "<coordinates>" + geometry.coordinates.join(",") + "</coordinates>"; |
| 130 | +// } else { |
| 131 | +// xml += "<coordinates>"; |
| 132 | +// for (let i = 0; i < geometry.coordinates.length; i++) { |
| 133 | +// xml += geometry.coordinates[i].join(",") + " "; |
| 134 | +// } |
| 135 | +// xml = xml.substr(0, xml.length - 2); |
| 136 | +// xml += "</coordinates>"; |
| 137 | +// } |
| 138 | +// xml += "</" + geometry.type + ">"; |
| 139 | +// xml += "</" + me.spatialRelationType + ">"; |
| 140 | +// return xml; |
| 141 | +// } |
| 142 | +// function initFilter(where, geometry, me) { |
| 143 | +// if (where.length > 0) { |
| 144 | +// //sql转ast插件 |
| 145 | +// let parse = new Parser(), |
| 146 | +// //ast树对象 |
| 147 | +// astObject, |
| 148 | +// //返回的filter |
| 149 | +// filter = "<Filter>", |
| 150 | +// //比较操作符枚举 |
| 151 | +// comparisonOperators = { |
| 152 | +// "=": "PropertyIsEqualTo", |
| 153 | +// "!=": "PropertyIsNotEqualTo", |
| 154 | +// "<": "PropertyIsLessThan", |
| 155 | +// ">": "PropertyIsGreaterThan", |
| 156 | +// "<=": "PropertyIsLessThanOrEq", |
| 157 | +// ">=": "PropertyIsGreaterThanO", |
| 158 | +// LIKE: "PropertyIsLike", |
| 159 | +// BETWEEN: "PropertyIsBetween" |
| 160 | +// }, |
| 161 | +// //逻辑操作符枚举 |
| 162 | +// logicOperators = { |
| 163 | +// OR: "Or", |
| 164 | +// AND: "And", |
| 165 | +// NOT: "Not" |
| 166 | +// }; |
| 167 | +// //拼接sql |
| 168 | +// where = "Select * from t where " + where; |
| 169 | +// //转换为ast树 |
| 170 | +// astObject = parse.astify(where); |
| 171 | +// //讲逻辑操作符转换为xml |
| 172 | +// function formatLogicOperator(xml, ast, operator, geometry, me) { |
| 173 | +// xml += "<" + logicOperators[operator] + ">"; |
| 174 | +// if (operator === "NOT") { |
| 175 | +// xml += astToXml(ast.expr); |
| 176 | +// } else { |
| 177 | +// xml += astToXml(ast.left); |
| 178 | +// xml += astToXml(ast.right); |
| 179 | +// } |
| 180 | +// if (geometry) { |
| 181 | +// xml += geometryToXml(geometry, me); |
| 182 | +// } |
| 183 | +// xml += "</" + logicOperators[operator] + ">"; |
| 184 | +// return xml; |
| 185 | +// } |
| 186 | +// //将ast转换为xml |
| 187 | +// function astToXml(ast) { |
| 188 | +// let xml = ""; |
| 189 | +// if (ast.operator === "OR") { |
| 190 | +// xml += formatLogicOperator(xml, ast, "OR", geometry, me); |
| 191 | +// } else if (ast.operator === "AND") { |
| 192 | +// xml += formatLogicOperator(xml, ast, "AND", geometry, me); |
| 193 | +// } else if (ast.operator === "NOT") { |
| 194 | +// xml += formatLogicOperator(xml, ast, "NOT", geometry, me); |
| 195 | +// } else { |
| 196 | +// if (comparisonOperators.hasOwnProperty(ast.operator)) { |
| 197 | +// xml += "<" + comparisonOperators[ast.operator] + ">"; |
| 198 | +// xml += "<PropertyName>" + ast.left.column + "</PropertyName>"; |
| 199 | +// if (ast.operator === "BETWEEN") { |
| 200 | +// xml += |
| 201 | +// "<LowerBoundary><Literal>" + |
| 202 | +// ast.right.value[0].value + |
| 203 | +// "</Literal></LowerBoundary>"; |
| 204 | +// xml += |
| 205 | +// "<UpperBoundary><Literal>" + |
| 206 | +// ast.right.value[1].value + |
| 207 | +// "</Literal></UpperBoundary>"; |
| 208 | +// } else { |
| 209 | +// if (ast.right.type === "column_ref") { |
| 210 | +// xml += "<Literal>" + ast.right.column + "</Literal>"; |
| 211 | +// } else { |
| 212 | +// xml += "<Literal>" + ast.right.value + "</Literal>"; |
| 213 | +// } |
| 214 | +// } |
| 215 | +// xml += "</" + comparisonOperators[ast.operator] + ">"; |
| 216 | +// if (geometry) { |
| 217 | +// xml += geometryToXml(geometry, me); |
| 218 | +// } |
| 219 | +// } else { |
| 220 | +// throw new Error("非法的SQL关键字" + ast.operator + "!"); |
| 221 | +// } |
| 222 | +// } |
| 223 | +// return xml; |
| 224 | +// } |
| 225 | +// filter += astToXml(astObject.where); |
| 226 | +// filter += "</Filter>"; |
| 227 | +// return filter; |
| 228 | +// } else { |
| 229 | +// let filter = "<Filter>"; |
| 230 | +// filter += geometryToXml(geometry, me); |
| 231 | +// filter += "</Filter>"; |
| 232 | +// return filter; |
| 233 | +// } |
| 234 | +// } |
| 235 | +export { BaseParameter }; |
| 236 | +Zondy.Service.BaseParameter = BaseParameter; |
0 commit comments