Text.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Feature/Vector.js
  7. * @requires OpenLayers/Geometry/Point.js
  8. */
  9. /**
  10. * Class: OpenLayers.Format.Text
  11. * Read Text format. Create a new instance with the <OpenLayers.Format.Text>
  12. * constructor. This reads text which is formatted like CSV text, using
  13. * tabs as the seperator by default. It provides parsing of data originally
  14. * used in the MapViewerService, described on the wiki. This Format is used
  15. * by the <OpenLayers.Layer.Text> class.
  16. *
  17. * Inherits from:
  18. * - <OpenLayers.Format>
  19. */
  20. OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, {
  21. /**
  22. * APIProperty: defaultStyle
  23. * defaultStyle allows one to control the default styling of the features.
  24. * It should be a symbolizer hash. By default, this is set to match the
  25. * Layer.Text behavior, which is to use the default OpenLayers Icon.
  26. */
  27. defaultStyle: null,
  28. /**
  29. * APIProperty: extractStyles
  30. * set to true to extract styles from the TSV files, using information
  31. * from the image or icon, iconSize and iconOffset fields. This will result
  32. * in features with a symbolizer (style) property set, using the
  33. * default symbolizer specified in <defaultStyle>. Set to false if you
  34. * wish to use a styleMap or OpenLayers.Style options to style your
  35. * layer instead.
  36. */
  37. extractStyles: true,
  38. /**
  39. * Constructor: OpenLayers.Format.Text
  40. * Create a new parser for TSV Text.
  41. *
  42. * Parameters:
  43. * options - {Object} An optional object whose properties will be set on
  44. * this instance.
  45. */
  46. initialize: function(options) {
  47. options = options || {};
  48. if(options.extractStyles !== false) {
  49. options.defaultStyle = {
  50. 'externalGraphic': OpenLayers.Util.getImageLocation("marker.png"),
  51. 'graphicWidth': 21,
  52. 'graphicHeight': 25,
  53. 'graphicXOffset': -10.5,
  54. 'graphicYOffset': -12.5
  55. };
  56. }
  57. OpenLayers.Format.prototype.initialize.apply(this, [options]);
  58. },
  59. /**
  60. * APIMethod: read
  61. * Return a list of features from a Tab Seperated Values text string.
  62. *
  63. * Parameters:
  64. * text - {String}
  65. *
  66. * Returns:
  67. * Array({<OpenLayers.Feature.Vector>})
  68. */
  69. read: function(text) {
  70. var lines = text.split('\n');
  71. var columns;
  72. var features = [];
  73. // length - 1 to allow for trailing new line
  74. for (var lcv = 0; lcv < (lines.length - 1); lcv++) {
  75. var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,'');
  76. if (currLine.charAt(0) != '#') { /* not a comment */
  77. if (!columns) {
  78. //First line is columns
  79. columns = currLine.split('\t');
  80. } else {
  81. var vals = currLine.split('\t');
  82. var geometry = new OpenLayers.Geometry.Point(0,0);
  83. var attributes = {};
  84. var style = this.defaultStyle ?
  85. OpenLayers.Util.applyDefaults({}, this.defaultStyle) :
  86. null;
  87. var icon, iconSize, iconOffset, overflow;
  88. var set = false;
  89. for (var valIndex = 0; valIndex < vals.length; valIndex++) {
  90. if (vals[valIndex]) {
  91. if (columns[valIndex] == 'point') {
  92. var coords = vals[valIndex].split(',');
  93. geometry.y = parseFloat(coords[0]);
  94. geometry.x = parseFloat(coords[1]);
  95. set = true;
  96. } else if (columns[valIndex] == 'lat') {
  97. geometry.y = parseFloat(vals[valIndex]);
  98. set = true;
  99. } else if (columns[valIndex] == 'lon') {
  100. geometry.x = parseFloat(vals[valIndex]);
  101. set = true;
  102. } else if (columns[valIndex] == 'title')
  103. attributes['title'] = vals[valIndex];
  104. else if (columns[valIndex] == 'image' ||
  105. columns[valIndex] == 'icon' && style) {
  106. style['externalGraphic'] = vals[valIndex];
  107. } else if (columns[valIndex] == 'iconSize' && style) {
  108. var size = vals[valIndex].split(',');
  109. style['graphicWidth'] = parseFloat(size[0]);
  110. style['graphicHeight'] = parseFloat(size[1]);
  111. } else if (columns[valIndex] == 'iconOffset' && style) {
  112. var offset = vals[valIndex].split(',');
  113. style['graphicXOffset'] = parseFloat(offset[0]);
  114. style['graphicYOffset'] = parseFloat(offset[1]);
  115. } else if (columns[valIndex] == 'description') {
  116. attributes['description'] = vals[valIndex];
  117. } else if (columns[valIndex] == 'overflow') {
  118. attributes['overflow'] = vals[valIndex];
  119. } else {
  120. // For StyleMap filtering, allow additional
  121. // columns to be stored as attributes.
  122. attributes[columns[valIndex]] = vals[valIndex];
  123. }
  124. }
  125. }
  126. if (set) {
  127. if (this.internalProjection && this.externalProjection) {
  128. geometry.transform(this.externalProjection,
  129. this.internalProjection);
  130. }
  131. var feature = new OpenLayers.Feature.Vector(geometry, attributes, style);
  132. features.push(feature);
  133. }
  134. }
  135. }
  136. }
  137. return features;
  138. },
  139. CLASS_NAME: "OpenLayers.Format.Text"
  140. });