Region.ts 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

L
lang 已提交
20

S
sushuang 已提交
21 22 23 24
import BoundingRect from 'zrender/src/core/BoundingRect';
import * as bbox from 'zrender/src/core/bbox';
import * as vec2 from 'zrender/src/core/vector';
import * as polygonContain from 'zrender/src/contain/polygon';
1
100pah 已提交
25 26 27
import { GeoJSON, GeoSVGGraphicRoot } from './geoTypes';
import * as matrix from 'zrender/src/core/matrix';
import Element from 'zrender/src/Element';
S
sushuang 已提交
28

1
100pah 已提交
29
const TMP_TRANSFORM = [] as number[];
L
lang 已提交
30

1
100pah 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44
export class Region {

    readonly name: string;
    readonly type: 'geoJSON' | 'geoSVG';

    constructor(
        name: string
    ) {
        this.name = name;
    }

    /**
     * Get center point in data unit. That is,
     * for GeoJSONRegion, the unit is lat/lng,
45
     * for GeoSVGRegion, the unit is SVG local coord.
1
100pah 已提交
46 47 48 49 50 51 52 53 54 55 56
     */
    getCenter(): number[] {
        return;
    }

}


export class GeoJSONRegion extends Region {

    readonly type = 'geoJSON';
57 58 59 60 61 62 63

    readonly geometries: {
        type: 'polygon'; // FIXME:TS Is there other types?
        exterior: number[][];
        interiors?: number[][][];
    }[];

1
100pah 已提交
64
    private _center: number[];
L
lang 已提交
65

66 67
    // Injected outside.
    properties: GeoJSON['features'][0]['properties'];
L
lang 已提交
68

69
    private _rect: BoundingRect;
P
pissang 已提交
70

71 72 73

    constructor(
        name: string,
1
100pah 已提交
74
        geometries: GeoJSONRegion['geometries'],
75 76
        cp: GeoJSON['features'][0]['properties']['cp']
    ) {
1
100pah 已提交
77 78
        super(name);

79 80 81
        this.geometries = geometries;

        if (!cp) {
82
            const rect = this.getBoundingRect();
83 84 85 86 87 88 89 90
            cp = [
                rect.x + rect.width / 2,
                rect.y + rect.height / 2
            ];
        }
        else {
            cp = [cp[0], cp[1]];
        }
1
100pah 已提交
91
        this._center = cp;
92 93 94
    }

