Skip to content

Commit

Permalink
closing polygons frontend side to store paths already closed
Browse files Browse the repository at this point in the history
  • Loading branch information
javisantana committed Oct 14, 2013
1 parent 20eb3fd commit 750b385
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
6 changes: 0 additions & 6 deletions src/application/kml.py
Expand Up @@ -7,9 +7,6 @@ def path_to_kml(paths):
kml+="<coordinates>"
for p in paths[0]:
kml+= str(p[1]) + "," + str(p[0]) + ",0\n"
# close polygon
p = paths[0][0]
kml += str(p[1]) + "," + str(p[0]) + ",0\n"
kml += "</coordinates>"
kml+= "</LinearRing>"
kml+= "</outerBoundaryIs>"
Expand All @@ -19,9 +16,6 @@ def path_to_kml(paths):
kml+="<coordinates>"
for p in inner:
kml+= str(p[1]) + "," + str(p[0]) + ",0\n"
# close polygon
p = inner[0][0]
kml += str(p[1]) + "," + str(p[0]) + ",0\n"
kml += "</coordinates>"
kml+= "</LinearRing>"
kml +="</innerBoundaryIs>"
Expand Down
13 changes: 9 additions & 4 deletions src/static/js/kml.js
Expand Up @@ -7,8 +7,10 @@ google.maps.Polygon.prototype.kml = function() {
kml+= "<LinearRing>";
kml+="<coordinates>";
this.getPath().forEach(function(p) {
kml+= p.lng() + "," + p.lat() + ",0\n";
kml += p.lng() + "," + p.lat() + ",0\n";
});
var p = this.getPath()[0];
kml += p.lng() + "," + p.lat() + ",0\n";
kml += "</coordinates>";
kml+= "</LinearRing>";
kml+= "</outerBoundaryIs>";
Expand All @@ -23,11 +25,14 @@ var KML = {
kml+= "<LinearRing>";
kml+="<coordinates>";
_.each(path, function(p) {
kml+= p.lng() + "," + p.lat() + ",0\n";
kml += p.lng() + "," + p.lat() + ",0\n";
});
// close polygon
var p = path[0];
kml += p.lng() + "," + p.lat() + ",0\n";
kml += "</coordinates>";
kml+= "</LinearRing>";
kml+= "</outerBoundaryIs>";
kml += "</LinearRing>";
kml += "</outerBoundaryIs>";
kml += "</Polygon>";
return kml;
}
Expand Down
29 changes: 29 additions & 0 deletions src/static/js/models/polygon.js
Expand Up @@ -26,6 +26,35 @@ var Polygon = Backbone.Model.extend({
// we're changing the internal reference so
// change signal will not be called. Call it manually
this.trigger('change');
},

// duplicate last vertex for each polygon before sending to
// the server as GeoJSON/kml requires
toJSON: function() {
var paths = this.get('paths');
var geojsonPath = _.map(paths, function (path) {
var p = _.clone(path);
p.push(p[0]);
return p;
});
return {
type: this.attributes.type,
paths: geojsonPath
};
},

parse: function(resp) {
// remove duplicated points from paths
// if the first and the last one are equal to maintain compatibility
// modify resp in place
_.each(resp.paths, function(path) {
if(_.isEqual(path[0], path[path.length - 1])) {
path.splice(path.length - 1, 1);
}
});

return resp;

}


Expand Down
45 changes: 45 additions & 0 deletions src/tests/frontend/polygon.test.js
@@ -0,0 +1,45 @@

module("polygon")

test('tojson', function() {
var p = new Polygon({
type: 'test',
paths: [
[ [0, 1], [1, 1], [1, 0] ],
[ [5, 1], [1, 1], [1, 5] ]
]
});
equal(_.isEqual(p.toJSON().paths, [
[ [0, 1], [1, 1], [1, 0], [0, 1] ],
[ [5, 1], [1, 1], [1, 5], [5, 1] ]
]), true)
});

test('parse', function() {
var p = new Polygon()
var res = p.parse({
type: 'test',
paths: [
[ [0, 1], [1, 1], [1, 0] ],
[ [5, 1], [1, 1], [1, 5] ]
]
});
equal(_.isEqual(res.paths, [
[ [0, 1], [1, 1], [1, 0] ],
[ [5, 1], [1, 1], [1, 5] ]
]), true)

var res = p.parse({
type: 'test',
paths: [
[ [0, 1], [1, 1], [1, 0], [0, 1] ],
[ [5, 1], [1, 1], [1, 5], [5, 1] ]
]
});

equal(_.isEqual(res.paths, [
[ [0, 1], [1, 1], [1, 0] ],
[ [5, 1], [1, 1], [1, 5] ]
]), true)

});
42 changes: 23 additions & 19 deletions src/tests/frontend/test.html
Expand Up @@ -3,10 +3,14 @@
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
<script type="text/javascript" src="../../static/js/underscore.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script type="text/javascript" src="../../static/js/libs/underscore-min.js"></script>
<script type="text/javascript" src="../../static/js/libs/backbone.js"></script>
<script type="text/javascript" src="../../static/js/pixel_ops.js"></script>
<script type="text/javascript" src="../../static/js/models/polygon.js"></script>

<script type="text/javascript" src="./polygon.test.js"></script>

<script>
$(document).ready(function(){
Expand Down Expand Up @@ -70,9 +74,9 @@
for(var i=1; i<5; ++i) {
for(var j=1; j<5; ++j) {
var p = 4*(i*6 + j);
equals(px[p], 255);
equals(px[p + 1], 254);
equals(px[p + 2], 253);
equal(px[p], 255);
equal(px[p + 1], 254);
equal(px[p + 2], 253);

}
}
Expand All @@ -82,43 +86,43 @@

test("should contour a shape with same color", function() {
var poly = contour(generate_pixels(bitmap), 6, 6, 1, 1);
equals( poly.length, 14 ,"test");
equal( poly.length, 14 ,"test");
var poly = contour(generate_pixels(bitmap), 6, 6, 3, 3);
equals( poly.length, 14 ,"test");
equal( poly.length, 14 ,"test");
});

test("should contour a small shape with same color", function() {
var poly = contour(generate_pixels(small_bitmap), 4, 4, 1, 1);
equals( poly.length, 4 ,"test");
equal( poly.length, 4 ,"test");
});

test("should does NOT contour 1px shape with same color", function() {
var poly = contour(generate_pixels(one_px_bitmap), 4, 4, 1, 1);
equals( poly.length, 0 ,"test");
equal( poly.length, 0 ,"test");
});

test("should contour a shape with holes with same color", function() {
var poly = contour(generate_pixels(bitmap_hole), 8, 8, 1, 1);
equals( poly.length, 22 ,"test");
equal( poly.length, 22 ,"test");
var poly = contour(generate_pixels(bitmap_hole), 8, 8, 3, 2);
equals( poly.length, 22 ,"test");
equal( poly.length, 22 ,"test");
});

test("should find holes", function() {
var outer_poly = contour(generate_pixels(bitmap_hole), 8, 8, 2, 2);
var poly = inner_polygons(generate_pixels(bitmap_hole), 8, 8, outer_poly, [1,1,1]);
equals( poly.length, 1 ,"test");
equals( poly[0].length, 8 ,"test");
equals( poly[0][0][0], 3 ,"test");
equals( poly[0][0][1], 4 ,"test");
equal( poly.length, 1 ,"test");
equal( poly[0].length, 8 ,"test");
equal( poly[0][0][0], 3 ,"test");
equal( poly[0][0][1], 4 ,"test");

});
test("should find 2 holes", function() {
var outer_poly = contour(generate_pixels(bitmap_hole_2), 8, 8, 2, 2);
var poly = inner_polygons(generate_pixels(bitmap_hole_2), 8, 8, outer_poly, [1,1,1]);
equals( poly.length, 2 ,"test");
equals( poly[0].length, 2 ,"test");
equals( poly[1].length, 2 ,"test");
equal( poly.length, 2 ,"test");
equal( poly[0].length, 2 ,"test");
equal( poly[1].length, 2 ,"test");
});

});
Expand Down

0 comments on commit 750b385

Please sign in to comment.