Skip to content

Commit

Permalink
fix: 传入的 coods 是 line 格式时也会删除最后一个点
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Oct 14, 2023
1 parent 58b7c95 commit 0c01879
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 11 deletions.
27 changes: 27 additions & 0 deletions src/helpers/__tests__/coordsOfGeoJSON2AMapPolygonPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import coordsOfGeoJSON2AMapPolygonPath from '../coordsOfGeoJSON2AMapPolygonPath';

describe('coordsOfGeoJSON2AMapPolygonPath', () => {
test('should remove the last position point when it is a ring', () => {
const input: GeoJSON.Position[] = [[1, 2], [3, 4], [5, 6], [1, 2]];
const output: GeoJSON.Position[] = [[1, 2], [3, 4], [5, 6]];
expect(coordsOfGeoJSON2AMapPolygonPath(input)).toEqual(output);
});

test('should not remove the last position point when it is not a ring line', () => {
const input: GeoJSON.Position[] = [[1, 2], [3, 4], [5, 6]];
const output: GeoJSON.Position[] = [[1, 2], [3, 4], [5, 6]];
expect(coordsOfGeoJSON2AMapPolygonPath(input)).toEqual(output);
});

test('should convert GeoJSON.Polygon coordinates', () => {
const input: GeoJSON.Polygon['coordinates'] = [[[1, 2], [3, 4], [5, 6], [1, 2]]];
const output: GeoJSON.Polygon['coordinates'] = [[[1, 2], [3, 4], [5, 6]]];
expect(coordsOfGeoJSON2AMapPolygonPath(input)).toEqual(output);
});

test('should convert GeoJSON.MultiPolygon coordinates', () => {
const input: GeoJSON.MultiPolygon['coordinates'] = [[[[1, 2], [3, 4], [5, 6], [1, 2]]]];
const output: GeoJSON.MultiPolygon['coordinates'] = [[[[1, 2], [3, 4], [5, 6]]]];
expect(coordsOfGeoJSON2AMapPolygonPath(input)).toEqual(output);
});
});
84 changes: 76 additions & 8 deletions src/helpers/__tests__/geoJSONHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isPosition, isLineCoords } from '../geoJSONHelper';
import {
isPosition,
isLineCoords,
isSamePosition,
isRingLineCoords,
} from '../geoJSONHelper';

describe('isPosition', () => {
test('should return true for valid position', () => {
Expand All @@ -20,18 +25,81 @@ describe('isPosition', () => {

describe('isLineCoords', () => {
test('should return true for valid line coordinates', () => {
const validLineCoords: GeoJSON.Position[] = [[1, 2], [3, 4], [5, 6]];
const validLineCoords1: GeoJSON.Position[] = [[1, 2], [3, 4]];
const validLineCoords2: GeoJSON.Position[] = [[1, 2], [3, 4], [5, 6]];

const result = isLineCoords(validLineCoords);

expect(result).toBe(true);
expect(isLineCoords(validLineCoords1)).toBe(true);
expect(isLineCoords(validLineCoords2)).toBe(true);
});

test('should return false for invalid line coordinates', () => {
const invalidLineCoords = [[1, 2], [3, '4'], [5, 6]];
const invalidLineCoords1 = [[1, 2], [3, '4'], [5, 6]];
const invalidLineCoords2 = [[0, 0]];

expect(isLineCoords(invalidLineCoords1)).toBe(false);
expect(isLineCoords(invalidLineCoords2)).toBe(false);
});
});

const result = isLineCoords(invalidLineCoords);
describe('isSamePosition', () => {
it('should return true if the positions are equal', () => {
const positionA: GeoJSON.Position = [0, 0];

expect(result).toBe(false);
expect(isSamePosition(positionA, positionA)).toBe(true);
});

it('should return true if the positions are equivalent', () => {
const positionA: GeoJSON.Position = [0, 0];
const positionB: GeoJSON.Position = [0, 0];

expect(isSamePosition(positionA, positionB)).toBe(true);
});

it('should throw an error if the positions are invalid', () => {
const positionA: GeoJSON.Position = [0, 0];
const positionB: GeoJSON.Position = [null, '0'] as any;

expect(() => isSamePosition(positionA, positionB)).toThrow('invalid position');
expect(() => isSamePosition(positionB, positionA)).toThrow('invalid position');
});
});

describe('isRingLineCoords', () => {
it('should return false if coords are not line coordinates', () => {
const coords = [
[0, 0],
[1, 1],
[2, 2],
];
expect(isRingLineCoords(coords)).toBe(false);
});

it('should return false if coords length is less than 4', () => {
const coords = [
[0, 0],
[1, 1],
[2, 2],
];
expect(isRingLineCoords(coords)).toBe(false);
});

it('should return false if first and last positions are not the same', () => {
const coords = [
[0, 0],
[1, 1],
[2, 2],
[3, 3],
];
expect(isRingLineCoords(coords)).toBe(false);
});

it('should return true if coords are line coordinates, length is greater than 3, and first and last positions are the same', () => {
const coords = [
[0, 0],
[1, 1],
[2, 2],
[0, 0],
];
expect(isRingLineCoords(coords)).toBe(true);
});
});
8 changes: 6 additions & 2 deletions src/helpers/coordsOfGeoJSON2AMapPolygonPath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isLineCoords } from './geoJSONHelper';
import { isLineCoords, isRingLineCoords } from './geoJSONHelper';

const coordsOfGeoJSONRingLine2AMapPolygonPath = (coords: GeoJSON.Position[]): typeof coords => {
const len = coords.length;
Expand All @@ -12,8 +12,12 @@ const coordsOfGeoJSONRingLine2AMapPolygonPath = (coords: GeoJSON.Position[]): ty
const coordsOfGeoJSON2AMapPolygonPath = (
coords: GeoJSON.Position[] | GeoJSON.Polygon['coordinates'] | GeoJSON.MultiPolygon['coordinates'],
): typeof coords => {
if (isRingLineCoords(coords as GeoJSON.Position[])) {
return coordsOfGeoJSONRingLine2AMapPolygonPath(coords as GeoJSON.Position[]);
}

if (isLineCoords(coords)) {
return coordsOfGeoJSONRingLine2AMapPolygonPath(coords);
return coords;
}

// trick way
Expand Down
18 changes: 17 additions & 1 deletion src/helpers/geoJSONHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ export const isPosition = (pos: any): pos is GeoJSON.Position => (

export const isLineCoords = (
line: any,
): line is GeoJSON.Position[] => Array.isArray(line) && line.every(isPosition);
): line is GeoJSON.Position[] => Array.isArray(line) && line.length >= 2 && line.every(isPosition);

export const isSamePosition = (positionA: GeoJSON.Position, positionB: GeoJSON.Position) => {
if (!isPosition(positionA) || !isPosition(positionB)) {
throw Error('invalid position');
}

return positionA === positionB || positionA.toString() === positionB.toString();
};

export const isRingLineCoords = (coords: GeoJSON.Position[]) => {
if (!isLineCoords(coords)) return false;
const coordsLen = coords.length;
const firstPosition = coords[0];
const lastPosition = coords[coordsLen - 1];
return coordsLen > 3 && isSamePosition(firstPosition, lastPosition);
};

0 comments on commit 0c01879

Please sign in to comment.