source: Dev/trunk/src/client/dojox/geo/openlayers/JsonImport.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.1 KB
Line 
1define([
2        "dojo/_base/declare",
3        "dojo/_base/xhr",
4        "dojo/_base/lang",
5        "dojo/_base/array",
6        "./LineString",
7        "./Collection",
8        "./GeometryFeature"
9], function(declare, xhr, lang, array, LineString, Collection, GeometryFeature){
10
11        /*=====
12        dojox.geo.openlayers.__JsonImportArgs = {
13                // summary:
14                //              The keyword arguments that can be passed in a JsonImport constructor.
15                // url: String
16                //              The url pointing to the JSON file to load.
17                // nextFeature: function
18                //              The function called each time a feature is read. The function is called with a GeometryFeature as argument.
19                // error: function
20                //              Error callback called if something fails.
21        };
22        =====*/
23
24        return declare("dojox.geo.openlayers.JsonImport", null, {
25                // summary:
26                //              Class to load JSON formated ShapeFile as output of the JSon Custom Map Converter.
27                // description:
28                //              This class loads JSON formated ShapeFile produced by the JSon Custom Map Converter.
29                //              When loading the JSON file, it calls a iterator function each time a feature is read.
30                //              This iterator function is provided as parameter to the constructor.
31                //
32                constructor : function(params){
33                        // summary:
34                        //              Construct a new JSON importer.
35                        // params: dojox.geo.openlayers.__JsonImportArgs
36                        //              The parameters to initialize this JsonImport instance.
37                        this._params = params;
38                },
39
40                loadData: function(){
41                        // summary:
42                        //              Triggers the loading.
43                        var p = this._params;
44                        xhr.get({
45                                url: p.url,
46                                handleAs: "json",
47                                sync: true,
48                                load: lang.hitch(this, this._gotData),
49                                error: lang.hitch(this, this._loadError)
50                        });
51                },
52
53                _gotData: function(/* Object */items){
54                        // summary:
55                        //              Called when loading is complete.
56                        // tags:
57                        //              private
58                        var nf = this._params.nextFeature;
59                        if(!lang.isFunction(nf)){
60                                return;
61                        }
62
63                        var extent = items.layerExtent;
64                        var ulx = extent[0];
65                        var uly = extent[1];
66                        var lrx = ulx + extent[2];
67                        var lry = uly + extent[3];
68
69                        var extentLL = items.layerExtentLL;
70                        var x1 = extentLL[0];
71                        var y1 = extentLL[1];
72                        var x2 = x1 + extentLL[2];
73                        var y2 = y1 + extentLL[3];
74
75                        var ulxLL = x1;
76                        var ulyLL = y2;
77                        var lrxLL = x2;
78                        var lryLL = y1;
79
80                        var features = items.features;
81
82                        for( var f in features){
83                                var o = features[f];
84                                var s = o["shape"];
85                                var gf = null;
86                                if(lang.isArray(s[0])){
87
88                                        var a = new Array();
89                                        array.forEach(s, function(item){
90                                                var ls = this._makeGeometry(item, ulx, uly, lrx, lry, ulxLL, ulyLL, lrxLL, lryLL);
91                                                a.push(ls);
92                                        }, this);
93                                        var g = new Collection(a);
94                                        gf = new GeometryFeature(g);
95                                        nf.call(this, gf);
96
97                                }else{
98                                        gf = this._makeFeature(s, ulx, uly, lrx, lry, ulxLL, ulyLL, lrxLL, lryLL);
99                                        nf.call(this, gf);
100                                }
101                        }
102                        var complete = this._params.complete;
103                        if(lang.isFunction(complete)){
104                                complete.call(this, complete);
105                        }
106                },
107
108                _makeGeometry: function(/* Array */s, /* Float */ulx, /* Float */uly, /* Float */lrx, /* Float */
109                lry, /* Float */ulxLL, /* Float */ulyLL, /* Float */lrxLL, /* Float */lryLL){
110                        // summary:
111                        //              Make a geometry with the specified points.
112                        // tags:
113                        //              private
114                        var a = [];
115                        var k = 0.0;
116                        for( var i = 0; i < s.length - 1; i += 2){
117                                var x = s[i];
118                                var y = s[i + 1];
119
120                                k = (x - ulx) / (lrx - ulx);
121                                var px = k * (lrxLL - ulxLL) + ulxLL;
122
123                                k = (y - uly) / (lry - uly);
124                                var py = k * (lryLL - ulyLL) + ulyLL;
125
126                                a.push({
127                                        x: px,
128                                        y: py
129                                });
130
131                        }
132                        var ls = new LineString(a);
133                        return ls; // LineString
134                },
135
136                _makeFeature: function(/* Array */s, /* Float */ulx, /* Float */uly, /* Float */lrx, /* Float */
137                lry, /* Float */ulxLL, /* Float */ulyLL, /* Float */lrxLL, /* Float */lryLL){
138                        // summary:
139                        //              Make a GeometryFeature with the specified points.
140                        // tags:
141                        //              private
142                        var ls = this._makeGeometry(s, ulx, uly, lrx, lry, ulxLL, ulyLL, lrxLL, lryLL);
143                        var gf = new GeometryFeature(ls);
144                        return gf;
145                },
146
147                _loadError: function(){
148                        // summary:
149                        //              Called when an error occurs. Calls the error function is provided in the parameters.
150                        // tags:
151                        //              private
152                        var f = this._params.error;
153                        if(lang.isFunction(f)){
154                                f.apply(this, parameters);
155                        }
156                }
157        });
158});
Note: See TracBrowser for help on using the repository browser.