    getBoundingRect(): BoundingRect {
95
        const rect = this._rect;
S
sushuang 已提交
96 97 98
        if (rect) {
            return rect;
        }
L
lang 已提交
99

100 101 102 103 104 105
        const MAX_NUMBER = Number.MAX_VALUE;
        const min = [MAX_NUMBER, MAX_NUMBER];
        const max = [-MAX_NUMBER, -MAX_NUMBER];
        const min2 = [] as number[];
        const max2 = [] as number[];
        const geometries = this.geometries;
106
        let i = 0;
1
100pah 已提交
107
        for (; i < geometries.length; i++) {
S
sushuang 已提交
108 109 110
            // Only support polygon
            if (geometries[i].type !== 'polygon') {
                continue;
L
lang 已提交
111
            }
S
sushuang 已提交
112
            // Doesn't consider hole
113
            const exterior = geometries[i].exterior;
S
sushuang 已提交
114 115 116 117 118 119 120 121 122 123 124 125
            bbox.fromPoints(exterior, min2, max2);
            vec2.min(min, min, min2);
            vec2.max(max, max, max2);
        }
        // No data
        if (i === 0) {
            min[0] = min[1] = max[0] = max[1] = 0;
        }

        return (this._rect = new BoundingRect(
            min[0], min[1], max[0] - min[0], max[1] - min[1]
        ));
126
    }
L
lang 已提交
127

128
    contain(coord: number[]): boolean {
129 130
        const rect = this.getBoundingRect();
        const geometries = this.geometries;
S
sushuang 已提交
131 132 133
        if (!rect.contain(coord[0], coord[1])) {
            return false;
        }
134
        loopGeo: for (let i = 0, len = geometries.length; i < len; i++) {
S
sushuang 已提交
135 136 137
            // Only support polygon.
            if (geometries[i].type !== 'polygon') {
                continue;
L
lang 已提交
138
            }
139 140
            const exterior = geometries[i].exterior;
            const interiors = geometries[i].interiors;
S
sushuang 已提交
141
            if (polygonContain.contain(exterior, coord[0], coord[1])) {
S
sushuang 已提交
142
                // Not in the region if point is in the hole.
143
                for (let k = 0; k < (interiors ? interiors.length : 0); k++) {
144
                    if (polygonContain.contain(interiors[k], coord[0], coord[1])) {
S
sushuang 已提交
145
                        continue loopGeo;
L
lang 已提交
146
                    }
L
lang 已提交
147
                }
S
sushuang 已提交
148
                return true;
L
lang 已提交
149
            }
S
sushuang 已提交
150 151
        }
        return false;
152
    }
S
sushuang 已提交
153

154
    transformTo(x: number, y: number, width: number, height: number): void {
155
        let rect = this.getBoundingRect();
156
        const aspect = rect.width / rect.height;
S
sushuang 已提交
157 158 159 160
        if (!width) {
            width = aspect * height;
        }
        else if (!height) {
S
sushuang 已提交
161
            height = width / aspect;
S
sushuang 已提交
162
        }
163 164 165
        const target = new BoundingRect(x, y, width, height);
        const transform = rect.calculateTransform(target);
        const geometries = this.geometries;
166
        for (let i = 0; i < geometries.length; i++) {
S
sushuang 已提交
167 168 169
            // Only support polygon.
            if (geometries[i].type !== 'polygon') {
                continue;
L
lang 已提交
170
            }
171 172
            const exterior = geometries[i].exterior;
            const interiors = geometries[i].interiors;
1
100pah 已提交
173
            for (let p = 0; p < exterior.length; p++) {
S
sushuang 已提交
174
                vec2.applyTransform(exterior[p], exterior[p], transform);
L
lang 已提交
175
            }
176
            for (let h = 0; h < (interiors ? interiors.length : 0); h++) {
1
100pah 已提交
177
                for (let p = 0; p < interiors[h].length; p++) {
S
sushuang 已提交
178
                    vec2.applyTransform(interiors[h][p], interiors[h][p], transform);
L
lang 已提交
179 180
                }
            }
L
lang 已提交
181
        }
S
sushuang 已提交
182 183 184
        rect = this._rect;
        rect.copy(target);
        // Update center
1
100pah 已提交
185
        this._center = [
S
sushuang 已提交
186 187 188
            rect.x + rect.width / 2,
            rect.y + rect.height / 2
        ];
189
    }
S
sushuang 已提交
190

1
100pah 已提交
191
    cloneShallow(name: string): GeoJSONRegion {
S
sushuang 已提交
192
        name == null && (name = this.name);
1
100pah 已提交
193
        const newRegion = new GeoJSONRegion(name, this.geometries, this._center);
S
sushuang 已提交
194 195 196
        newRegion._rect = this._rect;
        newRegion.transformTo = null; // Simply avoid to be called.
        return newRegion;
S
sushuang 已提交
197
    }
L
lang 已提交
198

1
100pah 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    getCenter() {
        return this._center;
    }

    setCenter(center: number[]) {
        this._center = center;
    }

}

export class GeoSVGRegion extends Region {

    readonly type = 'geoSVG';

    private _center: number[];

    // Can only be used to calculate, but not be modified.
    // Becuase this el may not belongs to this view,
    // but been displaying on some other view.
    private _elOnlyForCalculate: Element;

    constructor(
        name: string,
        elOnlyForCalculate: Element
    ) {
        super(name);
        this._elOnlyForCalculate = elOnlyForCalculate;
    }

    getCenter() {
        let center = this._center;
        if (!center) {
            // In most cases there are no need to calculate this center.
            // So calculate only when called.
            center = this._center = this._calculateCenter();
        }
        return center;
    }

    private _calculateCenter(): number[] {
        const el = this._elOnlyForCalculate;
        const rect = el.getBoundingRect();
        const center = [
242 243
            rect.x + rect.width / 2,
            rect.y + rect.height / 2
1
100pah 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        ];

        const mat = matrix.identity(TMP_TRANSFORM);

        let target = el;
        while (target && !(target as GeoSVGGraphicRoot).isGeoSVGGraphicRoot) {
            matrix.mul(mat, target.getLocalTransform(), mat);
            target = target.parent;
        }

        matrix.invert(mat, mat);

        vec2.applyTransform(center, center, mat);

        return center;
    }

261 262
}