-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from erelsgl/nodejs-examples
Nodejs examples
- Loading branch information
Showing
6 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Node.JS examples | ||
================ | ||
|
||
To run the examples, you have to install jsts from npm: | ||
|
||
npm install -g jsts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Example of calculating intersection between a polygon and a line or a point. | ||
*/ | ||
|
||
var jsts = require("jsts"); | ||
var show = require("./show"); // just for showing the geometric objects during the example. | ||
|
||
var reader = new jsts.io.WKTReader(); | ||
|
||
|
||
var polygon = reader.read('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'); // the square between (0,0) and (10,10) | ||
var line1 = reader.read('LINESTRING(-10 8, 20 8)'); | ||
var line2 = reader.read('LINESTRING(0 11, 11 11)'); | ||
var point1 = reader.read("POINT(7 7)"); | ||
var point2 = reader.read("POINT(11 11)"); | ||
|
||
show("polygon",polygon); | ||
show("line1",line1); | ||
show("line2",line2); | ||
show("point1",point1); | ||
show("point2",point2); | ||
|
||
show("line1 ^ polygon", line1.intersection(polygon)); | ||
show("polygon ^ line2", polygon.intersection(line2)); | ||
|
||
show("point1 ^ polygon", point1.intersection(polygon)); | ||
show("polygon ^ point2", polygon.intersection(point2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Example of calculating intersection and union between polygons, normalization and equality checking. | ||
*/ | ||
|
||
var jsts = require("jsts"); | ||
var show = require("./show"); // just for showing the geometric objects during the example. | ||
|
||
var reader = new jsts.io.WKTReader(); | ||
|
||
var a = reader.read('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'); // the square between (0,0) and (10,10) | ||
var b = reader.read('POLYGON((5 5, 5 20, 20 20, 20 5, 5 5))'); // the square between (5,5) and (20,20) | ||
var c = reader.read('POLYGON((15 15, 15 20, 20 20, 20 15, 15 15))'); // the square between (15,15) and (20,20) | ||
var d = reader.read('POLYGON((5 5, 10 10, 5 15, 0 10, 5 5))'); // a rotated square | ||
|
||
show("a",a); | ||
show("b",b); | ||
show("c",c); | ||
show("d",d); | ||
|
||
console.log(a.intersects(b)? "a intersects b": "a does not intersect b"); | ||
var a_intersection_b = a.intersection(b); | ||
show("a ^ b",a_intersection_b); // the square between (5,5) and (10,10) | ||
var e = reader.read('POLYGON((5 5, 5 10, 10 10, 10 5, 5 5))'); // also the square between (5,5) and (10,10) | ||
show("e",e); | ||
show("a^b equalsExact e",a_intersection_b.equalsExact(e)); // false | ||
show("a^b equalsTopo e",a_intersection_b.equalsTopo(e)); // true | ||
a_intersection_b = a_intersection_b.norm(); | ||
e = e.norm(); | ||
show("a^b normalized",a_intersection_b); | ||
show("e normalized",e); | ||
show("a^b normalized equalsExact e normalized",a_intersection_b.equalsExact(e)); // false | ||
|
||
console.log(a.intersects(c)? "a intersects c": "a does not intersect c"); | ||
show("a ^ c",a.intersection(c)); // empty intersection | ||
|
||
console.log(a.intersects(d)? "a intersects d": "a does not intersect d"); | ||
show("a ^ d",a.intersection(d)); // a RAIT | ||
|
||
show("a U b",a.union(b)); // octagon | ||
show("a U c",a.union(c)); // two disjoint squares | ||
show("a U d",a.union(d)); // a house-shaped pentagon | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Calculating the various boolean relations (overlaps, contains, etc.) | ||
*/ | ||
|
||
var jsts = require("jsts"); | ||
|
||
var reader = new jsts.io.WKTReader(); | ||
|
||
function booleanRelationToString(shape1, shape2, relationName) { | ||
var relationHolds = shape1[relationName](shape2); | ||
if (relationHolds) { | ||
return relationName+"s"; | ||
} else { | ||
return "does not "+relationName; | ||
} | ||
} | ||
|
||
var reference = reader.read('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'); // the square between (0,0) and (10,10) | ||
|
||
var others = {}; | ||
others.internalPoint = reader.read("POINT(7 7)"); // a point inside the square | ||
others.boundaryPoint = reader.read("POINT(10 5)"); // a point on the boundary of the square | ||
others.externalPoint = reader.read("POINT(11 11)"); // a point outside the square | ||
others.farawaySquare = reader.read('POLYGON((20 20, 20 30, 30 30, 30 20, 20 20))'); // the square between (20,20) and (30,30) | ||
others.cornerSquare = reader.read('POLYGON((10 10, 10 30, 30 30, 30 10, 10 10))'); // the square between (10,10) and (30,30) | ||
others.sideSquare = reader.read('POLYGON((10 0, 10 30, 30 30, 30 0, 10 0))'); // the rectangle between (10,0) and (30,30) | ||
others.overlappingSquare = reader.read('POLYGON((5 5, 5 20, 20 20, 20 5, 5 5))'); // the square between (5,5) and (20,20) | ||
|
||
var relations = [ | ||
"disjoint", "touches", | ||
"intersects", //"crosses", | ||
"within", "contains", | ||
"overlaps", | ||
//"covers", "coveredBy", | ||
"relate" | ||
]; | ||
|
||
|
||
for (var p in others) { | ||
console.log("\n"+p+"\n-----------------\n"); | ||
var point = others[p]; | ||
for (var r in relations) { | ||
var relation = relations[r]; | ||
console.log("reference."+relation+"("+p+") = "+reference[relation](point)); | ||
console.log(p+"."+relation+"(reference) = "+point[relation](reference)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* A simple routine for showing geometric objects in the examples. | ||
*/ | ||
|
||
|
||
//var util = require("util"); | ||
module.exports = function (title, polygon) { | ||
//console.log(title+" = \n"+util.inspect(polygon,null,3)+" "+(polygon.isEmpty? (polygon.isEmpty() ?" EMPTY": "NONEMPTY"): "")+"\n\n"); | ||
console.log(title+" = "+polygon.toString()+" "+(polygon.isEmpty? (polygon.isEmpty() ?" EMPTY": "NONEMPTY"): "")+"\n\n"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Example of constructing and translating polygons. | ||
*/ | ||
var jsts = require("jsts"); | ||
var factory = new jsts.geom.GeometryFactory(); | ||
|
||
var show = require("./show"); // just for showing the geometric objects during the example. | ||
|
||
|
||
// some shorthands: | ||
|
||
function coord(x,y) { return new jsts.geom.Coordinate(x,y); } | ||
function linstr(coords) { return factory.createLineString(coords); } | ||
function linring(coords) { return factory.createLinearRing(coords); } | ||
function polygon(coords,holes) { return factory.createPolygon(linring(coords),holes); } | ||
|
||
var a = polygon([coord(0, 0), coord(0, 10), coord(10, 10), coord(10, 0), coord(0, 0)]); | ||
var b = polygon([coord(5, 5), coord(10, 10), coord(5, 15), coord(0, 10), coord(5, 5)]); | ||
var c = linstr([coord(3,0), coord(3,10)]); | ||
show("a",a); | ||
show("b",b); | ||
show("c",c); | ||
|
||
show("a.centroid", a.getCentroid()); //(5,5) | ||
show("b.centroid", b.getCentroid()); //(5,10) | ||
show("c.centroid", c.getCentroid()); //(3,5) | ||
|
||
show("a.dimension", a.getDimension()); // 2 | ||
show("b.dimension", b.getDimension()); // 2 | ||
show("c.dimension", c.getDimension()); // 1 | ||
|
||
show("a.type", a.getGeometryType()); // Geometry | ||
show("b.type", b.getGeometryType()); // Geometry | ||
show("c.type", c.getGeometryType()); // LineString | ||
|
||
show("a.length", a.getLength()); // 40 | ||
show("b.length", b.getLength()); // 20*sqrt(2) | ||
show("c.length", c.getLength()); // 10 | ||
|
||
show("a.area", a.getArea()); // 100 (10x10) | ||
show("b.area", b.getArea()); // 50 (100/2) | ||
//show("c.area", c.getArea()); // error: a linestring doesn't have area | ||
|
||
show("a.envelope", a.getEnvelope()); // == a | ||
show("b.envelope", b.getEnvelope()); // = the square [0,10]x[5,15] | ||
show("c.envelope", c.getEnvelope()); // == c | ||
|
||
show("a.envelope.area", a.getEnvelope().getArea()); // 100 | ||
show("b.envelope.area", b.getEnvelope().getArea()); // 100 |