[OpenLayers-Commits] r12351 - in sandbox/camptocamp/gpx/openlayers:
examples lib/OpenLayers/Format tests/Format
commits-20090109 at openlayers.org
commits-20090109 at openlayers.org
Fri Sep 9 05:04:43 EDT 2011
Author: pgiraud
Date: 2011-09-09 02:04:42 -0700 (Fri, 09 Sep 2011)
New Revision: 12351
Modified:
sandbox/camptocamp/gpx/openlayers/examples/vector-formats.html
sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js
sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html
Log:
First round for a GPX writer, still needs works (manage features attributes)
Modified: sandbox/camptocamp/gpx/openlayers/examples/vector-formats.html
===================================================================
--- sandbox/camptocamp/gpx/openlayers/examples/vector-formats.html 2011-09-09 09:01:23 UTC (rev 12350)
+++ sandbox/camptocamp/gpx/openlayers/examples/vector-formats.html 2011-09-09 09:04:42 UTC (rev 12351)
@@ -86,7 +86,8 @@
gml2: new OpenLayers.Format.GML.v2(gmlOptionsIn),
gml3: new OpenLayers.Format.GML.v3(gmlOptionsIn),
kml: new OpenLayers.Format.KML(kmlOptionsIn),
- atom: new OpenLayers.Format.Atom(in_options)
+ atom: new OpenLayers.Format.Atom(in_options),
+ gpx: new OpenLayers.Format.GPX(in_options)
},
'out': {
wkt: new OpenLayers.Format.WKT(out_options),
@@ -95,7 +96,8 @@
gml2: new OpenLayers.Format.GML.v2(gmlOptionsOut),
gml3: new OpenLayers.Format.GML.v3(gmlOptionsOut),
kml: new OpenLayers.Format.KML(out_options),
- atom: new OpenLayers.Format.Atom(out_options)
+ atom: new OpenLayers.Format.Atom(out_options),
+ gpx: new OpenLayers.Format.GPX(out_options)
}
};
}
@@ -199,6 +201,7 @@
<option value="gml2">GML (v2)</option>
<option value="gml3">GML (v3)</option>
<option value="wkt">Well-Known Text (WKT)</option>
+ <option value="gpx">GPX</option>
</select>
<label for="prettyPrint">Pretty print</label>
Modified: sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js
===================================================================
--- sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js 2011-09-09 09:01:23 UTC (rev 12350)
+++ sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js 2011-09-09 09:04:42 UTC (rev 12351)
@@ -45,6 +45,13 @@
* be extracted.
*/
extractAttributes: true,
+
+ /**
+ * APIProperty: gpxns
+ * {String} GPX namespace to use. Defaults to
+ * "http://www.topografix.com/GPX/1/1"
+ */
+ gpxns: "http://www.topografix.com/GPX/1/1",
/**
* Constructor: OpenLayers.Format.GPX
@@ -179,6 +186,110 @@
}
return attributes;
},
+
+ /**
+ * APIMethod: write
+ * Accept Feature Collection, and return a string.
+ *
+ * Parameters:
+ * features - {Array(<OpenLayers.Feature.Vector>)} List of features to serialize into a string.
+ */
+ write: function(features) {
+ var gpx;
+ features = OpenLayers.Util.isArray(features) ?
+ features : [features];
+ gpx = this.createElementNS(this.gpxns, "gpx");
+
+ var featuresNodes = this.buildFeaturesNodes(features);
+ for(var i=0, len=featuresNodes.length; i<len; i++) {
+ gpx.appendChild(featuresNodes[i]);
+ }
+ return OpenLayers.Format.XML.prototype.write.apply(this, [gpx]);
+ },
+
+ /**
+ * Method: buildFeaturesNodes
+ * Accept an array of <OpenLayers.Feature.Vector>, and build a array of nodes for it.
+ *
+ * Parameters:
+ * features - {Array(<OpenLayers.Feature.Vector>)}
+ *
+ * Returns:
+ * {Array(DOMElement)}
+ */
+ buildFeaturesNodes: function(features) {
+ var featuresNodes = [],
+ trkNode;
+
+ // trk node needs to be added only once
+ function createTrkNode() {
+ if (!trkNode) {
+ trkNode = this.createElementNS(this.gpxns, "trk");
+ featuresNodes.push(trkNode);
+ }
+ return trkNode;
+ }
+ for (var i = 0, len=features.length; i < len; i++) {
+ var geometry = features[i].geometry;
+ geometry = geometry.clone();
+ if (this.internalProjection && this.externalProjection) {
+ geometry.transform(this.internalProjection,
+ this.externalProjection);
+ }
+ if (geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
+ featureNode = this.createElementNS(this.gpxns, "wpt");
+ featureNode.setAttribute("lon", geometry.x);
+ featureNode.setAttribute("lat", geometry.y);
+ featuresNodes.push(featureNode);
+ } else {
+ trkNode = createTrkNode.call(this);
+ this.buildTrkSegNode(trkNode, geometry);
+ }
+ }
+
+ return featuresNodes;
+ },
+
+ /**
+ * Method: buildTrkSegNode
+ * Builds trkseg node(s) given a geometry
+ *
+ * Parameters:
+ * trkNode - {DOMElement} the node to add the trkseg nodes to
+ * geometry - {OpenLayers.Geometry}
+ */
+ buildTrkSegNode: function(trkNode, geometry) {
+ if (geometry.CLASS_NAME == "OpenLayers.Geometry.LineString" ||
+ geometry.CLASS_NAME == "OpenLayers.Geometry.LinearRing") {
+ var node = this.createElementNS(this.gpxns, "trkseg");
+ for (var i = 0, len=geometry.components.length; i < len; i++) {
+ var point = geometry.components[i];
+ node.appendChild(this.buildTrkPtNode(point));
+ }
+ trkNode.appendChild(node);
+ } else {
+ for (var i = 0, len=geometry.components.length; i < len; i++) {
+ this.buildTrkSegNode(trkNode, geometry.components[i]);
+ }
+ }
+ },
+ /**
+ * Method: buildTrkPtNode
+ * Builds a trkpt node given a point
+ *
+ * Parameters:
+ * line - {OpenLayers.Geometry.Point}
+ *
+ * Returns:
+ * {DOMElement} A trkpt node
+ */
+ buildTrkPtNode: function(point) {
+ var node = this.createElementNS(this.gpxns, "trkpt");
+ node.setAttribute("lon", point.x);
+ node.setAttribute("lat", point.y);
+ return node;
+ },
+
CLASS_NAME: "OpenLayers.Format.GPX"
});
Modified: sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html
===================================================================
--- sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html 2011-09-09 09:01:23 UTC (rev 12350)
+++ sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html 2011-09-09 09:04:42 UTC (rev 12351)
@@ -34,6 +34,77 @@
t.eq(features[2].attributes['name'], "Mark", "Text attribute node read correctly.");
t.eq(features[2].attributes['sym'], "Flag", "CDATA attribute node read correctly.");
}
+ function test_Format_GPX_serialize_points(t) {
+ t.plan(1);
+
+ var parser = new OpenLayers.Format.GPX();
+
+ var point = new OpenLayers.Geometry.Point(-111.04, 45.68);
+ var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68);
+ var features = [
+ new OpenLayers.Feature.Vector(point),
+ new OpenLayers.Feature.Vector(point2)
+ ];
+ var data = parser.write(features);
+ t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><wpt lon="-111.04" lat="45.68"/><wpt lon="-112.04" lat="45.68"/></gpx>', 'GPX serializes points correctly');
+ }
+ function test_Format_GPX_serialize_line(t) {
+ t.plan(1);
+
+ var parser = new OpenLayers.Format.GPX();
+
+ var point = new OpenLayers.Geometry.Point(-111.04, 45.68);
+ var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68);
+ var line = new OpenLayers.Geometry.LineString([point, point2]);
+ var f = new OpenLayers.Feature.Vector(line);
+ var data = parser.write(f);
+ t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes line correctly');
+ }
+ function test_Format_GPX_serialize_lines(t) {
+ t.plan(1);
+
+ var parser = new OpenLayers.Format.GPX();
+
+ var point = new OpenLayers.Geometry.Point(-111.04, 45.68);
+ var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68);
+ var line = new OpenLayers.Geometry.LineString([point, point2]);
+ var point3 = new OpenLayers.Geometry.Point(1, 2);
+ var point4 = new OpenLayers.Geometry.Point(3, 4);
+ var line2 = new OpenLayers.Geometry.LineString([point3, point4]);
+ var f = new OpenLayers.Feature.Vector(line);
+ var f2 = new OpenLayers.Feature.Vector(line2);
+ var data = parser.write([f, f2]);
+ t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes lines correctly');
+ }
+ function test_Format_GPX_serialize_multiline(t) {
+ t.plan(1);
+
+ var parser = new OpenLayers.Format.GPX();
+
+ var point = new OpenLayers.Geometry.Point(-111.04, 45.68);
+ var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68);
+ var line = new OpenLayers.Geometry.LineString([point, point2]);
+ var point3 = new OpenLayers.Geometry.Point(1, 2);
+ var point4 = new OpenLayers.Geometry.Point(3, 4);
+ var line2 = new OpenLayers.Geometry.LineString([point3, point4]);
+ var multiline = new OpenLayers.Geometry.MultiLineString([line, line2]);
+ var f = new OpenLayers.Feature.Vector(multiline);
+ var data = parser.write([f]);
+ t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/></trkseg><trkseg><trkpt lon="1" lat="2"/><trkpt lon="3" lat="4"/></trkseg></trk></gpx>', 'GPX serializes multiline correctly');
+ }
+ function test_Format_GPX_serialize_polygon(t) {
+ t.plan(1);
+
+ var parser = new OpenLayers.Format.GPX();
+
+ var point = new OpenLayers.Geometry.Point(-111.04, 45.68);
+ var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68);
+ var linearRing = new OpenLayers.Geometry.LinearRing([point, point2, point.clone()]);
+ var polygon = new OpenLayers.Geometry.Polygon([linearRing]);
+ var f = new OpenLayers.Feature.Vector(polygon);
+ var data = parser.write([f]);
+ t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/><trkpt lon="-111.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes polygon correctly');
+ }
</script>
</head>
<body>
More information about the Commits
mailing list