refactor: ts error

This commit is contained in:
thinkinggis 2020-04-03 14:25:34 +08:00
parent b7498103a0
commit 239c99948d
7 changed files with 17 additions and 14 deletions

View File

@ -51,13 +51,13 @@ describe('moveFeature', () => {
const res = moveFeature([pointFeature], delta) as Array< const res = moveFeature([pointFeature], delta) as Array<
Feature<Geometry, Properties> Feature<Geometry, Properties>
>; >;
expect(res[0].geometry.coordinates).toEqual([131, 48]); expect(res[0]?.geometry?.coordinates).toEqual([131, 48]);
}); });
it('move BBox', () => { it('move BBox', () => {
const res = moveFeature([polyon], delta) as Array< const res = moveFeature([polyon], delta) as Array<
Feature<Geometry, Properties> Feature<Geometry, Properties>
>; >;
expect(res[0].geometry.coordinates).toEqual([ expect(res[0]?.geometry?.coordinates).toEqual([
[ [
[50.5703125, 46.583289756006316], [50.5703125, 46.583289756006316],
[72.3671875, 46.583289756006316], [72.3671875, 46.583289756006316],
@ -71,7 +71,7 @@ describe('moveFeature', () => {
const res = moveFeature([line], delta) as Array< const res = moveFeature([line], delta) as Array<
Feature<Geometry, Properties> Feature<Geometry, Properties>
>; >;
expect(res[0].geometry.coordinates).toEqual([ expect(res[0]?.geometry?.coordinates).toEqual([
[55.31640625, 63.91523303947614], [55.31640625, 63.91523303947614],
[72.015625, 63.59334083012024], [72.015625, 63.59334083012024],
[71.48828125, 59.07787626787517], [71.48828125, 59.07787626787517],

View File

@ -80,7 +80,7 @@ export default abstract class DrawMode extends EventEmitter {
throw new Error('子类未实现该方法'); throw new Error('子类未实现该方法');
} }
public getCurrentVertex(feature: Feature) { public getCurrentVertex() {
return this.currentVertex; return this.currentVertex;
} }
public getCurrentFeature() { public getCurrentFeature() {

View File

@ -59,12 +59,12 @@ export default class DrawPolygon extends DrawFeature {
public addVertex(vertex: Feature<Geometries, Properties>) { public addVertex(vertex: Feature<Geometries, Properties>) {
// @ts-ignore // @ts-ignore
const id = vertex.properties.id; const id = vertex.properties.id;
const coord = vertex.geometry.coordinates as Position; const coord = vertex?.geometry?.coordinates as Position;
const feature = this.currentFeature as Feature<Geometries, Properties>; const feature = this.currentFeature as Feature<Geometries, Properties>;
const type = feature.geometry.type; const type = feature?.geometry?.type;
const points = []; const points = [];
if (type === 'Polygon') { if (type === 'Polygon') {
const coords = feature.geometry.coordinates as Position[][]; const coords = feature?.geometry?.coordinates as Position[][];
coords[0].splice(id + 1, 0, coord); coords[0].splice(id + 1, 0, coord);
for (let i = 0; i < coords[0].length - 1; i++) { for (let i = 0; i < coords[0].length - 1; i++) {
points.push({ points.push({
@ -73,7 +73,7 @@ export default class DrawPolygon extends DrawFeature {
}); });
} }
} else { } else {
const coords = feature.geometry.coordinates as Position[]; const coords = feature?.geometry?.coordinates as Position[];
coords.splice(id + 1, 0, coord); coords.splice(id + 1, 0, coord);
for (const coor of coords) { for (const coor of coords) {
points.push({ points.push({
@ -169,6 +169,7 @@ export default class DrawPolygon extends DrawFeature {
} else { } else {
// @ts-ignore // @ts-ignore
const id = selectVertexed.properties.id * 1; const id = selectVertexed.properties.id * 1;
// @ts-ignore
selectVertexed.geometry.coordinates = [vertex.lng, vertex.lat]; selectVertexed.geometry.coordinates = [vertex.lng, vertex.lat];
// @ts-ignore // @ts-ignore
this.pointFeatures[id].geometry.coordinates = [vertex.lng, vertex.lat]; this.pointFeatures[id].geometry.coordinates = [vertex.lng, vertex.lat];
@ -226,15 +227,15 @@ export default class DrawPolygon extends DrawFeature {
private editPolygonVertex(id: number, vertex: ILngLat) { private editPolygonVertex(id: number, vertex: ILngLat) {
const feature = this.currentFeature as Feature<Geometries, Properties>; const feature = this.currentFeature as Feature<Geometries, Properties>;
const type = feature.geometry.type; const type = feature?.geometry?.type;
if (type === 'Polygon') { if (type === 'Polygon') {
const coords = feature.geometry.coordinates as Position[][]; const coords = feature?.geometry?.coordinates as Position[][];
coords[0][id] = [vertex.lng, vertex.lat]; coords[0][id] = [vertex.lng, vertex.lat];
if (-id === 0) { if (-id === 0) {
coords[0][coords[0].length - 1] = [vertex.lng, vertex.lat]; coords[0][coords[0].length - 1] = [vertex.lng, vertex.lat];
} }
} else { } else {
const coords = feature.geometry.coordinates as Position[]; const coords = feature?.geometry?.coordinates as Position[];
coords[id] = [vertex.lng, vertex.lat]; coords[id] = [vertex.lng, vertex.lat];
} }
this.setCurrentFeature(feature); this.setCurrentFeature(feature);

View File

@ -1,7 +1,7 @@
import { ILayer, LineLayer, PointLayer, PolygonLayer } from '@antv/l7'; import { ILayer, LineLayer, PointLayer, PolygonLayer } from '@antv/l7';
import { FeatureCollection } from '@turf/helpers'; import { FeatureCollection } from '@turf/helpers';
export function renderFeature(fe: FeatureCollection, style: any): ILayer[] { export function renderFeature(fe: FeatureCollection, style: any): ILayer[] {
const type = fe.features[0].geometry.type; const type = fe.features[0]?.geometry?.type;
let layers; let layers;
switch (type) { switch (type) {
case 'Point': case 'Point':

View File

@ -91,8 +91,9 @@ export default class MapboxService
public getContainer(): HTMLElement | null { public getContainer(): HTMLElement | null {
return this.map.getContainer(); return this.map.getContainer();
} }
public getMapCanvasContainer(): HTMLElement { public getMapCanvasContainer(): HTMLElement {
return this.map.getCanvasContainer(); return this.map.getCanvasContainer() as HTMLElement;
} }
public getSize(): [number, number] { public getSize(): [number, number] {

View File

@ -59,6 +59,7 @@ export default interface IMapController {
// get map params // get map params
getType(): string; getType(): string;
getMapContainer(): HTMLElement | null; getMapContainer(): HTMLElement | null;
getMapCanvasContainer(): HTMLElement;
// control with raw map // control with raw map
setRotation(rotation: number): void; setRotation(rotation: number): void;

View File

@ -4179,7 +4179,7 @@
dependencies: dependencies:
"@turf/helpers" "^5.1.5" "@turf/helpers" "^5.1.5"
"@turf/midpoint@5.1.x": "@turf/midpoint@5.1.x", "@turf/midpoint@^5.1.5":
version "5.1.5" version "5.1.5"
resolved "https://registry.npmjs.org/@turf/midpoint/-/midpoint-5.1.5.tgz#e261f6b2b0ea8124cceff552a262dd465c9d05f0" resolved "https://registry.npmjs.org/@turf/midpoint/-/midpoint-5.1.5.tgz#e261f6b2b0ea8124cceff552a262dd465c9d05f0"
integrity sha1-4mH2srDqgSTM7/VSomLdRlydBfA= integrity sha1-4mH2srDqgSTM7/VSomLdRlydBfA=