Named exports instead of static functions from ol/geom/Polygon

This commit is contained in:
Tim Schaub
2017-12-20 14:49:56 -07:00
parent dae1921533
commit c89738cadb
10 changed files with 39 additions and 39 deletions

View File

@@ -37,12 +37,12 @@ The map and sources no longer accept a `logo` option. Instead, if you wish to a
#### Replacement of `ol/Sphere` constructor with `ol/sphere` functions
The `ol/Sphere` constructor has been removed. If you were using the `getGeodesicArea` method, use the `getArea` function instead. If you were using the `haversineDistance` method, use the `getDistance` function instead. The `circular` method in `ol/Polygon` no longer takes a sphere as the first argument.
The `ol/Sphere` constructor has been removed. If you were using the `getGeodesicArea` method, use the `getArea` function instead. If you were using the `haversineDistance` method, use the `getDistance` function instead. The `circular` method in `ol/geom/Polygon` no longer takes a sphere as the first argument.
Examples before:
```js
// using ol@4
import Polygon from 'ol/Polygon';
import Polygon from 'ol/geom/Polygon';
import Sphere from 'ol/Sphere';
var sphere = new Sphere(Sphere.DEFAULT_RADIUS);
@@ -54,7 +54,7 @@ var circle = Polygon.circular(sphere, center, radius);
Examples after:
```js
// using ol@5
import {circular as circularPolygon} from 'ol/Polygon';
import {circular as circularPolygon} from 'ol/geom/Polygon';
import {getArea, getDistance} from 'ol/sphere'; // note the lowercase
var area = getArea(polygon);

View File

