forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygonJSON.js
More file actions
214 lines (203 loc) · 6.58 KB
/
PolygonJSON.js
File metadata and controls
214 lines (203 loc) · 6.58 KB
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
import {Zondy} from '../../service/common/Base';
import {assign} from 'ol/obj.js';
import Feature from 'ol/Feature.js';
import * as ol_extent from 'ol/extent.js';
import BaseObject from 'ol/Object.js';
import Polygon from 'ol/geom/Polygon.js';
import GeometryLayout from 'ol/geom/GeometryLayout.js';
import MultiLineString from 'ol/geom/MultiLineString.js';
import MultiPoint from 'ol/geom/MultiPoint.js';
var PolygonJSON = function (opt_options) {
var options = opt_options !== undefined ? opt_options : {};
assign(this, options);
};
/**
Deserialize a MapGIS Features , and Return an array of ol.Feature
*
* Parameters:
* json-{String} | {Object},needs a Zondy.Object.FeatureSet format Javascript object.
**/
PolygonJSON.prototype.read = function (json, options) {
if (json === undefined) {
return null;
}
var obj = null;
if (typeof json === 'string') {
obj = JSON.parse(json);
} else {
obj = json;
}
if (obj !== null) {
return this.parseVectors(obj);
}
};
/*
* Parameters:
* obj: {Object},an object stand for Zondy.IGServer.WebService.REST.IGS.ExtendBaselibClass.SFeatureElementSet
*/
PolygonJSON.prototype.parseVectors = function (zfeatureset) {
// an array of OpenLayers.Feature.Vector
if (zfeatureset === undefined || zfeatureset.SFEleArray === undefined) {
return null;
}
if(!zfeatureset.SFEleArray){
return null ;
}
if (zfeatureset.SFEleArray.length == 0) {
return null;
}
var results = new Array();
for (var i = 0, len = zfeatureset.SFEleArray.length; i < len; i++) {
var zfeature = zfeatureset.SFEleArray[i];
var attribute = this.parseAttribute(zfeatureset.AttStruct, zfeature.AttValue);
var geometry = this.parseGeometry(zfeature.fGeom, zfeature.ftype);
//var vector = new OpenLayers.Feature.Vector(geometry, attribute, null);
var feature = new Feature();
feature.setGeometry(geometry);
feature.setId(zfeature.FID.toString());
feature.setProperties(attribute);
results[i] = feature;
}
return results;
};
PolygonJSON.prototype.parseBound = function (zBound) {
if (zBound === undefined) {
return null;
}
var result = ol_extent.createOrUpdate(zBound.xmin, zBound.ymin, zBound.xmax, zBound.ymax);
return result;
};
/*
* get the attribute object of the vector
* parameters :
* attstruct: {Zondy.Object.CAttStruct}
* attvalue: {ol.Object}
*/
PolygonJSON.prototype.parseAttribute = function (attstruct, attvalue) {
if (attstruct === undefined || attvalue === undefined) {
return null;
}
if (attstruct.FldName.length != attvalue.length) {
return null;
}
var attributes = new BaseObject();
for (var i = 0, len = attstruct.FldName.length; i < len; i++) {
attributes.set(attstruct.FldName[i], attvalue[i]);
};
return attributes;
};
/**
* fGeom :{Zondy.Object.FeatureGeometry}转换为{ol.geom.Geometry}
* @fGeom {Zondy.Object.FeatureGeometry} fGeom.
* @type {number} type:{1:点;2:线;3:多边形}.
* @return {ol.geom.Geometry} .
* @api stable
*/
PolygonJSON.prototype.parseGeometry = function (fGeom, type) {
var result = null;
if (type == "Unknow") {
if (fGeom.PntGeom.length > 0) {
type = 1;
}
else if (fGeom.LinGeom.length > 0) {
type = 2;
} else {
type = 3;
}
}
switch (type) {
case 1:
result = this.parseGPoint(fGeom.PntGeom);
break;
case 2:
// if the obj is type of Line
result = this.parseGLine(fGeom.LinGeom);
break;
case 3:
// if the obj is type of Region
result = this.parseGRegion(fGeom.RegGeom);
break;
}
return result;
};
/**
* gRegions Array{Zondy.Object.GRegion}转换为{ol.geom.Polygon}
* @param Array{Zondy.Object.GRegion} gRegions.
* @return {ol.geom.Polygon} .
* @api stable
*/
PolygonJSON.prototype.parseGRegion = function (gRegions) {
if (gRegions === undefined || gRegions.length === undefined || gRegions.length == 0) {
return null;
}
var m = 0;
var results = new Array();
for (var i = 0; i < gRegions.length; i++) {
var specifiedGRegion = gRegions[i];
if (specifiedGRegion === undefined || specifiedGRegion.Rings === undefined) {
return null;
}
var specifiedGRegionLength = specifiedGRegion.Rings.length;
for (var j = 0, len = specifiedGRegionLength; j < len; j++) {
var zondyAnyLine = specifiedGRegion.Rings[j];
var points = new Array();
var zondyDots = zondyAnyLine.Arcs[0].Dots;
for (var k = 0, zLen = zondyDots.length; k < zLen; k++) {
points[k] = [zondyDots[k].x, zondyDots[k].y];
}
results[m++] = points;
}
}
return new Polygon(results, GeometryLayout.XY);
};
/**
* glines Array{Zondy.Object.GLine}转换为{ol.geom.MultiLineString}
* @param Array{Zondy.Object.GLine} glines.
* @return {ol.geom.MultiLineString} .
* @api stable
*/
PolygonJSON.prototype.parseGLine = function (glines) {
if (glines === undefined || glines.length === undefined || glines.length == 0) {
return null;
}
var glinesLength;
var results = []; // an array of ol.geom.LineString;
if (!glines)
return null;
glinesLength = glines.length;
if (glinesLength === 0)
return null;
for (var i = 0, len = glines.length; i < len; i++) {
var points = new Array();
var zondyDots = glines[i].Line.Arcs[0].Dots;
for (var j = 0, dLen = zondyDots.length; j < dLen; j++) {
points[j] = [zondyDots[j].x, zondyDots[j].y];
}
results[i] = points;
}
var mulLineString = new MultiLineString(results);
//mulLineString.setLineStrings(results);
return mulLineString;
};
/**
* 将gpoint: Array{Zondy.Object.GPoint}转换为{ol.geom.MultiPoint}
* @param: Array{Zondy.Object.GPoint} gpoint.
* @return {ol.geom.MultiPoint} .
* @api stable
*/
PolygonJSON.prototype.parseGPoint = function (gpoint) {
if (gpoint === undefined || gpoint.length === undefined || gpoint.length == 0) {
return null;
}
var points = [];
var dot = null;
for (var i = 0, len = gpoint.length; i < len; i++) {
dot = gpoint[i].Dot;
//points[i] = new ol.geom.Point([dot.x, dot.y], ol.geom.GeometryLayout.XY);
points[i] = [dot.x, dot.y];
}
var result = new MultiPoint(points, GeometryLayout.XY);
return result;
};
export { PolygonJSON };
Zondy.Format.PolygonJSON = PolygonJSON;