From 6573b308b692d48ea68a45e66148a14440dd19f3 Mon Sep 17 00:00:00 2001 From: raiyni Date: Tue, 13 Feb 2018 08:57:35 -0600 Subject: [PATCH] ol/geom/flat/orient export --- src/ol/format/EsriJSON.js | 4 ++-- src/ol/format/MVT.js | 4 ++-- src/ol/geom/MultiPolygon.js | 8 +++---- src/ol/geom/Polygon.js | 8 +++---- src/ol/geom/flat/orient.js | 30 ++++++++++++------------- src/ol/render/webgl/LineStringReplay.js | 4 ++-- src/ol/render/webgl/PolygonReplay.js | 8 +++---- test/spec/ol/geom/flat/orient.test.js | 15 +++++++------ 8 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/ol/format/EsriJSON.js b/src/ol/format/EsriJSON.js index f56d27b99d..c101ac31ad 100644 --- a/src/ol/format/EsriJSON.js +++ b/src/ol/format/EsriJSON.js @@ -17,7 +17,7 @@ import MultiPolygon from '../geom/MultiPolygon.js'; import Point from '../geom/Point.js'; import Polygon from '../geom/Polygon.js'; import {deflateCoordinates} from '../geom/flat/deflate.js'; -import _ol_geom_flat_orient_ from '../geom/flat/orient.js'; +import {linearRingIsClockwise} from '../geom/flat/orient.js'; import {assign, isEmpty} from '../obj.js'; import {get as getProjection} from '../proj.js'; @@ -134,7 +134,7 @@ function convertRings(rings, layout) { flatRing.length = 0; deflateCoordinates(flatRing, 0, rings[i], layout.length); // is this ring an outer ring? is it clockwise? - const clockwise = _ol_geom_flat_orient_.linearRingIsClockwise(flatRing, 0, + const clockwise = linearRingIsClockwise(flatRing, 0, flatRing.length, layout.length); if (clockwise) { outerRings.push([rings[i]]); diff --git a/src/ol/format/MVT.js b/src/ol/format/MVT.js index c4379e431e..fa54a2dcff 100644 --- a/src/ol/format/MVT.js +++ b/src/ol/format/MVT.js @@ -16,7 +16,7 @@ import MultiPoint from '../geom/MultiPoint.js'; import MultiPolygon from '../geom/MultiPolygon.js'; import Point from '../geom/Point.js'; import Polygon from '../geom/Polygon.js'; -import _ol_geom_flat_orient_ from '../geom/flat/orient.js'; +import {linearRingIsClockwise} from '../geom/flat/orient.js'; import Projection from '../proj/Projection.js'; import Units from '../proj/Units.js'; import RenderFeature from '../render/Feature.js'; @@ -309,7 +309,7 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) { let prevEndIndex = 0; for (let i = 0, ii = ends.length; i < ii; ++i) { const end = ends[i]; - if (!_ol_geom_flat_orient_.linearRingIsClockwise(flatCoordinates, offset, end, 2)) { + if (!linearRingIsClockwise(flatCoordinates, offset, end, 2)) { endss.push(ends.slice(prevEndIndex, i)); prevEndIndex = i; } diff --git a/src/ol/geom/MultiPolygon.js b/src/ol/geom/MultiPolygon.js index 9ee2af7e72..6c39b787cb 100644 --- a/src/ol/geom/MultiPolygon.js +++ b/src/ol/geom/MultiPolygon.js @@ -17,7 +17,7 @@ import {deflateMultiCoordinatesArray} from '../geom/flat/deflate.js'; import {inflateMultiCoordinatesArray} from '../geom/flat/inflate.js'; import {getInteriorPointsOfMultiArray} from '../geom/flat/interiorpoint.js'; import _ol_geom_flat_intersectsextent_ from '../geom/flat/intersectsextent.js'; -import _ol_geom_flat_orient_ from '../geom/flat/orient.js'; +import {linearRingsAreOriented, orientLinearRingsArray} from '../geom/flat/orient.js'; import {quantizeMultiArray} from '../geom/flat/simplify.js'; /** @@ -183,7 +183,7 @@ MultiPolygon.prototype.getCoordinates = function(opt_right) { let flatCoordinates; if (opt_right !== undefined) { flatCoordinates = this.getOrientedFlatCoordinates().slice(); - _ol_geom_flat_orient_.orientLinearRingss( + orientLinearRingsArray( flatCoordinates, 0, this.endss_, this.stride, opt_right); } else { flatCoordinates = this.flatCoordinates; @@ -238,13 +238,13 @@ MultiPolygon.prototype.getInteriorPoints = function() { MultiPolygon.prototype.getOrientedFlatCoordinates = function() { if (this.orientedRevision_ != this.getRevision()) { const flatCoordinates = this.flatCoordinates; - if (_ol_geom_flat_orient_.linearRingssAreOriented( + if (linearRingsAreOriented( flatCoordinates, 0, this.endss_, this.stride)) { this.orientedFlatCoordinates_ = flatCoordinates; } else { this.orientedFlatCoordinates_ = flatCoordinates.slice(); this.orientedFlatCoordinates_.length = - _ol_geom_flat_orient_.orientLinearRingss( + orientLinearRingsArray( this.orientedFlatCoordinates_, 0, this.endss_, this.stride); } this.orientedRevision_ = this.getRevision(); diff --git a/src/ol/geom/Polygon.js b/src/ol/geom/Polygon.js index c2ac483f4e..644d4eb8e8 100644 --- a/src/ol/geom/Polygon.js +++ b/src/ol/geom/Polygon.js @@ -17,7 +17,7 @@ import {deflateCoordinatesArray} from '../geom/flat/deflate.js'; import {inflateCoordinatesArray} from '../geom/flat/inflate.js'; import {getInteriorPointOfArray} from '../geom/flat/interiorpoint.js'; import _ol_geom_flat_intersectsextent_ from '../geom/flat/intersectsextent.js'; -import _ol_geom_flat_orient_ from '../geom/flat/orient.js'; +import {linearRingIsOriented, orientLinearRings} from '../geom/flat/orient.js'; import {quantizeArray} from '../geom/flat/simplify.js'; import {modulo} from '../math.js'; @@ -173,7 +173,7 @@ Polygon.prototype.getCoordinates = function(opt_right) { let flatCoordinates; if (opt_right !== undefined) { flatCoordinates = this.getOrientedFlatCoordinates().slice(); - _ol_geom_flat_orient_.orientLinearRings( + orientLinearRings( flatCoordinates, 0, this.ends_, this.stride, opt_right); } else { flatCoordinates = this.flatCoordinates; @@ -279,13 +279,13 @@ Polygon.prototype.getLinearRings = function() { Polygon.prototype.getOrientedFlatCoordinates = function() { if (this.orientedRevision_ != this.getRevision()) { const flatCoordinates = this.flatCoordinates; - if (_ol_geom_flat_orient_.linearRingsAreOriented( + if (linearRingIsOriented( flatCoordinates, 0, this.ends_, this.stride)) { this.orientedFlatCoordinates_ = flatCoordinates; } else { this.orientedFlatCoordinates_ = flatCoordinates.slice(); this.orientedFlatCoordinates_.length = - _ol_geom_flat_orient_.orientLinearRings( + orientLinearRings( this.orientedFlatCoordinates_, 0, this.ends_, this.stride); } this.orientedRevision_ = this.getRevision(); diff --git a/src/ol/geom/flat/orient.js b/src/ol/geom/flat/orient.js index ef0467880a..864e0a35c5 100644 --- a/src/ol/geom/flat/orient.js +++ b/src/ol/geom/flat/orient.js @@ -2,7 +2,6 @@ * @module ol/geom/flat/orient */ import {coordinates as reverseCoordinates} from '../flat/reverse.js'; -const _ol_geom_flat_orient_ = {}; /** @@ -12,7 +11,7 @@ const _ol_geom_flat_orient_ = {}; * @param {number} stride Stride. * @return {boolean} Is clockwise. */ -_ol_geom_flat_orient_.linearRingIsClockwise = function(flatCoordinates, offset, end, stride) { +export function linearRingIsClockwise(flatCoordinates, offset, end, stride) { // http://tinyurl.com/clockwise-method // https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp let edge = 0; @@ -26,7 +25,7 @@ _ol_geom_flat_orient_.linearRingIsClockwise = function(flatCoordinates, offset, y1 = y2; } return edge > 0; -}; +} /** @@ -42,11 +41,11 @@ _ol_geom_flat_orient_.linearRingIsClockwise = function(flatCoordinates, offset, * (counter-clockwise exterior ring and clockwise interior rings). * @return {boolean} Rings are correctly oriented. */ -_ol_geom_flat_orient_.linearRingsAreOriented = function(flatCoordinates, offset, ends, stride, opt_right) { +export function linearRingIsOriented(flatCoordinates, offset, ends, stride, opt_right) { const right = opt_right !== undefined ? opt_right : false; for (let i = 0, ii = ends.length; i < ii; ++i) { const end = ends[i]; - const isClockwise = _ol_geom_flat_orient_.linearRingIsClockwise( + const isClockwise = linearRingIsClockwise( flatCoordinates, offset, end, stride); if (i === 0) { if ((right && isClockwise) || (!right && !isClockwise)) { @@ -60,7 +59,7 @@ _ol_geom_flat_orient_.linearRingsAreOriented = function(flatCoordinates, offset, offset = end; } return true; -}; +} /** @@ -76,15 +75,15 @@ _ol_geom_flat_orient_.linearRingsAreOriented = function(flatCoordinates, offset, * (counter-clockwise exterior ring and clockwise interior rings). * @return {boolean} Rings are correctly oriented. */ -_ol_geom_flat_orient_.linearRingssAreOriented = function(flatCoordinates, offset, endss, stride, opt_right) { +export function linearRingsAreOriented(flatCoordinates, offset, endss, stride, opt_right) { for (let i = 0, ii = endss.length; i < ii; ++i) { - if (!_ol_geom_flat_orient_.linearRingsAreOriented( + if (!linearRingIsOriented( flatCoordinates, offset, endss[i], stride, opt_right)) { return false; } } return true; -}; +} /** @@ -100,11 +99,11 @@ _ol_geom_flat_orient_.linearRingssAreOriented = function(flatCoordinates, offset * @param {boolean=} opt_right Follow the right-hand rule for orientation. * @return {number} End. */ -_ol_geom_flat_orient_.orientLinearRings = function(flatCoordinates, offset, ends, stride, opt_right) { +export function orientLinearRings(flatCoordinates, offset, ends, stride, opt_right) { const right = opt_right !== undefined ? opt_right : false; for (let i = 0, ii = ends.length; i < ii; ++i) { const end = ends[i]; - const isClockwise = _ol_geom_flat_orient_.linearRingIsClockwise( + const isClockwise = linearRingIsClockwise( flatCoordinates, offset, end, stride); const reverse = i === 0 ? (right && isClockwise) || (!right && !isClockwise) : @@ -115,7 +114,7 @@ _ol_geom_flat_orient_.orientLinearRings = function(flatCoordinates, offset, ends offset = end; } return offset; -}; +} /** @@ -131,11 +130,10 @@ _ol_geom_flat_orient_.orientLinearRings = function(flatCoordinates, offset, ends * @param {boolean=} opt_right Follow the right-hand rule for orientation. * @return {number} End. */ -_ol_geom_flat_orient_.orientLinearRingss = function(flatCoordinates, offset, endss, stride, opt_right) { +export function orientLinearRingsArray(flatCoordinates, offset, endss, stride, opt_right) { for (let i = 0, ii = endss.length; i < ii; ++i) { - offset = _ol_geom_flat_orient_.orientLinearRings( + offset = orientLinearRings( flatCoordinates, offset, endss[i], stride, opt_right); } return offset; -}; -export default _ol_geom_flat_orient_; +} diff --git a/src/ol/render/webgl/LineStringReplay.js b/src/ol/render/webgl/LineStringReplay.js index 89e411ce5a..fb24b1aff7 100644 --- a/src/ol/render/webgl/LineStringReplay.js +++ b/src/ol/render/webgl/LineStringReplay.js @@ -5,7 +5,7 @@ import {getUid, inherits} from '../../index.js'; import {equals} from '../../array.js'; import {asArray} from '../../color.js'; import {intersects} from '../../extent.js'; -import _ol_geom_flat_orient_ from '../../geom/flat/orient.js'; +import {linearRingIsClockwise} from '../../geom/flat/orient.js'; import _ol_geom_flat_transform_ from '../../geom/flat/transform.js'; import {lineStringIsClosed} from '../../geom/flat/topology.js'; import {isEmpty} from '../../obj.js'; @@ -252,7 +252,7 @@ WebGLLineStringReplay.prototype.drawCoordinates_ = function(flatCoordinates, off if (closed) { n = n || numVertices / 7; - sign = _ol_geom_flat_orient_.linearRingIsClockwise([p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]], 0, 6, 2) + sign = linearRingIsClockwise([p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]], 0, 6, 2) ? 1 : -1; numVertices = this.addVertices_(p0, p1, p2, diff --git a/src/ol/render/webgl/PolygonReplay.js b/src/ol/render/webgl/PolygonReplay.js index 75b9fc747a..ae407045ee 100644 --- a/src/ol/render/webgl/PolygonReplay.js +++ b/src/ol/render/webgl/PolygonReplay.js @@ -7,7 +7,7 @@ import {asArray} from '../../color.js'; import {intersects} from '../../extent.js'; import {isEmpty} from '../../obj.js'; import {linearRingContainsXY} from '../../geom/flat/contains.js'; -import _ol_geom_flat_orient_ from '../../geom/flat/orient.js'; +import {linearRingIsClockwise} from '../../geom/flat/orient.js'; import _ol_geom_flat_transform_ from '../../geom/flat/transform.js'; import {fragment, vertex} from '../webgl/polygonreplay/defaultshader.js'; import Locations from '../webgl/polygonreplay/defaultshader/Locations.js'; @@ -140,7 +140,7 @@ WebGLPolygonReplay.prototype.drawCoordinates_ = function( */ WebGLPolygonReplay.prototype.processFlatCoordinates_ = function( flatCoordinates, stride, list, rtree, clockwise) { - const isClockwise = _ol_geom_flat_orient_.linearRingIsClockwise(flatCoordinates, + const isClockwise = linearRingIsClockwise(flatCoordinates, 0, flatCoordinates.length, stride); let i, ii; let n = this.vertices.length / 2; @@ -404,7 +404,7 @@ WebGLPolygonReplay.prototype.clipEars_ = function(list, rtree, simple, ccw) { variableCriterion) { //The diagonal is completely inside the polygon if (simple || p0.reflex === false || p2.reflex === false || - _ol_geom_flat_orient_.linearRingIsClockwise([s0.p0.x, s0.p0.y, p0.x, + linearRingIsClockwise([s0.p0.x, s0.p0.y, p0.x, p0.y, p1.x, p1.y, p2.x, p2.y, s3.p1.x, s3.p1.y], 0, 10, 2) === !ccw) { //The diagonal is persumably valid, we have an ear this.indices[numIndices++] = p0.i; @@ -533,7 +533,7 @@ WebGLPolygonReplay.prototype.isClockwise_ = function(list) { flatCoordinates[i++] = seg.p0.y; seg = list.nextItem(); } while (seg !== start); - return _ol_geom_flat_orient_.linearRingIsClockwise(flatCoordinates, 0, length, 2); + return linearRingIsClockwise(flatCoordinates, 0, length, 2); }; diff --git a/test/spec/ol/geom/flat/orient.test.js b/test/spec/ol/geom/flat/orient.test.js index cc481c01fd..6dd173317a 100644 --- a/test/spec/ol/geom/flat/orient.test.js +++ b/test/spec/ol/geom/flat/orient.test.js @@ -1,4 +1,5 @@ -import _ol_geom_flat_orient_ from '../../../../../src/ol/geom/flat/orient.js'; +import {linearRingIsClockwise, linearRingIsOriented, + linearRingsAreOriented, orientLinearRings, orientLinearRingsArray} from '../../../../../src/ol/geom/flat/orient.js'; describe('ol.geom.flat.orient', function() { @@ -7,14 +8,14 @@ describe('ol.geom.flat.orient', function() { it('identifies clockwise rings', function() { const flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; - const isClockwise = _ol_geom_flat_orient_.linearRingIsClockwise( + const isClockwise = linearRingIsClockwise( flatCoordinates, 0, flatCoordinates.length, 2); expect(isClockwise).to.be(true); }); it('identifies anti-clockwise rings', function() { const flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3]; - const isClockwise = _ol_geom_flat_orient_.linearRingIsClockwise( + const isClockwise = linearRingIsClockwise( flatCoordinates, 0, flatCoordinates.length, 2); expect(isClockwise).to.be(false); }); @@ -22,7 +23,7 @@ describe('ol.geom.flat.orient', function() { }); describe('ol.geom.flat.orient.linearRingsAreOriented()', function() { - const oriented = _ol_geom_flat_orient_.linearRingsAreOriented; + const oriented = linearRingIsOriented; const rightCoords = [ -180, -90, 180, -90, 180, 90, -180, 90, -180, -90, @@ -49,7 +50,7 @@ describe('ol.geom.flat.orient', function() { }); describe('ol.geom.flat.orient.linearRingssAreOriented()', function() { - const oriented = _ol_geom_flat_orient_.linearRingssAreOriented; + const oriented = linearRingsAreOriented; const rightCoords = [ -180, -90, 180, -90, 180, 90, -180, 90, -180, -90, @@ -80,7 +81,7 @@ describe('ol.geom.flat.orient', function() { }); describe('ol.geom.flat.orient.orientLinearRings()', function() { - const orient = _ol_geom_flat_orient_.orientLinearRings; + const orient = orientLinearRings; const rightCoords = [ -180, -90, 180, -90, 180, 90, -180, 90, -180, -90, @@ -117,7 +118,7 @@ describe('ol.geom.flat.orient', function() { }); describe('ol.geom.flat.orient.orientLinearRingss()', function() { - const orient = _ol_geom_flat_orient_.orientLinearRingss; + const orient = orientLinearRingsArray; const rightCoords = [ -180, -90, 180, -90, 180, 90, -180, 90, -180, -90,