@@ -1,7 +1,7 @@
import _ol_Feature_ from '../src/ol/Feature.js';
import _ol_Map_ from '../src/ol/Map.js';
import _ol_View_ from '../src/ol/View.js';
import Polygon from '../src/ol/geom/Polygon.js';
import {circular as circularPolygon} from '../src/ol/geom/Polygon.js';
import TileLayer from '../src/ol/layer/Tile.js';
import _ol_layer_Vector_ from '../src/ol/layer/Vector.js';
import _ol_source_TileWMS_ from '../src/ol/source/TileWMS.js';
@@ -60,7 +60,7 @@ var radius = 800000;
var x, y;
for (x = -180; x < 180; x += 30) {
for (y = -90; y < 90; y += 30) {
var circle4326 = Polygon.circular([x, y], radius, 64);
var circle4326 = circularPolygon([x, y], radius, 64);
var circle3857 = circle4326.clone().transform('EPSG:4326', 'EPSG:3857');
vectorLayer4326.getSource().addFeature(new _ol_Feature_(circle4326));
vectorLayer3857.getSource().addFeature(new _ol_Feature_(circle3857));

View File

@@ -6,7 +6,7 @@ import _ol_GeolocationProperty_ from './GeolocationProperty.js';
import _ol_Object_ from './Object.js';
import _ol_events_ from './events.js';
import EventType from './events/EventType.js';
import Polygon from './geom/Polygon.js';
import {circular as circularPolygon} from './geom/Polygon.js';
import _ol_has_ from './has.js';
import {toRadians} from './math.js';
import {get as getProjection, getTransformFromProjections, identityTransform} from './proj.js';
@@ -167,7 +167,7 @@ Geolocation.prototype.positionChange_ = function(position) {
this.set(_ol_GeolocationProperty_.POSITION, projectedPosition);
this.set(_ol_GeolocationProperty_.SPEED,
coords.speed === null ? undefined : coords.speed);
var geometry = Polygon.circular(this.position_, coords.accuracy);
var geometry = circularPolygon(this.position_, coords.accuracy);
geometry.applyTransform(this.transform_);
this.set(_ol_GeolocationProperty_.ACCURACY_GEOMETRY, geometry);
this.changed();

View File

@@ -15,7 +15,7 @@ import _ol_coordinate_ from './coordinate.js';
import {inAndOut} from './easing.js';
import {getForViewAndSize, getCenter, getHeight, getWidth, isEmpty} from './extent.js';
import GeometryType from './geom/GeometryType.js';
import Polygon from './geom/Polygon.js';
import {fromExtent as polygonFromExtent} from './geom/Polygon.js';
import SimpleGeometry from './geom/SimpleGeometry.js';
import {clamp, modulo} from './math.js';
import _ol_obj_ from './obj.js';
@@ -884,10 +884,10 @@ _ol_View_.prototype.fit = function(geometryOrExtent, opt_options) {
24); // Invalid extent or geometry provided as `geometry`
assert(!isEmpty(geometryOrExtent),
25); // Cannot fit empty extent provided as `geometry`
geometry = Polygon.fromExtent(geometryOrExtent);
geometry = polygonFromExtent(geometryOrExtent);
} else if (geometryOrExtent.getType() === GeometryType.CIRCLE) {
geometryOrExtent = geometryOrExtent.getExtent();
geometry = Polygon.fromExtent(geometryOrExtent);
geometry = polygonFromExtent(geometryOrExtent);
geometry.rotate(this.getRotation(), getCenter(geometryOrExtent));
} else {
geometry = geometryOrExtent;

View File

@@ -367,6 +367,8 @@ Polygon.prototype.setFlatCoordinates = function(layout, flatCoordinates, ends) {
this.changed();
};
export default Polygon;
/**
* Create an approximation of a circle on the surface of a sphere.
@@ -380,7 +382,7 @@ Polygon.prototype.setFlatCoordinates = function(layout, flatCoordinates, ends) {
* @return {ol.geom.Polygon} The "circular" polygon.
* @api
*/
Polygon.circular = function(center, radius, opt_n, opt_sphereRadius) {
export function circular(center, radius, opt_n, opt_sphereRadius) {
var n = opt_n ? opt_n : 32;
/** @type {Array.<number>} */
var flatCoordinates = [];
@@ -393,7 +395,7 @@ Polygon.circular = function(center, radius, opt_n, opt_sphereRadius) {
polygon.setFlatCoordinates(
GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]);
return polygon;
};
}
/**
@@ -402,7 +404,7 @@ Polygon.circular = function(center, radius, opt_n, opt_sphereRadius) {
* @return {ol.geom.Polygon} The polygon.
* @api
*/
Polygon.fromExtent = function(extent) {
export function fromExtent(extent) {
var minX = extent[0];
var minY = extent[1];
var maxX = extent[2];
@@ -413,7 +415,7 @@ Polygon.fromExtent = function(extent) {
polygon.setFlatCoordinates(
GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]);
return polygon;
};
}
/**
@@ -425,7 +427,7 @@ Polygon.fromExtent = function(extent) {
* @return {ol.geom.Polygon} Polygon geometry.
* @api
*/
Polygon.fromCircle = function(circle, opt_sides, opt_angle) {
export function fromCircle(circle, opt_sides, opt_angle) {
var sides = opt_sides ? opt_sides : 32;
var stride = circle.getStride();
var layout = circle.getLayout();
@@ -437,10 +439,9 @@ Polygon.fromCircle = function(circle, opt_sides, opt_angle) {
}
var ends = [flatCoordinates.length];
polygon.setFlatCoordinates(layout, flatCoordinates, ends);
Polygon.makeRegular(
polygon, circle.getCenter(), circle.getRadius(), opt_angle);
makeRegular(polygon, circle.getCenter(), circle.getRadius(), opt_angle);
return polygon;
};
}
/**
@@ -451,7 +452,7 @@ Polygon.fromCircle = function(circle, opt_sides, opt_angle) {
* @param {number=} opt_angle Start angle for the first vertex of the polygon in
* radians. Default is 0.
*/
Polygon.makeRegular = function(polygon, center, radius, opt_angle) {
export function makeRegular(polygon, center, radius, opt_angle) {
var flatCoordinates = polygon.getFlatCoordinates();
var layout = polygon.getLayout();
var stride = polygon.getStride();
@@ -466,5 +467,4 @@ Polygon.makeRegular = function(polygon, center, radius, opt_angle) {
flatCoordinates[offset + 1] = center[1] + (radius * Math.sin(angle));
}
polygon.setFlatCoordinates(layout, flatCoordinates, ends);
};
export default Polygon;
}

View File

@@ -18,7 +18,7 @@ import MultiLineString from '../geom/MultiLineString.js';
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 Polygon, {fromCircle, makeRegular} from '../geom/Polygon.js';
import DrawEventType from '../interaction/DrawEventType.js';
import _ol_interaction_Pointer_ from '../interaction/Pointer.js';
import _ol_interaction_Property_ from '../interaction/Property.js';
@@ -795,10 +795,10 @@ Draw.createRegularPolygon = function(opt_sides, opt_angle) {
var radius = Math.sqrt(
_ol_coordinate_.squaredDistance(center, end));
var geometry = opt_geometry ? /** @type {ol.geom.Polygon} */ (opt_geometry) :
Polygon.fromCircle(new Circle(center), opt_sides);
fromCircle(new Circle(center), opt_sides);
var angle = opt_angle ? opt_angle :
Math.atan((end[1] - center[1]) / (end[0] - center[0]));
Polygon.makeRegular(geometry, center, radius, angle);
makeRegular(geometry, center, radius, angle);
return geometry;
}
);

View File

@@ -10,7 +10,7 @@ import Event from '../events/Event.js';
import {boundingExtent, getArea} from '../extent.js';
import GeometryType from '../geom/GeometryType.js';
import Point from '../geom/Point.js';
import Polygon from '../geom/Polygon.js';
import {fromExtent as polygonFromExtent} from '../geom/Polygon.js';
import _ol_interaction_ExtentEventType_ from '../interaction/ExtentEventType.js';
import _ol_interaction_Pointer_ from '../interaction/Pointer.js';
import _ol_layer_Vector_ from '../layer/Vector.js';
@@ -376,7 +376,7 @@ _ol_interaction_Extent_.prototype.createOrUpdateExtentFeature_ = function(extent
if (!extent) {
extentFeature = new _ol_Feature_({});
} else {
extentFeature = new _ol_Feature_(Polygon.fromExtent(extent));
extentFeature = new _ol_Feature_(polygonFromExtent(extent));
}
this.extentFeature_ = extentFeature;
this.extentOverlay_.getSource().addFeature(extentFeature);
@@ -384,7 +384,7 @@ _ol_interaction_Extent_.prototype.createOrUpdateExtentFeature_ = function(extent
if (!extent) {
extentFeature.setGeometry(undefined);
} else {
extentFeature.setGeometry(Polygon.fromExtent(extent));
extentFeature.setGeometry(polygonFromExtent(extent));
}
}
return extentFeature;

View File

@@ -10,7 +10,7 @@ import EventType from '../events/EventType.js';
import {boundingExtent, createEmpty} from '../extent.js';
import {TRUE, FALSE} from '../functions.js';
import GeometryType from '../geom/GeometryType.js';
import Polygon from '../geom/Polygon.js';
import {fromCircle} from '../geom/Polygon.js';
import _ol_interaction_Pointer_ from '../interaction/Pointer.js';
import _ol_obj_ from '../obj.js';
import _ol_source_Vector_ from '../source/Vector.js';
@@ -435,7 +435,7 @@ _ol_interaction_Snap_.prototype.updateFeature_ = function(feature) {
* @private
*/
_ol_interaction_Snap_.prototype.writeCircleGeometry_ = function(feature, geometry) {
var polygon = Polygon.fromCircle(geometry);
var polygon = fromCircle(geometry);
var coordinates = polygon.getCoordinates()[0];
var i, ii, segment, segmentData;
for (i = 0, ii = coordinates.length - 1; i < ii; ++i) {

View File

@@ -1,10 +1,10 @@
import * as _ol_extent_ from '../../../../src/ol/extent.js';
import Circle from '../../../../src/ol/geom/Circle.js';
import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import Polygon, {fromCircle, fromExtent} from '../../../../src/ol/geom/Polygon.js';
describe('ol.geom.Polygon', function() {
describe('ol/geom/Polygon', function() {
it('can be constructed with a null geometry', function() {
expect(function() {
@@ -561,10 +561,10 @@ describe('ol.geom.Polygon', function() {
});
});
describe('ol.geom.Polygon.fromExtent', function() {
describe('fromExtent()', function() {
it('creates the correct polygon', function() {
var extent = [1, 2, 3, 5];
var polygon = Polygon.fromExtent(extent);
var polygon = fromExtent(extent);
var flatCoordinates = polygon.getFlatCoordinates();
expect(flatCoordinates).to.eql(
[1, 2, 1, 5, 3, 5, 3, 2, 1, 2]);
@@ -574,11 +574,11 @@ describe('ol.geom.Polygon', function() {
});
});
describe('ol.geom.Polygon.fromCircle', function() {
describe('fromCircle()', function() {
it('creates a regular polygon', function() {
var circle = new Circle([0, 0, 0], 1, 'XYZ');
var polygon = Polygon.fromCircle(circle);
var polygon = fromCircle(circle);
var coordinates = polygon.getLinearRing(0).getCoordinates();
expect(coordinates[0].length).to.eql(3);
expect(coordinates[0][2]).to.eql(0);
@@ -599,7 +599,7 @@ describe('ol.geom.Polygon', function() {
it('creates a regular polygon with custom sides and angle', function() {
var circle = new Circle([0, 0], 1);
var polygon = Polygon.fromCircle(circle, 4, Math.PI / 2);
var polygon = fromCircle(circle, 4, Math.PI / 2);
var coordinates = polygon.getLinearRing(0).getCoordinates();
expect(coordinates[4]).to.eql(coordinates[0]);
expect(coordinates[0][0]).to.roughlyEqual(0, 1e-9);

View File

@@ -1,7 +1,7 @@
import _ol_Map_ from '../../../../src/ol/Map.js';
import _ol_View_ from '../../../../src/ol/View.js';
import * as _ol_extent_ from '../../../../src/ol/extent.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import {fromExtent as polygonFromExtent} from '../../../../src/ol/geom/Polygon.js';
import DragZoom from '../../../../src/ol/interaction/DragZoom.js';
import _ol_layer_Vector_ from '../../../../src/ol/layer/Vector.js';
import _ol_render_Box_ from '../../../../src/ol/render/Box.js';
@@ -72,7 +72,7 @@ describe('ol.interaction.DragZoom', function() {
var box = new _ol_render_Box_();
var extent = [-110, 40, -90, 60];
box.geometry_ = Polygon.fromExtent(extent);
box.geometry_ = polygonFromExtent(extent);
interaction.box_ = box;
interaction.onBoxEnd();
@@ -94,7 +94,7 @@ describe('ol.interaction.DragZoom', function() {
var box = new _ol_render_Box_();
var extent = [-11.25, -11.25, 11.25, 11.25];
box.geometry_ = Polygon.fromExtent(extent);
box.geometry_ = polygonFromExtent(extent);
interaction.box_ = box;
map.getView().setResolution(0.25);