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
|
//>>built
define("dojox/geo/openlayers/JsonImport", ["dojo/_base/kernel",
"dojo/_base/declare",
"dojo/_base/xhr",
"dojo/_base/lang",
"dojo/_base/array",
"dojox/geo/openlayers/LineString",
"dojox/geo/openlayers/Collection",
"dojo/data/ItemFileReadStore",
"dojox/geo/openlayers/GeometryFeature"], function(dojo, declare, xhr, lang, array, LineString, Collection,
ItemFileReadStore, GeometryFeature){
return declare("dojox.geo.openlayers.JsonImport", null, {
// summary:
// Class to load JSON formated ShapeFile as output of the JSon Custom Map Converter.
// description:
// This class loads JSON formated ShapeFile produced by the JSon Custom Map Converter.
// When loading the JSON file, it calls a iterator function each time a feature is read.
// This iterator function is provided as parameter to the constructor.
//
constructor : function(/* Object */params){
// summary:
// Construct a new JSON importer.
// description:
// Construct a new JSON importer with the specified parameters. These parameters are
// passed through an Object and include:
// <ul>
// <li> url : <em>url</em> </li> The url pointing to the JSON file to load.
// <li> nextFeature : <em>function</em> </li> The function called each time a feature is read.
// The function is called with a GeometryFeature as argument.
// <li> error : <em>function</em> </li> Error function called if something goes wrong.
// </ul>
this._params = params;
},
loadData : function(){
// summary:
// Triggers the loading.
var p = this._params;
xhr.get({
url : p.url,
handleAs : "json",
sync : true,
load : lang.hitch(this, this._gotData),
error : lang.hitch(this, this._loadError)
});
},
_gotData : function(/* Object */items){
// summary:
// Called when loading is complete.
// tags:
// private
var nf = this._params.nextFeature;
if (!lang.isFunction(nf))
return;
var extent = items.layerExtent;
var ulx = extent[0];
var uly = extent[1];
var lrx = ulx + extent[2];
var lry = uly + extent[3];
var extentLL = items.layerExtentLL;
var x1 = extentLL[0];
var y1 = extentLL[1];
var x2 = x1 + extentLL[2];
var y2 = y1 + extentLL[3];
var ulxLL = x1;
var ulyLL = y2;
var lrxLL = x2;
var lryLL = y1;
var features = items.features;
for ( var f in features) {
var o = features[f];
var s = o["shape"];
var gf = null;
if (lang.isArray(s[0])) {
var a = new Array();
array.forEach(s, function(item){
var ls = this._makeGeometry(item, ulx, uly, lrx, lry, ulxLL, ulyLL, lrxLL, lryLL);
a.push(ls);
}, this);
var g = new Collection(a);
gf = new GeometryFeature(g);
nf.call(this, gf);
} else {
gf = this._makeFeature(s, ulx, uly, lrx, lry, ulxLL, ulyLL, lrxLL, lryLL);
nf.call(this, gf);
}
}
var complete = this._params.complete;
if (lang.isFunction(complete))
complete.call(this, complete);
},
_makeGeometry : function(/* Array */s, /* Float */ulx, /* Float */uly, /* Float */lrx, /* Float */
lry, /* Float */ulxLL, /* Float */ulyLL, /* Float */lrxLL, /* Float */lryLL){
// summary:
// Make a geometry with the specified points.
// tags:
// private
var a = [];
var k = 0.0;
for ( var i = 0; i < s.length - 1; i += 2) {
var x = s[i];
var y = s[i + 1];
k = (x - ulx) / (lrx - ulx);
var px = k * (lrxLL - ulxLL) + ulxLL;
k = (y - uly) / (lry - uly);
var py = k * (lryLL - ulyLL) + ulyLL;
a.push({
x : px,
y : py
});
}
var ls = new LineString(a);
return ls;
},
_makeFeature : function(/* Array */s, /* Float */ulx, /* Float */uly, /* Float */lrx, /* Float */
lry, /* Float */ulxLL, /* Float */ulyLL, /* Float */lrxLL, /* Float */lryLL){
// summary:
// Make a GeometryFeature with the specified points.
// tags:
// private
var ls = this._makeGeometry(s, ulx, uly, lrx, lry, ulxLL, ulyLL, lrxLL, lryLL);
var gf = new GeometryFeature(ls);
return gf;
},
_loadError : function(){
// summary:
// Called when an error occurs. Calls the error function is provided in the parameters.
// tags:
// private
var f = this._params.error;
if (lang.isFunction(f))
f.apply(this, parameters);
}
});
});
|