diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 6f83eaef1f..877455a4b1 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -171,6 +171,7 @@ "OpenLayers/Format/GeoRSS.js", "OpenLayers/Format/WFS.js", "OpenLayers/Format/WKT.js", + "OpenLayers/Format/Text.js", "OpenLayers/Format/JSON.js", "OpenLayers/Format/GeoJSON.js", "OpenLayers/Layer/WFS.js", diff --git a/lib/OpenLayers/Format/Text.js b/lib/OpenLayers/Format/Text.js new file mode 100644 index 0000000000..e659997cb6 --- /dev/null +++ b/lib/OpenLayers/Format/Text.js @@ -0,0 +1,107 @@ +/* Copyright (c) 2006-2007 MetaCarta, Inc., published under a modified BSD license. + * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt + * for the full text of the license. */ + +/** + * @requires OpenLayers/Feature/Vector.js + * @requires OpenLayers/Geometry/Point.js + * + * Class: OpenLayers.Format.Text + * Read Text format. Create a new instance with the + * constructor. This reads text which is formatted like CSV text, using + * tabs as the seperator by default. It provides parsing of data originally + * used in the MapViewerService, described on the wiki. This Format is used + * by the class. + * + * Inherits from: + * - + */ +OpenLayers.Format.Text = OpenLayers.Class(OpenLayers.Format, { + + /** + * Constructor: OpenLayers.Format.Text + * Create a new parser for TSV Text. + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * this instance. + */ + initialize: function(options) { + OpenLayers.Format.prototype.initialize.apply(this, [options]); + }, + + /** + * APIMethod: read + * Return a list of features from a Tab Seperated Values text string. + * + * Parameters: + * data - {String} + * + * Returns: + * An Array of s + */ + read: function(text) { + var lines = text.split('\n'); + var columns; + var features = []; + // length - 1 to allow for trailing new line + for (var lcv = 0; lcv < (lines.length - 1); lcv++) { + var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,''); + + if (currLine.charAt(0) != '#') { /* not a comment */ + + if (!columns) { + //First line is columns + columns = currLine.split('\t'); + } else { + var vals = currLine.split('\t'); + var geometry = new OpenLayers.Geometry.Point(0,0); + var attributes = {}; + var style = {}; + var icon, iconSize, iconOffset, overflow; + var set = false; + for (var valIndex = 0; valIndex < vals.length; valIndex++) { + if (vals[valIndex]) { + if (columns[valIndex] == 'point') { + var coords = vals[valIndex].split(','); + geometry.y = parseFloat(coords[0]); + geometry.x = parseFloat(coords[1]); + set = true; + } else if (columns[valIndex] == 'lat') { + geometry.y = parseFloat(vals[valIndex]); + set = true; + } else if (columns[valIndex] == 'lon') { + geometry.x = parseFloat(vals[valIndex]); + set = true; + } else if (columns[valIndex] == 'title') + attributes['title'] = vals[valIndex]; + else if (columns[valIndex] == 'image' || + columns[valIndex] == 'icon') + style['externalGraphic'] = vals[valIndex]; + else if (columns[valIndex] == 'iconSize') { + var size = vals[valIndex].split(','); + style['graphicWidth'] = parseFloat(size[0]); + style['graphicHeight'] = parseFloat(size[1]); + } else if (columns[valIndex] == 'iconOffset') { + var offset = vals[valIndex].split(','); + style['graphicXOffset'] = parseFloat(offset[0]); + style['graphicYOffset'] = parseFloat(offset[1]); + } else if (columns[valIndex] == 'description') { + attributes['description'] = vals[valIndex]; + } else if (columns[valIndex] == 'overflow') { + attributes['overflow'] = vals[valIndex]; + } + } + } + if (set) { + var feature = new OpenLayers.Feature.Vector(geometry, attributes, style); + features.push(feature); + } + } + } + } + return features; + }, + + CLASS_NAME: "OpenLayers.Format.Text" +}); diff --git a/lib/OpenLayers/Layer/Text.js b/lib/OpenLayers/Layer/Text.js index f50953bd0e..9141dc103d 100644 --- a/lib/OpenLayers/Layer/Text.js +++ b/lib/OpenLayers/Layer/Text.js @@ -100,91 +100,65 @@ OpenLayers.Layer.Text = OpenLayers.Class(OpenLayers.Layer.Markers, { */ parseData: function(ajaxRequest) { var text = ajaxRequest.responseText; - var lines = text.split('\n'); - var columns; - // length - 1 to allow for trailing new line - for (var lcv = 0; lcv < (lines.length - 1); lcv++) { - var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,''); - - if (currLine.charAt(0) != '#') { /* not a comment */ + var parser = new OpenLayers.Format.Text(); + features = parser.read(text); + for (var i = 0; i < features.length; i++) { + var data = {}; + var feature = features[i]; + var location; + var iconSize, iconOffset; - if (!columns) { - //First line is columns - columns = currLine.split('\t'); - } else { - var vals = currLine.split('\t'); - var location = new OpenLayers.LonLat(0,0); - var title, url, description; - var icon, iconSize, iconOffset, overflow; - var set = false; - for (var valIndex = 0; valIndex < vals.length; valIndex++) { - if (vals[valIndex]) { - if (columns[valIndex] == 'point') { - var coords = vals[valIndex].split(','); - location.lat = parseFloat(coords[0]); - location.lon = parseFloat(coords[1]); - set = true; - } else if (columns[valIndex] == 'lat') { - location.lat = parseFloat(vals[valIndex]); - set = true; - } else if (columns[valIndex] == 'lon') { - location.lon = parseFloat(vals[valIndex]); - set = true; - } else if (columns[valIndex] == 'title') { - title = vals[valIndex]; - } else if (columns[valIndex] == 'image' || - columns[valIndex] == 'icon') { - url = vals[valIndex]; - } else if (columns[valIndex] == 'iconSize') { - var size = vals[valIndex].split(','); - iconSize = new OpenLayers.Size(parseFloat(size[0]), - parseFloat(size[1])); - } else if (columns[valIndex] == 'iconOffset') { - var offset = vals[valIndex].split(','); - iconOffset = new OpenLayers.Pixel(parseFloat(offset[0]), - parseFloat(offset[1])); - } else if (columns[valIndex] == 'title') { - title = vals[valIndex]; - } else if (columns[valIndex] == 'description') { - description = vals[valIndex]; - } else if (columns[valIndex] == 'overflow') { - overflow = vals[valIndex]; - } - } - } - if (set) { - var data = {}; - if (url != null) { - data.icon = new OpenLayers.Icon(url, - iconSize, - iconOffset); - } else { - data.icon = OpenLayers.Marker.defaultIcon(); + location = new OpenLayers.LonLat(feature.geometry.x, + feature.geometry.y); + + if (feature.style.graphicWidth + && feature.style.graphicHeight) { + iconSize = new OpenLayers.Size( + feature.style.graphicWidth, + feature.style.graphicHeight); + } + + // FIXME: At the moment, we only use this if we have an + // externalGraphic, because icon has no setOffset API Method. + if (feature.style.graphicXOffset + && feature.style.graphicYOffset) { + iconOffset = new OpenLayers.Size( + feature.style.graphicXOffset, + feature.style.graphicYOffset); + } + + if (feature.style.externalGraphic != null) { + data.icon = new OpenLayers.Icon(feature.style.externalGraphic, + iconSize, + iconOffset); + } else { + data.icon = OpenLayers.Marker.defaultIcon(); - //allows for the case where the image url is not - // specified but the size is. use a default icon - // but change the size - if (iconSize != null) { - data.icon.setSize(iconSize); - } - - } - if ((title != null) && (description != null)) { - data['popupContentHTML'] = '

'+title+'

'+description+'

'; - } - - data['overflow'] = overflow || "auto"; - - var feature = new OpenLayers.Feature(this, location, data); - this.features.push(feature); - var marker = feature.createMarker(); - if ((title != null) && (description != null)) { - marker.events.register('click', feature, this.markerClick); - } - this.addMarker(marker); - } + //allows for the case where the image url is not + // specified but the size is. use a default icon + // but change the size + if (iconSize != null) { + data.icon.setSize(iconSize); } } + + if ((feature.attributes.title != null) + && (feature.attributes.description != null)) { + data['popupContentHTML'] = + '

'+feature.attributes.title+'

' + + '

'+feature.attributes.description+'

'; + } + + data['overflow'] = feature.attributes.overflow || "auto"; + + var markerFeature = new OpenLayers.Feature(this, location, data); + this.features.push(markerFeature); + var marker = markerFeature.createMarker(); + if ((feature.attributes.title != null) + && (feature.attributes.description != null)) { + marker.events.register('click', markerFeature, this.markerClick); + } + this.addMarker(marker); } this.events.triggerEvent("loadend"); },