1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420 |
- (function (root, factory) {
- // Node.
- if(typeof module === 'object' && typeof module.exports === 'object') {
- exports = module.exports = factory();
- }
- // Browser Global.
- if(typeof window === "object") {
- root.Terraformer = factory();
- }
- }(this, function(){
- "use strict";
- var exports = {},
- EarthRadius = 6378137,
- DegreesPerRadian = 57.295779513082320,
- RadiansPerDegree = 0.017453292519943,
- MercatorCRS = {
- "type": "link",
- "properties": {
- "href": "http://spatialreference.org/ref/sr-org/6928/ogcwkt/",
- "type": "ogcwkt"
- }
- },
- GeographicCRS = {
- "type": "link",
- "properties": {
- "href": "http://spatialreference.org/ref/epsg/4326/ogcwkt/",
- "type": "ogcwkt"
- }
- };
- /*
- Internal: isArray function
- */
- function isArray(obj) {
- return Object.prototype.toString.call(obj) === "[object Array]";
- }
- /*
- Internal: safe warning
- */
- function warn() {
- var args = Array.prototype.slice.apply(arguments);
- if (typeof console !== undefined && console.warn) {
- console.warn.apply(console, args);
- }
- }
- /*
- Internal: Extend one object with another.
- */
- function extend(destination, source) {
- for (var k in source) {
- if (source.hasOwnProperty(k)) {
- destination[k] = source[k];
- }
- }
- return destination;
- }
- /*
- Public: Calculate an bounding box for a geojson object
- */
- function calculateBounds (geojson) {
- if(geojson.type){
- switch (geojson.type) {
- case 'Point':
- return [ geojson.coordinates[0], geojson.coordinates[1], geojson.coordinates[0], geojson.coordinates[1]];
- case 'MultiPoint':
- return calculateBoundsFromArray(geojson.coordinates);
- case 'LineString':
- return calculateBoundsFromArray(geojson.coordinates);
- case 'MultiLineString':
- return calculateBoundsFromNestedArrays(geojson.coordinates);
- case 'Polygon':
- return calculateBoundsFromNestedArrays(geojson.coordinates);
- case 'MultiPolygon':
- return calculateBoundsFromNestedArrayOfArrays(geojson.coordinates);
- case 'Feature':
- return geojson.geometry? calculateBounds(geojson.geometry) : null;
- case 'FeatureCollection':
- return calculateBoundsForFeatureCollection(geojson);
- case 'GeometryCollection':
- return calculateBoundsForGeometryCollection(geojson);
- default:
- throw new Error("Unknown type: " + geojson.type);
- }
- }
- return null;
- }
- /*
- Internal: Calculate an bounding box from an nested array of positions
- [
- [
- [ [lng, lat],[lng, lat],[lng, lat] ]
- ]
- [
- [lng, lat],[lng, lat],[lng, lat]
- ]
- [
- [lng, lat],[lng, lat],[lng, lat]
- ]
- ]
- */
- function calculateBoundsFromNestedArrays (array) {
- var x1 = null, x2 = null, y1 = null, y2 = null;
- for (var i = 0; i < array.length; i++) {
- var inner = array[i];
- for (var j = 0; j < inner.length; j++) {
- var lonlat = inner[j];
- var lon = lonlat[0];
- var lat = lonlat[1];
- if (x1 === null) {
- x1 = lon;
- } else if (lon < x1) {
- x1 = lon;
- }
- if (x2 === null) {
- x2 = lon;
- } else if (lon > x2) {
- x2 = lon;
- }
- if (y1 === null) {
- y1 = lat;
- } else if (lat < y1) {
- y1 = lat;
- }
- if (y2 === null) {
- y2 = lat;
- } else if (lat > y2) {
- y2 = lat;
- }
- }
- }
- return [x1, y1, x2, y2 ];
- }
- /*
- Internal: Calculate a bounding box from an array of arrays of arrays
- [
- [ [lng, lat],[lng, lat],[lng, lat] ]
- [ [lng, lat],[lng, lat],[lng, lat] ]
- [ [lng, lat],[lng, lat],[lng, lat] ]
- ]
- */
- function calculateBoundsFromNestedArrayOfArrays (array) {
- var x1 = null, x2 = null, y1 = null, y2 = null;
- for (var i = 0; i < array.length; i++) {
- var inner = array[i];
- for (var j = 0; j < inner.length; j++) {
- var innerinner = inner[j];
- for (var k = 0; k < innerinner.length; k++) {
- var lonlat = innerinner[k];
- var lon = lonlat[0];
- var lat = lonlat[1];
- if (x1 === null) {
- x1 = lon;
- } else if (lon < x1) {
- x1 = lon;
- }
- if (x2 === null) {
- x2 = lon;
- } else if (lon > x2) {
- x2 = lon;
- }
- if (y1 === null) {
- y1 = lat;
- } else if (lat < y1) {
- y1 = lat;
- }
- if (y2 === null) {
- y2 = lat;
- } else if (lat > y2) {
- y2 = lat;
- }
- }
- }
- }
- return [x1, y1, x2, y2];
- }
- /*
- Internal: Calculate a bounding box from an array of positions
- [
- [lng, lat],[lng, lat],[lng, lat]
- ]
- */
- function calculateBoundsFromArray (array) {
- var x1 = null, x2 = null, y1 = null, y2 = null;
- for (var i = 0; i < array.length; i++) {
- var lonlat = array[i];
- var lon = lonlat[0];
- var lat = lonlat[1];
- if (x1 === null) {
- x1 = lon;
- } else if (lon < x1) {
- x1 = lon;
- }
- if (x2 === null) {
- x2 = lon;
- } else if (lon > x2) {
- x2 = lon;
- }
- if (y1 === null) {
- y1 = lat;
- } else if (lat < y1) {
- y1 = lat;
- }
- if (y2 === null) {
- y2 = lat;
- } else if (lat > y2) {
- y2 = lat;
- }
- }
- return [x1, y1, x2, y2 ];
- }
- /*
- Internal: Calculate an bounding box for a feature collection
- */
- function calculateBoundsForFeatureCollection(featureCollection){
- var extents = [], extent;
- for (var i = featureCollection.features.length - 1; i >= 0; i--) {
- extent = calculateBounds(featureCollection.features[i].geometry);
- extents.push([extent[0],extent[1]]);
- extents.push([extent[2],extent[3]]);
- }
- return calculateBoundsFromArray(extents);
- }
- /*
- Internal: Calculate an bounding box for a geometry collection
- */
- function calculateBoundsForGeometryCollection(geometryCollection){
- var extents = [], extent;
- for (var i = geometryCollection.geometries.length - 1; i >= 0; i--) {
- extent = calculateBounds(geometryCollection.geometries[i]);
- extents.push([extent[0],extent[1]]);
- extents.push([extent[2],extent[3]]);
- }
- return calculateBoundsFromArray(extents);
- }
- function calculateEnvelope(geojson){
- var bounds = calculateBounds(geojson);
- return {
- x: bounds[0],
- y: bounds[1],
- w: Math.abs(bounds[0] - bounds[2]),
- h: Math.abs(bounds[1] - bounds[3])
- };
- }
- /*
- Internal: Convert radians to degrees. Used by spatial reference converters.
- */
- function radToDeg(rad) {
- return rad * DegreesPerRadian;
- }
- /*
- Internal: Convert degrees to radians. Used by spatial reference converters.
- */
- function degToRad(deg) {
- return deg * RadiansPerDegree;
- }
- /*
- Internal: Loop over each array in a geojson object and apply a function to it. Used by spatial reference converters.
- */
- function eachPosition(coordinates, func) {
- for (var i = 0; i < coordinates.length; i++) {
- // we found a number so lets convert this pair
- if(typeof coordinates[i][0] === "number"){
- coordinates[i] = func(coordinates[i]);
- }
- // we found an coordinates array it again and run THIS function against it
- if(typeof coordinates[i] === "object"){
- coordinates[i] = eachPosition(coordinates[i], func);
- }
- }
- return coordinates;
- }
- /*
- Public: Convert a GeoJSON Position object to Geographic (4326)
- */
- function positionToGeographic(position) {
- var x = position[0];
- var y = position[1];
- return [radToDeg(x / EarthRadius) - (Math.floor((radToDeg(x / EarthRadius) + 180) / 360) * 360), radToDeg((Math.PI / 2) - (2 * Math.atan(Math.exp(-1.0 * y / EarthRadius))))];
- }
- /*
- Public: Convert a GeoJSON Position object to Web Mercator (102100)
- */
- function positionToMercator(position) {
- var lng = position[0];
- var lat = Math.max(Math.min(position[1], 89.99999), -89.99999);
- return [degToRad(lng) * EarthRadius, EarthRadius/2.0 * Math.log( (1.0 + Math.sin(degToRad(lat))) / (1.0 - Math.sin(degToRad(lat))) )];
- }
- /*
- Public: Apply a function agaist all positions in a geojson object. Used by spatial reference converters.
- */
- function applyConverter(geojson, converter, noCrs){
- if(geojson.type === "Point") {
- geojson.coordinates = converter(geojson.coordinates);
- } else if(geojson.type === "Feature") {
- geojson.geometry = applyConverter(geojson.geometry, converter, true);
- } else if(geojson.type === "FeatureCollection") {
- for (var f = 0; f < geojson.features.length; f++) {
- geojson.features[f] = applyConverter(geojson.features[f], converter, true);
- }
- } else if(geojson.type === "GeometryCollection") {
- for (var g = 0; g < geojson.geometries.length; g++) {
- geojson.geometries[g] = applyConverter(geojson.geometries[g], converter, true);
- }
- } else {
- geojson.coordinates = eachPosition(geojson.coordinates, converter);
- }
- if(!noCrs){
- if(converter === positionToMercator){
- geojson.crs = MercatorCRS;
- }
- }
- if(converter === positionToGeographic){
- delete geojson.crs;
- }
- return geojson;
- }
- /*
- Public: Convert a GeoJSON object to ESRI Web Mercator (102100)
- */
- function toMercator(geojson) {
- return applyConverter(geojson, positionToMercator);
- }
- /*
- Convert a GeoJSON object to Geographic coordinates (WSG84, 4326)
- */
- function toGeographic(geojson) {
- return applyConverter(geojson, positionToGeographic);
- }
- /*
- Internal: -1,0,1 comparison function
- */
- function cmp(a, b) {
- if(a < b) {
- return -1;
- } else if(a > b) {
- return 1;
- } else {
- return 0;
- }
- }
- /*
- Internal: used for sorting
- */
- function compSort(p1, p2) {
- if (p1[0] > p2[0]) {
- return -1;
- } else if (p1[0] < p2[0]) {
- return 1;
- } else if (p1[1] > p2[1]) {
- return -1;
- } else if (p1[1] < p2[1]) {
- return 1;
- } else {
- return 0;
- }
- }
- /*
- Internal: used to determine turn
- */
- function turn(p, q, r) {
- // Returns -1, 0, 1 if p,q,r forms a right, straight, or left turn.
- return cmp((q[0] - p[0]) * (r[1] - p[1]) - (r[0] - p[0]) * (q[1] - p[1]), 0);
- }
- /*
- Internal: used to determine euclidean distance between two points
- */
- function euclideanDistance(p, q) {
- // Returns the squared Euclidean distance between p and q.
- var dx = q[0] - p[0];
- var dy = q[1] - p[1];
- return dx * dx + dy * dy;
- }
- function nextHullPoint(points, p) {
- // Returns the next point on the convex hull in CCW from p.
- var q = p;
- for(var r in points) {
- var t = turn(p, q, points[r]);
- if(t === -1 || t === 0 && euclideanDistance(p, points[r]) > euclideanDistance(p, q)) {
- q = points[r];
- }
- }
- return q;
- }
- function convexHull(points) {
- // implementation of the Jarvis March algorithm
- // adapted from http://tixxit.wordpress.com/2009/12/09/jarvis-march/
- if(points.length === 0) {
- return [];
- } else if(points.length === 1) {
- return points;
- }
- // Returns the points on the convex hull of points in CCW order.
- var hull = [points.sort(compSort)[0]];
- for(var p = 0; p < hull.length; p++) {
- var q = nextHullPoint(points, hull[p]);
- if(q !== hull[0]) {
- hull.push(q);
- }
- }
- return hull;
- }
- function isConvex(points) {
- var ltz;
- for (var i = 0; i < points.length - 3; i++) {
- var p1 = points[i];
- var p2 = points[i + 1];
- var p3 = points[i + 2];
- var v = [p2[0] - p1[0], p2[1] - p1[1]];
- // p3.x * v.y - p3.y * v.x + v.x * p1.y - v.y * p1.x
- var res = p3[0] * v[1] - p3[1] * v[0] + v[0] * p1[1] - v[1] * p1[0];
- if (i === 0) {
- if (res < 0) {
- ltz = true;
- } else {
- ltz = false;
- }
- } else {
- if (ltz && (res > 0) || !ltz && (res < 0)) {
- return false;
- }
- }
- }
- return true;
- }
- function coordinatesContainPoint(coordinates, point) {
- var contains = false;
- for(var i = -1, l = coordinates.length, j = l - 1; ++i < l; j = i) {
- if (((coordinates[i][1] <= point[1] && point[1] < coordinates[j][1]) ||
- (coordinates[j][1] <= point[1] && point[1] < coordinates[i][1])) &&
- (point[0] < (coordinates[j][0] - coordinates[i][0]) * (point[1] - coordinates[i][1]) / (coordinates[j][1] - coordinates[i][1]) + coordinates[i][0])) {
- contains = !contains;
- }
- }
- return contains;
- }
- function polygonContainsPoint(polygon, point) {
- if (polygon && polygon.length) {
- if (polygon.length === 1) { // polygon with no holes
- return coordinatesContainPoint(polygon[0], point);
- } else { // polygon with holes
- if (coordinatesContainPoint(polygon[0], point)) {
- for (var i = 1; i < polygon.length; i++) {
- if (coordinatesContainPoint(polygon[i], point)) {
- return false; // found in hole
- }
- }
- return true;
- } else {
- return false;
- }
- }
- } else {
- return false;
- }
- }
- function edgeIntersectsEdge(a1, a2, b1, b2) {
- var ua_t = (b2[0] - b1[0]) * (a1[1] - b1[1]) - (b2[1] - b1[1]) * (a1[0] - b1[0]);
- var ub_t = (a2[0] - a1[0]) * (a1[1] - b1[1]) - (a2[1] - a1[1]) * (a1[0] - b1[0]);
- var u_b = (b2[1] - b1[1]) * (a2[0] - a1[0]) - (b2[0] - b1[0]) * (a2[1] - a1[1]);
- if ( u_b !== 0 ) {
- var ua = ua_t / u_b;
- var ub = ub_t / u_b;
- if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
- return true;
- }
- }
- return false;
- }
- function isNumber(n) {
- return !isNaN(parseFloat(n)) && isFinite(n);
- }
- function arraysIntersectArrays(a, b) {
- if (isNumber(a[0][0])) {
- if (isNumber(b[0][0])) {
- for (var i = 0; i < a.length - 1; i++) {
- for (var j = 0; j < b.length - 1; j++) {
- if (edgeIntersectsEdge(a[i], a[i + 1], b[j], b[j + 1])) {
- return true;
- }
- }
- }
- } else {
- for (var k = 0; k < b.length; k++) {
- if (arraysIntersectArrays(a, b[k])) {
- return true;
- }
- }
- }
- } else {
- for (var l = 0; l < a.length; l++) {
- if (arraysIntersectArrays(a[l], b)) {
- return true;
- }
- }
- }
- return false;
- }
- /*
- Internal: Returns a copy of coordinates for s closed polygon
- */
- function closedPolygon(coordinates) {
- var outer = [ ];
- for (var i = 0; i < coordinates.length; i++) {
- var inner = coordinates[i].slice();
- if (pointsEqual(inner[0], inner[inner.length - 1]) === false) {
- inner.push(inner[0]);
- }
- outer.push(inner);
- }
- return outer;
- }
- function pointsEqual(a, b) {
- for (var i = 0; i < a.length; i++) {
- if (a[i] !== b[i]) {
- return false;
- }
- }
- return true;
- }
- function coordinatesEqual(a, b) {
- if (a.length !== b.length) {
- return false;
- }
- var na = a.slice().sort(compSort);
- var nb = b.slice().sort(compSort);
- for (var i = 0; i < na.length; i++) {
- if (na[i].length !== nb[i].length) {
- return false;
- }
- for (var j = 0; j < na.length; j++) {
- if (na[i][j] !== nb[i][j]) {
- return false;
- }
- }
- }
- return true;
- }
- /*
- Internal: An array of variables that will be excluded form JSON objects.
- */
- var excludeFromJSON = ["length"];
- /*
- Internal: Base GeoJSON Primitive
- */
- function Primitive(geojson){
- if(geojson){
- switch (geojson.type) {
- case 'Point':
- return new Point(geojson);
- case 'MultiPoint':
- return new MultiPoint(geojson);
- case 'LineString':
- return new LineString(geojson);
- case 'MultiLineString':
- return new MultiLineString(geojson);
- case 'Polygon':
- return new Polygon(geojson);
- case 'MultiPolygon':
- return new MultiPolygon(geojson);
- case 'Feature':
- return new Feature(geojson);
- case 'FeatureCollection':
- return new FeatureCollection(geojson);
- case 'GeometryCollection':
- return new GeometryCollection(geojson);
- default:
- throw new Error("Unknown type: " + geojson.type);
- }
- }
- }
- Primitive.prototype.toMercator = function(){
- return toMercator(this);
- };
- Primitive.prototype.toGeographic = function(){
- return toGeographic(this);
- };
- Primitive.prototype.envelope = function(){
- return calculateEnvelope(this);
- };
- Primitive.prototype.bbox = function(){
- return calculateBounds(this);
- };
- Primitive.prototype.convexHull = function(){
- var coordinates = [ ], i, j;
- if (this.type === 'Point') {
- return null;
- } else if (this.type === 'LineString' || this.type === 'MultiPoint') {
- if (this.coordinates && this.coordinates.length >= 3) {
- coordinates = this.coordinates;
- } else {
- return null;
- }
- } else if (this.type === 'Polygon' || this.type === 'MultiLineString') {
- if (this.coordinates && this.coordinates.length > 0) {
- for (i = 0; i < this.coordinates.length; i++) {
- coordinates = coordinates.concat(this.coordinates[i]);
- }
- if(coordinates.length < 3){
- return null;
- }
- } else {
- return null;
- }
- } else if (this.type === 'MultiPolygon') {
- if (this.coordinates && this.coordinates.length > 0) {
- for (i = 0; i < this.coordinates.length; i++) {
- for (j = 0; j < this.coordinates[i].length; j++) {
- coordinates = coordinates.concat(this.coordinates[i][j]);
- }
- }
- if(coordinates.length < 3){
- return null;
- }
- } else {
- return null;
- }
- } else if(this.type === "Feature"){
- var primitive = new Primitive(this.geometry);
- return primitive.convexHull();
- }
- return new Polygon({
- type: 'Polygon',
- coordinates: closedPolygon([convexHull(coordinates)])
- });
- };
- Primitive.prototype.toJSON = function(){
- var obj = {};
- for (var key in this) {
- if (this.hasOwnProperty(key) && excludeFromJSON.indexOf(key) === -1) {
- obj[key] = this[key];
- }
- }
- obj.bbox = calculateBounds(this);
- return obj;
- };
- Primitive.prototype.contains = function(primitive){
- return new Primitive(primitive).within(this);
- };
- Primitive.prototype.within = function(primitive) {
- var coordinates, i, j, contains;
- // if we are passed a feature, use the polygon inside instead
- if (primitive.type === 'Feature') {
- primitive = primitive.geometry;
- }
- // point.within(point) :: equality
- if (primitive.type === "Point") {
- if (this.type === "Point") {
- return pointsEqual(this.coordinates, primitive.coordinates);
- }
- }
- // point.within(multilinestring)
- if (primitive.type === "MultiLineString") {
- if (this.type === "Point") {
- for (i = 0; i < primitive.coordinates.length; i++) {
- var linestring = { type: "LineString", coordinates: primitive.coordinates[i] };
- if (this.within(linestring)) {
- return true;
- }
- }
- }
- }
- // point.within(linestring), point.within(multipoint)
- if (primitive.type === "LineString" || primitive.type === "MultiPoint") {
- if (this.type === "Point") {
- for (i = 0; i < primitive.coordinates.length; i++) {
- if (this.coordinates.length !== primitive.coordinates[i].length) {
- return false;
- }
- if (pointsEqual(this.coordinates, primitive.coordinates[i])) {
- return true;
- }
- }
- }
- }
- if (primitive.type === "Polygon") {
- // polygon.within(polygon)
- if (this.type === "Polygon") {
- // check for equal polygons
- if (primitive.coordinates.length === this.coordinates.length) {
- for (i = 0; i < this.coordinates.length; i++) {
- if (coordinatesEqual(this.coordinates[i], primitive.coordinates[i])) {
- return true;
- }
- }
- }
- if (this.coordinates.length && polygonContainsPoint(primitive.coordinates, this.coordinates[0][0])) {
- return !arraysIntersectArrays(closedPolygon(this.coordinates), closedPolygon(primitive.coordinates));
- } else {
- return false;
- }
- // point.within(polygon)
- } else if (this.type === "Point") {
- return polygonContainsPoint(primitive.coordinates, this.coordinates);
- // linestring/multipoint withing polygon
- } else if (this.type === "LineString" || this.type === "MultiPoint") {
- if (!this.coordinates || this.coordinates.length === 0) {
- return false;
- }
- for (i = 0; i < this.coordinates.length; i++) {
- if (polygonContainsPoint(primitive.coordinates, this.coordinates[i]) === false) {
- return false;
- }
- }
- return true;
- // multilinestring.within(polygon)
- } else if (this.type === "MultiLineString") {
- for (i = 0; i < this.coordinates.length; i++) {
- var ls = new LineString(this.coordinates[i]);
- if (ls.within(primitive) === false) {
- contains++;
- return false;
- }
- }
- return true;
- // multipolygon.within(polygon)
- } else if (this.type === "MultiPolygon") {
- for (i = 0; i < this.coordinates.length; i++) {
- var p1 = new Primitive({ type: "Polygon", coordinates: this.coordinates[i] });
- if (p1.within(primitive) === false) {
- return false;
- }
- }
- return true;
- }
- }
- if (primitive.type === "MultiPolygon") {
- // point.within(multipolygon)
- if (this.type === "Point") {
- if (primitive.coordinates.length) {
- for (i = 0; i < primitive.coordinates.length; i++) {
- coordinates = primitive.coordinates[i];
- if (polygonContainsPoint(coordinates, this.coordinates) && arraysIntersectArrays([this.coordinates], primitive.coordinates) === false) {
- return true;
- }
- }
- }
- return false;
- // polygon.within(multipolygon)
- } else if (this.type === "Polygon") {
- for (i = 0; i < this.coordinates.length; i++) {
- if (primitive.coordinates[i].length === this.coordinates.length) {
- for (j = 0; j < this.coordinates.length; j++) {
- if (coordinatesEqual(this.coordinates[j], primitive.coordinates[i][j])) {
- return true;
- }
- }
- }
- }
- if (arraysIntersectArrays(this.coordinates, primitive.coordinates) === false) {
- if (primitive.coordinates.length) {
- for (i = 0; i < primitive.coordinates.length; i++) {
- coordinates = primitive.coordinates[i];
- if (polygonContainsPoint(coordinates, this.coordinates[0][0]) === false) {
- contains = false;
- } else {
- contains = true;
- }
- }
- return contains;
- }
- }
- // linestring.within(multipolygon), multipoint.within(multipolygon)
- } else if (this.type === "LineString" || this.type === "MultiPoint") {
- for (i = 0; i < primitive.coordinates.length; i++) {
- var p = { type: "Polygon", coordinates: primitive.coordinates[i] };
- if (this.within(p)) {
- return true;
- }
- return false;
- }
- // multilinestring.within(multipolygon)
- } else if (this.type === "MultiLineString") {
- for (i = 0; i < this.coordinates.length; i++) {
- var lines = new LineString(this.coordinates[i]);
- if (lines.within(primitive) === false) {
- return false;
- }
- }
- return true;
- // multipolygon.within(multipolygon)
- } else if (this.type === "MultiPolygon") {
- for (i = 0; i < primitive.coordinates.length; i++) {
- var mpoly = { type: "Polygon", coordinates: primitive.coordinates[i] };
- if (this.within(mpoly) === false) {
- return false;
- }
- }
- return true;
- }
- }
- // default to false
- return false;
- };
- Primitive.prototype.intersects = function(primitive) {
- // if we are passed a feature, use the polygon inside instead
- if (primitive.type === 'Feature') {
- primitive = primitive.geometry;
- }
- var p = new Primitive(primitive);
- if (this.within(primitive) || p.within(this)) {
- return true;
- }
- if (this.type !== 'Point' && this.type !== 'MultiPoint' &&
- primitive.type !== 'Point' && primitive.type !== 'MultiPoint') {
- return arraysIntersectArrays(this.coordinates, primitive.coordinates);
- } else if (this.type === 'Feature') {
- // in the case of a Feature, use the internal primitive for intersection
- var inner = new Primitive(this.geometry);
- return inner.intersects(primitive);
- }
- warn("Type " + this.type + " to " + primitive.type + " intersection is not supported by intersects");
- return false;
- };
- /*
- GeoJSON Point Class
- new Point();
- new Point(x,y,z,wtf);
- new Point([x,y,z,wtf]);
- new Point([x,y]);
- new Point({
- type: "Point",
- coordinates: [x,y]
- });
- */
- function Point(input){
- var args = Array.prototype.slice.call(arguments);
- if(input && input.type === "Point" && input.coordinates){
- extend(this, input);
- } else if(input && isArray(input)) {
- this.coordinates = input;
- } else if(args.length >= 2) {
- this.coordinates = args;
- } else {
- throw "Terraformer: invalid input for Terraformer.Point";
- }
- this.type = "Point";
- }
- Point.prototype = new Primitive();
- Point.prototype.constructor = Point;
- /*
- GeoJSON MultiPoint Class
- new MultiPoint();
- new MultiPoint([[x,y], [x1,y1]]);
- new MultiPoint({
- type: "MultiPoint",
- coordinates: [x,y]
- });
- */
- function MultiPoint(input){
- if(input && input.type === "MultiPoint" && input.coordinates){
- extend(this, input);
- } else if(isArray(input)) {
- this.coordinates = input;
- } else {
- throw "Terraformer: invalid input for Terraformer.MultiPoint";
- }
- this.type = "MultiPoint";
- }
- MultiPoint.prototype = new Primitive();
- MultiPoint.prototype.constructor = MultiPoint;
- MultiPoint.prototype.forEach = function(func){
- for (var i = 0; i < this.coordinates.length; i++) {
- func.apply(this, [this.coordinates[i], i, this.coordinates]);
- }
- return this;
- };
- MultiPoint.prototype.addPoint = function(point){
- this.coordinates.push(point);
- return this;
- };
- MultiPoint.prototype.insertPoint = function(point, index){
- this.coordinates.splice(index, 0, point);
- return this;
- };
- MultiPoint.prototype.removePoint = function(remove){
- if(typeof remove === "number"){
- this.coordinates.splice(remove, 1);
- } else {
- this.coordinates.splice(this.coordinates.indexOf(remove), 1);
- }
- return this;
- };
- MultiPoint.prototype.get = function(i){
- return new Point(this.coordinates[i]);
- };
- /*
- GeoJSON LineString Class
- new LineString();
- new LineString([[x,y], [x1,y1]]);
- new LineString({
- type: "LineString",
- coordinates: [x,y]
- });
- */
- function LineString(input){
- if(input && input.type === "LineString" && input.coordinates){
- extend(this, input);
- } else if(isArray(input)) {
- this.coordinates = input;
- } else {
- throw "Terraformer: invalid input for Terraformer.LineString";
- }
- this.type = "LineString";
- }
- LineString.prototype = new Primitive();
- LineString.prototype.constructor = LineString;
- LineString.prototype.addVertex = function(point){
- this.coordinates.push(point);
- return this;
- };
- LineString.prototype.insertVertex = function(point, index){
- this.coordinates.splice(index, 0, point);
- return this;
- };
- LineString.prototype.removeVertex = function(remove){
- this.coordinates.splice(remove, 1);
- return this;
- };
- /*
- GeoJSON MultiLineString Class
- new MultiLineString();
- new MultiLineString([ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ]);
- new MultiLineString({
- type: "MultiLineString",
- coordinates: [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ]
- });
- */
- function MultiLineString(input){
- if(input && input.type === "MultiLineString" && input.coordinates){
- extend(this, input);
- } else if(isArray(input)) {
- this.coordinates = input;
- } else {
- throw "Terraformer: invalid input for Terraformer.MultiLineString";
- }
- this.type = "MultiLineString";
- }
- MultiLineString.prototype = new Primitive();
- MultiLineString.prototype.constructor = MultiLineString;
- MultiLineString.prototype.forEach = function(func){
- for (var i = 0; i < this.coordinates.length; i++) {
- func.apply(this, [this.coordinates[i], i, this.coordinates ]);
- }
- };
- MultiLineString.prototype.get = function(i){
- return new LineString(this.coordinates[i]);
- };
- /*
- GeoJSON Polygon Class
- new Polygon();
- new Polygon([ [[x,y], [x1,y1], [x2,y2]] ]);
- new Polygon({
- type: "Polygon",
- coordinates: [ [[x,y], [x1,y1], [x2,y2]] ]
- });
- */
- function Polygon(input){
- if(input && input.type === "Polygon" && input.coordinates){
- extend(this, input);
- } else if(isArray(input)) {
- this.coordinates = input;
- } else {
- throw "Terraformer: invalid input for Terraformer.Polygon";
- }
- this.type = "Polygon";
- }
- Polygon.prototype = new Primitive();
- Polygon.prototype.constructor = Polygon;
- Polygon.prototype.addVertex = function(point){
- this.insertVertex(point, this.coordinates[0].length - 1);
- return this;
- };
- Polygon.prototype.insertVertex = function(point, index){
- this.coordinates[0].splice(index, 0, point);
- return this;
- };
- Polygon.prototype.removeVertex = function(remove){
- this.coordinates[0].splice(remove, 1);
- return this;
- };
- Polygon.prototype.close = function() {
- this.coordinates = closedPolygon(this.coordinates);
- };
- Polygon.prototype.hasHoles = function() {
- return this.coordinates.length > 1;
- };
- Polygon.prototype.holes = function() {
- var holes = [];
- if (this.hasHoles()) {
- for (var i = 1; i < this.coordinates.length; i++) {
- holes.push(new Polygon([this.coordinates[i]]));
- }
- }
- return holes;
- };
- /*
- GeoJSON MultiPolygon Class
- new MultiPolygon();
- new MultiPolygon([ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]);
- new MultiPolygon({
- type: "MultiPolygon",
- coordinates: [ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]
- });
- */
- function MultiPolygon(input){
- if(input && input.type === "MultiPolygon" && input.coordinates){
- extend(this, input);
- } else if(isArray(input)) {
- this.coordinates = input;
- } else {
- throw "Terraformer: invalid input for Terraformer.MultiPolygon";
- }
- this.type = "MultiPolygon";
- }
- MultiPolygon.prototype = new Primitive();
- MultiPolygon.prototype.constructor = MultiPolygon;
- MultiPolygon.prototype.forEach = function(func){
- for (var i = 0; i < this.coordinates.length; i++) {
- func.apply(this, [this.coordinates[i], i, this.coordinates ]);
- }
- };
- MultiPolygon.prototype.get = function(i){
- return new Polygon(this.coordinates[i]);
- };
- MultiPolygon.prototype.close = function(){
- var outer = [];
- this.forEach(function(polygon){
- outer.push(closedPolygon(polygon));
- });
- this.coordinates = outer;
- return this;
- };
- /*
- GeoJSON Feature Class
- new Feature();
- new Feature({
- type: "Feature",
- geometry: {
- type: "Polygon",
- coordinates: [ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]
- }
- });
- new Feature({
- type: "Polygon",
- coordinates: [ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]
- });
- */
- function Feature(input){
- if(input && input.type === "Feature"){
- extend(this, input);
- } else if(input && input.type && input.coordinates) {
- this.geometry = input;
- } else {
- throw "Terraformer: invalid input for Terraformer.Feature";
- }
- this.type = "Feature";
- }
- Feature.prototype = new Primitive();
- Feature.prototype.constructor = Feature;
- /*
- GeoJSON FeatureCollection Class
- new FeatureCollection();
- new FeatureCollection([feature, feature1]);
- new FeatureCollection({
- type: "FeatureCollection",
- coordinates: [feature, feature1]
- });
- */
- function FeatureCollection(input){
- if(input && input.type === "FeatureCollection" && input.features){
- extend(this, input);
- } else if(isArray(input)) {
- this.features = input;
- } else {
- throw "Terraformer: invalid input for Terraformer.FeatureCollection";
- }
- this.type = "FeatureCollection";
- }
- FeatureCollection.prototype = new Primitive();
- FeatureCollection.prototype.constructor = FeatureCollection;
- FeatureCollection.prototype.forEach = function(func){
- for (var i = 0; i < this.features.length; i++) {
- func.apply(this, [this.features[i], i, this.features]);
- }
- };
- FeatureCollection.prototype.get = function(id){
- var found;
- this.forEach(function(feature){
- if(feature.id === id){
- found = feature;
- }
- });
- return new Feature(found);
- };
- /*
- GeoJSON GeometryCollection Class
- new GeometryCollection();
- new GeometryCollection([geometry, geometry1]);
- new GeometryCollection({
- type: "GeometryCollection",
- coordinates: [geometry, geometry1]
- });
- */
- function GeometryCollection(input){
- if(input && input.type === "GeometryCollection" && input.geometries){
- extend(this, input);
- } else if(isArray(input)) {
- this.geometries = input;
- } else if(input.coordinates && input.type){
- this.type = "GeometryCollection";
- this.geometries = [input];
- } else {
- throw "Terraformer: invalid input for Terraformer.GeometryCollection";
- }
- this.type = "GeometryCollection";
- }
- GeometryCollection.prototype = new Primitive();
- GeometryCollection.prototype.constructor = GeometryCollection;
- GeometryCollection.prototype.forEach = function(func){
- for (var i = 0; i < this.geometries.length; i++) {
- func.apply(this, [this.geometries[i], i, this.geometries]);
- }
- };
- GeometryCollection.prototype.get = function(i){
- return new Primitive(this.geometries[i]);
- };
- function createCircle(center, radius, interpolate){
- var mercatorPosition = positionToMercator(center);
- var steps = interpolate || 64;
- var polygon = {
- type: "Polygon",
- coordinates: [[]]
- };
- for(var i=1; i<=steps; i++) {
- var radians = i * (360/steps) * Math.PI / 180;
- polygon.coordinates[0].push([mercatorPosition[0] + radius * Math.cos(radians), mercatorPosition[1] + radius * Math.sin(radians)]);
- }
- polygon.coordinates = closedPolygon(polygon.coordinates);
- return toGeographic(polygon);
- }
- function Circle (center, radius, interpolate) {
- var steps = interpolate || 64;
- var rad = radius || 250;
- if(!center || center.length < 2 || !rad || !steps) {
- throw new Error("Terraformer: missing parameter for Terraformer.Circle");
- }
- extend(this, new Feature({
- type: "Feature",
- geometry: createCircle(center, rad, steps),
- properties: {
- radius: rad,
- center: center,
- steps: steps
- }
- }));
- }
- Circle.prototype = new Primitive();
- Circle.prototype.constructor = Circle;
- Circle.prototype.recalculate = function(){
- this.geometry = createCircle(this.properties.center, this.properties.radius, this.properties.steps);
- return this;
- };
- Circle.prototype.center = function(coordinates){
- if(coordinates){
- this.properties.center = coordinates;
- this.recalculate();
- }
- return this.properties.center;
- };
- Circle.prototype.radius = function(radius){
- if(radius){
- this.properties.radius = radius;
- this.recalculate();
- }
- return this.properties.radius;
- };
- Circle.prototype.steps = function(steps){
- if(steps){
- this.properties.steps = steps;
- this.recalculate();
- }
- return this.properties.steps;
- };
- Circle.prototype.toJSON = function() {
- var output = Primitive.prototype.toJSON.call(this);
- return output;
- };
- exports.Primitive = Primitive;
- exports.Point = Point;
- exports.MultiPoint = MultiPoint;
- exports.LineString = LineString;
- exports.MultiLineString = MultiLineString;
- exports.Polygon = Polygon;
- exports.MultiPolygon = MultiPolygon;
- exports.Feature = Feature;
- exports.FeatureCollection = FeatureCollection;
- exports.GeometryCollection = GeometryCollection;
- exports.Circle = Circle;
- exports.toMercator = toMercator;
- exports.toGeographic = toGeographic;
- exports.Tools = {};
- exports.Tools.positionToMercator = positionToMercator;
- exports.Tools.positionToGeographic = positionToGeographic;
- exports.Tools.applyConverter = applyConverter;
- exports.Tools.toMercator = toMercator;
- exports.Tools.toGeographic = toGeographic;
- exports.Tools.createCircle = createCircle;
- exports.Tools.calculateBounds = calculateBounds;
- exports.Tools.calculateEnvelope = calculateEnvelope;
- exports.Tools.coordinatesContainPoint = coordinatesContainPoint;
- exports.Tools.polygonContainsPoint = polygonContainsPoint;
- exports.Tools.arraysIntersectArrays = arraysIntersectArrays;
- exports.Tools.coordinatesContainPoint = coordinatesContainPoint;
- exports.Tools.coordinatesEqual = coordinatesEqual;
- exports.Tools.convexHull = convexHull;
- exports.Tools.isConvex = isConvex;
- exports.MercatorCRS = MercatorCRS;
- exports.GeographicCRS = GeographicCRS;
- return exports;
- }));
|