JsonImport.js 4.3 KB

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