Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove geojson-flatten dependency #1066

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
},
"dependencies": {
"@mapbox/geojson-area": "^0.2.2",
"@mapbox/geojson-extent": "^1.0.1",
"@mapbox/geojson-normalize": "^0.0.1",
"@mapbox/point-geometry": "^0.1.0",
"fast-deep-equal": "^3.1.3",
Expand Down
20 changes: 18 additions & 2 deletions src/lib/constrain_feature_movement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import extent from '@mapbox/geojson-extent';
import * as Constants from '../constants.js';

const {
Expand All @@ -7,8 +6,25 @@ const {
LAT_RENDERED_MIN,
LAT_RENDERED_MAX,
LNG_MIN,
LNG_MAX
LNG_MAX,
} = Constants;
function extent(feature) {
const depth = {
Point: 0,
LineString: 1,
Polygon: 2,
MultiPoint: 1,
MultiLineString: 2,
MultiPolygon: 3,
}[feature.geometry.type];

const coords = [feature.geometry.coordinates].flat(depth);
const lngs = coords.map(coord => coord[0]);
const lats = coords.map(coord => coord[1]);
const min = vals => Math.min.apply(null, vals);
const max = vals => Math.max.apply(null, vals);
return [min(lngs), min(lats), max(lngs), max(lats)];
}

// Ensure that we do not drag north-south far enough for
// - any part of any feature to exceed the poles
Expand Down
28 changes: 14 additions & 14 deletions test/constrain_feature_movement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ test('constrainFeatureMovement line, requiring eastern wrap', () => {

test('constrainFeatureMovement polygon, no constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const constrainedDelta = constrainFeatureMovement([polygon], {
lat: 13,
lng: 0
Expand All @@ -187,7 +187,7 @@ test('constrainFeatureMovement polygon, no constraint', () => {

test('constrainFeatureMovement polygon, requiring northern inner constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[80, 80], [81, 81], [81, 82], [80, 80]];
polygon.geometry.coordinates = [[[80, 80], [81, 81], [81, 82], [80, 80]]];
const constrainedDelta = constrainFeatureMovement([polygon], {
lat: 7,
lng: 0
Expand All @@ -201,7 +201,7 @@ test('constrainFeatureMovement polygon, requiring northern inner constraint', ()

test('constrainFeatureMovement polygon, requiring northern outer constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[30, 30], [30, 40], [81, 81], [30, 30]];
polygon.geometry.coordinates = [[[30, 30], [30, 40], [81, 81], [30, 30]]];
const constrainedDelta = constrainFeatureMovement([polygon], {
lat: 12,
lng: 0
Expand All @@ -215,7 +215,7 @@ test('constrainFeatureMovement polygon, requiring northern outer constraint', ()

test('constrainFeatureMovement polygon, requiring southern inner constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[-80, -80], [-81, -81], [-81, -82], [-80, -80]];
polygon.geometry.coordinates = [[[-80, -80], [-81, -81], [-81, -82], [-80, -80]]];
const constrainedDelta = constrainFeatureMovement([polygon], {
lat: -7,
lng: 0
Expand All @@ -229,7 +229,7 @@ test('constrainFeatureMovement polygon, requiring southern inner constraint', ()

test('constrainFeatureMovement polygon, requiring southern outer constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[-30, -30], [-30, -40], [-81, -81], [-30, -30]];
polygon.geometry.coordinates = [[[-30, -30], [-30, -40], [-81, -81], [-30, -30]]];
const constrainedDelta = constrainFeatureMovement([polygon], {
lat: -12,
lng: 0
Expand All @@ -243,7 +243,7 @@ test('constrainFeatureMovement polygon, requiring southern outer constraint', ()

test('constrainFeatureMovement polygon, requiring western wrap', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const constrainedDelta = constrainFeatureMovement([polygon], {
lat: 70,
lng: -270
Expand All @@ -257,7 +257,7 @@ test('constrainFeatureMovement polygon, requiring western wrap', () => {

test('constrainFeatureMovement polygon, requiring eastern wrap', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const constrainedDelta = constrainFeatureMovement([polygon], {
lat: 35,
lng: 270
Expand All @@ -271,7 +271,7 @@ test('constrainFeatureMovement polygon, requiring eastern wrap', () => {

test('constrainFeatureMovement many features, no constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const point = getGeoJSON('point');
point.geometry.coordinates = [15, 15];
const line = getGeoJSON('line');
Expand All @@ -289,7 +289,7 @@ test('constrainFeatureMovement many features, no constraint', () => {

test('constrainFeatureMovement many features, requiring northern inner constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[80, 80], [81, 81], [81, 82], [80, 80]];
polygon.geometry.coordinates = [[[80, 80], [81, 81], [81, 82], [80, 80]]];
const point = getGeoJSON('point');
point.geometry.coordinates = [15, 15];
const line = getGeoJSON('line');
Expand All @@ -307,7 +307,7 @@ test('constrainFeatureMovement many features, requiring northern inner constrain

test('constrainFeatureMovement many features, requiring northern outer constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const point = getGeoJSON('point');
point.geometry.coordinates = [15, 15];
const line = getGeoJSON('line');
Expand All @@ -325,7 +325,7 @@ test('constrainFeatureMovement many features, requiring northern outer constrain

test('constrainFeatureMovement many features, requiring southern inner constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[-80, -80], [-81, -81], [-81, -82], [-80, -80]];
polygon.geometry.coordinates = [[[-80, -80], [-81, -81], [-81, -82], [-80, -80]]];
const point = getGeoJSON('point');
point.geometry.coordinates = [15, 15];
const line = getGeoJSON('line');
Expand All @@ -343,7 +343,7 @@ test('constrainFeatureMovement many features, requiring southern inner constrain

test('constrainFeatureMovement many features, requiring southern outer constraint', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const point = getGeoJSON('point');
point.geometry.coordinates = [15, 15];
const line = getGeoJSON('line');
Expand All @@ -361,7 +361,7 @@ test('constrainFeatureMovement many features, requiring southern outer constrain

test('constrainFeatureMovement many features, requiring western wrap', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const point = getGeoJSON('point');
point.geometry.coordinates = [15, 15];
const line = getGeoJSON('line');
Expand All @@ -379,7 +379,7 @@ test('constrainFeatureMovement many features, requiring western wrap', () => {

test('constrainFeatureMovement many features, requiring eastern wrap', () => {
const polygon = getGeoJSON('polygon');
polygon.geometry.coordinates = [[0, 0], [10, 10], [20, 20], [0, 0]];
polygon.geometry.coordinates = [[[0, 0], [10, 10], [20, 20], [0, 0]]];
const point = getGeoJSON('point');
point.geometry.coordinates = [15, 15];
const line = getGeoJSON('line');
Expand Down
Loading