forked from kashif/geonode
/
test.js
153 lines (96 loc) · 10.7 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var assert = require('assert');
var geonode = require("./geonode");
var sys = require("sys");
var rss = process.memoryUsage().rss;
function assertGeomsEquals(ga, gb) {
return assert.ok(ga.equals(gb));
}
function assertGeomsEqualsExact(ga, gb, tol) {
return assert.ok(ga.equalsExact(gb, tol));
}
var Geometry = geonode.Geometry;
var geom = new Geometry();
assert.ok(geom instanceof Geometry);
assert.ok(/^3\.[0-9.]+-CAPI-1\.[567]\.[0-9]$/.test(geom._geosVersion));
assert.equal(geom.toWkt(), "");
geom.fromWkt("POINT(0 0)");
// FIXME this kind of string matching can be fragile with WKTs
assert.equal(geom.toWkt(), "POINT (0.0000000000000000 0.0000000000000000)");
// You can also initialize a Geometry with a WKT passed to the constructor
var pt = new Geometry("POINT(1 1)");
assert.ok(pt instanceof Geometry);
assert.equal(pt.toWkt(), "POINT (1.0000000000000000 1.0000000000000000)");
var poly = new Geometry("POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))");
polyWkt = "POLYGON ((0.0000000000000000 0.0000000000000000, 0.0000000000000000 2.0000000000000000, 2.0000000000000000 2.0000000000000000, 2.0000000000000000 0.0000000000000000, 0.0000000000000000 0.0000000000000000))";
assert.equal(poly.toWkt(), polyWkt);
assert.throws("new Geometry(\"SOMEGROSSLYINVALIDWKT\")");
assert.throws("var g = new Geometry(); g.fromWkt(\"SOMEGROSSLYINVALIDWKT\")");
assert.ok(poly.preparedContains(pt));
assert.ok(!pt.preparedContains(poly));
assert.ok(!poly.preparedContains(new Geometry("POINT(3 3)")));
assert.ok(!poly.isEmpty());
assert.ok(new Geometry("POINT EMPTY").isEmpty());
assert.ok(poly.isValid());
assert.ok(!new Geometry("POLYGON((0 0, 2 2, 0 2, 2 0, 0 0))").isValid());
assert.ok(poly.isSimple());
assert.ok(poly.preparedIntersects(new Geometry("POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))")));
assert.ok(!poly.preparedIntersects(new Geometry("LINESTRING(3 3, 4 4)")));
assert.ok(poly.preparedIntersects(new Geometry("POINT(0 0)")));
assert.ok(poly.preparedContainsProperly(pt));
assert.ok(poly.preparedContains(new Geometry("LINESTRING(0 0, 0 2, 1 1)")));
assert.ok(!poly.preparedContainsProperly(new Geometry("LINESTRING(0 0, 0 2, 1 1)")));
assert.ok(poly.preparedCovers(new Geometry("POINT(0 0)")));
poly.srid = 4326;
assert.equal(poly.srid, 4326);
assert.equal(poly.type, "Polygon");
assert.equal(new Geometry("POINT(0 0)").type, "Point");
assert.equal(poly.area, 4);
assert.equal(new Geometry("LINESTRING(0 0, 1 1)").length, Math.sqrt(2));
assert.equal(new Geometry("POINT(0 0)").distance(new Geometry("POINT(1 1)")), Math.sqrt(2));
assertGeomsEquals(new Geometry("POLYGON((0 1, 1 0, 2 1, 1 2, 0 1))").envelope, new Geometry("POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))"));
assertGeomsEquals(poly.intersection(new Geometry("POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))")), new Geometry("POLYGON((1 2, 2 2, 2 1, 1 1, 1 2))"));
var myCircle = new Geometry("POLYGON ((2.0000000000000000 1.0000000000000000, 1.9807852804032304 0.8049096779838719, 1.9238795325112870 0.6173165676349106, 1.8314696123025456 0.4444297669803983, 1.7071067811865481 0.2928932188134531, 1.5555702330196031 0.1685303876974553, 1.3826834323650909 0.0761204674887137, 1.1950903220161297 0.0192147195967698, 1.0000000000000016 0.0000000000000000, 0.8049096779838736 0.0192147195967692, 0.6173165676349122 0.0761204674887125, 0.4444297669803995 0.1685303876974537, 0.2928932188134541 0.2928932188134509, 0.1685303876974562 0.4444297669803957, 0.0761204674887143 0.6173165676349077, 0.0192147195967701 0.8049096779838688, 0.0000000000000000 0.9999999999999968, 0.0192147195967689 1.1950903220161249, 0.0761204674887118 1.3826834323650863, 0.1685303876974525 1.5555702330195991, 0.2928932188134495 1.7071067811865446, 0.4444297669803942 1.8314696123025427, 0.6173165676349064 1.9238795325112852, 0.8049096779838678 1.9807852804032295, 0.9999999999999962 2.0000000000000000, 1.1950903220161249 1.9807852804032311, 1.3826834323650867 1.9238795325112881, 1.5555702330195995 1.8314696123025469, 1.7071067811865455 1.7071067811865497, 1.8314696123025438 1.5555702330196044, 1.9238795325112859 1.3826834323650921, 1.9807852804032300 1.1950903220161304, 2.0000000000000000 1.0000000000000000))");
assertGeomsEqualsExact(new Geometry("POINT(1 1)").buffer(1), myCircle, 0.01);
assertGeomsEquals(new Geometry("POINT(1 1)").buffer(1, 1), new Geometry("POLYGON ((2.0000000000000000 1.0000000000000000, 1.0000000000000016 0.0000000000000000, 0.0000000000000000 0.9999999999999968, 0.9999999999999953 2.0000000000000000, 2.0000000000000000 1.0000000000000000))"));
assertGeomsEquals(new Geometry("POLYGON((1 1, 3 2, 2 1, 3 0, 1 1))").convexHull, new Geometry("POLYGON ((3 0, 1 1, 3 2, 3 0))"));
assertGeomsEquals(poly.difference(new Geometry("POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))")), new Geometry("POLYGON ((0 0, 0 2, 1 2, 1 1, 2 1, 2 0, 0 0))"));
assertGeomsEquals(poly.symDifference(new Geometry("POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))")), new Geometry("MULTIPOLYGON (((0 0, 0 2, 1 2, 1 1, 2 1, 2 0, 0 0)), ((2 1, 2 2, 1 2, 1 3, 3 3, 3 1, 2 1)))"));
assertGeomsEquals(new Geometry("LINESTRING(1 1, 0 0, -1 1)").boundary, new Geometry("MULTIPOINT(1 1, -1 1)"));
assertGeomsEquals(poly.union(new Geometry("POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))")), new Geometry("POLYGON ((0 0, 0 2, 1 2, 1 3, 3 3, 3 1, 2 1, 2 0, 0 0))"));
assertGeomsEquals(poly.pointOnSurface, new Geometry("POINT(1 1)"));
assertGeomsEquals(poly.centroid, new Geometry("POINT(1 1)"));
assertGeomsEquals(myCircle.simplify(0.1), new Geometry("POLYGON ((2.0000000000000000 1.0000000000000000, 1.7071067811865481 0.2928932188134531, 1.0000000000000016 0.0000000000000000, 0.2928932188134541 0.2928932188134509, 0.0000000000000000 0.9999999999999968, 0.2928932188134495 1.7071067811865446, 0.9999999999999962 2.0000000000000000, 1.7071067811865455 1.7071067811865497, 2.0000000000000000 1.0000000000000000))"));
assert.equal(poly.relate(new Geometry("POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))")), "212101212");
assert.ok(poly.relate(new Geometry("POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))"), "212101212"));
var missionBay = new Geometry("POLYGON((-122.396481 37.781929,-122.396181 37.781729,-122.395681 37.781329,-122.395381 37.781029,-122.394981 37.780629,-122.394481 37.780329,-122.394081 37.780129,-122.392581 37.778829,-122.391681 37.778229,-122.391281 37.777929,-122.390981 37.777529,-122.390356 37.777082,-122.390281 37.777029,-122.389981 37.776729,-122.387081 37.777178,-122.386996 37.776336,-122.384981 37.776529,-122.384881 37.775329,-122.386973 37.775139,-122.386881 37.774529,-122.382081 37.774729,-122.381481 37.771829,-122.384781 37.773329,-122.386681 37.773229,-122.386581 37.772929,-122.386781 37.772729,-122.385981 37.770729,-122.383281 37.769829,-122.383481 37.769329,-122.385581 37.770029,-122.384781 37.768729,-122.385381 37.767929,-122.384281 37.768029,-122.383881 37.767729,-122.383581 37.767729,-122.383481 37.767429,-122.384881 37.767329,-122.384781 37.767129,-122.382981 37.767129,-122.382981 37.766829,-122.385681 37.766729,-122.385781 37.765929,-122.386381 37.765829,-122.386581 37.765529,-122.386181 37.765429,-122.385981 37.764629,-122.386881 37.763529,-122.386581 37.763229,-122.387881 37.763029,-122.387881 37.763929,-122.387781 37.764329,-122.388881 37.764329,-122.389781 37.764229,-122.390781 37.764129,-122.391781 37.764129,-122.392981 37.764129,-122.393121 37.764129,-122.393181 37.764429,-122.393581 37.765629,-122.393881 37.765829,-122.394681 37.766529,-122.395477 37.767167,-122.396673 37.768194,-122.399881 37.770629,-122.400081 37.770929,-122.400481 37.771229,-122.400781 37.771029,-122.402081 37.770029,-122.40231 37.770014,-122.402875 37.769976,-122.403581 37.769929,-122.404181 37.769829,-122.404781 37.769729,-122.405164 37.769721,-122.405181 37.769829,-122.405781 37.770929,-122.406181 37.772029,-122.406681 37.772729,-122.406582 37.773329,-122.406582 37.774729,-122.406282 37.775129,-122.406082 37.775429,-122.405837 37.775739,-122.405287 37.776044,-122.404782 37.776329,-122.404281 37.776729,-122.403681 37.777029,-122.402981 37.777429,-122.402281 37.777829,-122.401781 37.778229,-122.401281 37.778529,-122.400681 37.779029,-122.398881 37.780329,-122.396481 37.781929))")
assertGeomsEquals(missionBay.topologyPreserveSimplify(0.5), new Geometry("POLYGON ((-122.3964809999999943 37.7819289999999981, -122.3820809999999994 37.7747290000000007, -122.3865810000000067 37.7632290000000026, -122.4051639999999992 37.7697209999999970, -122.3964809999999943 37.7819289999999981))"))
//////////////////////
var Projection = geonode.Projection;
var lonlat = new Projection("+init=epsg:4326");
// Sanity checks, we're getting good stuff
assert.ok(lonlat instanceof Projection);
assert.equal(lonlat.definition, " +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0");
assert.throws('new Projection();');
assert.throws('new Projection("INVALID_PROJECTION");');
assert.throws('Projection.transform();');
// We're going to use the "World Mercator" projection for testing
var worldmerc = new Projection("+proj=merc +lat_ts=0 +lon_0=0 +k=1.000000 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0")
// Test Point Projection
Projection.transform(lonlat, worldmerc, pt);
assert.equal("POINT (111319.4907932735659415 110579.9652218968840316)", pt.toWkt());
// Test Line Projection
var line = new Geometry("LINESTRING(0 0, 1 1)");
Projection.transform(lonlat, worldmerc, line);
assert.equal("LINESTRING (0.0000000000000000 0.0000000007081155, 111319.4907932735659415 110579.9652218968840316)", line.toWkt());
// Test Polygon Projection
var polyProj = new Geometry("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2))");
Projection.transform(lonlat, worldmerc, polyProj);
assert.equal("POLYGON ((111319.4907932735659415 110579.9652218968840316, 556597.4539663678733632 110579.9652218968840316, 556597.4539663678733632 553583.8468157640891150, 111319.4907932735659415 553583.8468157640891150, 111319.4907932735659415 110579.9652218968840316), (222638.9815865471318830 221194.0771677151496988, 333958.4723798207123764 221194.0771677151496988, 333958.4723798207123764 331876.5342131220386364, 222638.9815865471318830 331876.5342131220386364, 222638.9815865471318830 221194.0771677151496988))",
polyProj.toWkt());
// Test Polygon Projection Backwards
Projection.transform(worldmerc, lonlat, polyProj);
assertGeomsEqualsExact(new Geometry("POLYGON ((0.9999999999999998 0.9999999999865795, 5.0000000000000000 0.9999999999865795, 5.0000000000000000 4.9999999999995621, 0.9999999999999998 4.9999999999995621, 0.9999999999999998 0.9999999999865795), (1.9999999999999996 1.9999999999732607, 3.0000000000000000 1.9999999999732607, 3.0000000000000000 2.9999999999997500, 1.9999999999999996 2.9999999999997500, 1.9999999999999996 1.9999999999732607))"),
polyProj, 0.001);
//////////////////////
sys.puts("Heap increased by " + ((process.memoryUsage().rss - rss) / 1024) + " KB");
sys.puts("Tests pass!");