Skip to content

Commit

Permalink
fix Geometry miss rotate info when toJSON (#2378)
Browse files Browse the repository at this point in the history
* record Geometry rotate info for toJSON

* spec

* fixing

* updates

* updates

* update spec
  • Loading branch information
deyihu authored Aug 8, 2024
1 parent 53c72cc commit 0de4575
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
20 changes: 19 additions & 1 deletion src/geometry/Geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
public _projCode: string
public _painter: Painter
public _maskPainter: CollectionPainter | Painter
public _dirtyCoords: any
public _dirtyCoords: boolean;
public _pcenter: Coordinate
public _coordinates: any;
public _infoWinOptions: InfoWindowOptionsType;
Expand All @@ -132,6 +132,7 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
public _paintAsPath?: () => any;
public _getPaintParams?: (disableSimplify?: boolean) => any[];
public _simplified?: boolean;
private _dirtyRotate?: boolean;
// 本身应该存于 Path 类,但是由于渲染层需要大量的特殊熟悉判断,定义在这里回减少很多麻烦
public getHoles?(): Array<Array<Coordinate>>;
__connectors: Array<Geometry>;
Expand Down Expand Up @@ -169,6 +170,10 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
if (!isNil(id)) {
this.setId(id);
}
//record rotate
if (options && isNumber(options.rotateAngle)) {
this._dirtyRotate = true;
}
}

static fromJSON(json: { [key: string]: any } | Array<{ [key: string]: any }>): Geometry | Array<Geometry> {
Expand Down Expand Up @@ -1136,6 +1141,9 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
console.error(`angle:${angle} is not number`);
return this;
}
if (!this._painter) {
this._dirtyRotate = true;
}
if (this.type === 'GeometryCollection') {
const geometries = this.getGeometries();
geometries.forEach(g => g.rotate(angle, pivot));
Expand All @@ -1159,6 +1167,10 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
//only redraw ,not to change coordinate
this.onPositionChanged();
}
if (this.getShell) {
this.options.rotateAngle = angle;
this.options.rotatePivot = pivot.toArray();
}
return this;
}
forEachCoord(coordinates, c => {
Expand Down Expand Up @@ -1467,6 +1479,10 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
this._clearCache();
}
}
if (this._dirtyRotate && isNumber(this.options.rotateAngle)) {
this.rotate(this.options.rotateAngle, this.options.rotatePivot as unknown as Coordinate);
}
this._dirtyRotate = false;
this._painter.paint(extent);
}
}
Expand Down Expand Up @@ -1848,6 +1864,8 @@ export type GeometryOptionsType = {
zIndex?: number;
symbol?: any;
properties?: { [key: string]: any };
rotateAngle?: number;
rotatePivot?: Array<number>;

}

Expand Down
62 changes: 44 additions & 18 deletions test/geometry/GeometrySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe('Geometry.main', function () {
var center = new maptalks.Coordinate(118.846825, 32.046534);
var layer;
var context = {
map:map,
layer:layer
map: map,
layer: layer
};

beforeEach(function () {
Expand All @@ -27,20 +27,20 @@ describe('Geometry.main', function () {
it('constructor options', function () {
it('some common options', function () {
var symbol = {
markerFile : 'file',
markerWidth : 10,
markerHeight : 15
markerFile: 'file',
markerWidth: 10,
markerHeight: 15
};
var properties = {
foo1 : 1,
foo2 : 'test',
foo3 : true
foo1: 1,
foo2: 'test',
foo3: true
};
var id = '1';
var marker = new maptalks.Marker([0, 0], {
id : id,
symbol : symbol,
properties : properties
id: id,
symbol: symbol,
properties: properties
});

expect(marker.getProperties()).to.be.eql(properties);
Expand All @@ -64,7 +64,7 @@ describe('Geometry.main', function () {
registerGeometryCommonTest.call(this, geometries[i], context);
}

it('translate LineString', function (done) {
it('translate LineString', function (done) {
var line = new maptalks.LineString([
{ x: 121.111, y: 30.111 },
{ x: 121.222, y: 30.222 }
Expand All @@ -75,6 +75,32 @@ describe('Geometry.main', function () {
line.translate(-1, -1);
});

it('#1625 toJSON has rotate info', function (done) {
//rect ellipse etc
var geometries = GEN_GEOMETRIES_OF_ALL_TYPES().slice(0, 7).filter(g => {
return !!g.getShell;
});
let idx = 0;
const test = () => {
if (idx === geometries.length) {
done();
return;
}
const geo = geometries[idx];
geo.addTo(layer);
const angle = Math.round(Math.random() * 180);
geo.rotate(angle);

setTimeout(() => {
const json = geo.toJSON();
expect(json.options.rotateAngle).to.eql(angle);
idx++;
test();
}, 200);
}
test();
});

});
//测试Geometry的公共方法
function registerGeometryCommonTest(geometry, _context) {
Expand Down Expand Up @@ -130,7 +156,7 @@ function registerGeometryCommonTest(geometry, _context) {
it('Properties', function () {
var oldProps = geometry.getProperties();

var props = { 'foo_num':1, 'foo_str':'str', 'foo_bool':false };
var props = { 'foo_num': 1, 'foo_str': 'str', 'foo_bool': false };
geometry.setProperties(props);

props = geometry.getProperties();
Expand Down Expand Up @@ -266,7 +292,7 @@ function registerGeometryCommonTest(geometry, _context) {
geometry.remove();
expect(geometry.getLayer()).to.not.be.ok();

var canvasLayer = new maptalks.VectorLayer('event_test_canvas', { 'render':'canvas' });
var canvasLayer = new maptalks.VectorLayer('event_test_canvas', { 'render': 'canvas' });
canvasLayer.addGeometry(geometry);
_context.map.addLayer(canvasLayer);

Expand All @@ -287,7 +313,7 @@ function registerGeometryCommonTest(geometry, _context) {
expect(painter).to.be.ok();
geometry.remove();

var canvasLayer = new maptalks.VectorLayer('event_test_canvas', { 'render':'canvas' });
var canvasLayer = new maptalks.VectorLayer('event_test_canvas', { 'render': 'canvas' });
canvasLayer.addGeometry(geometry);
_context.map.addLayer(canvasLayer);

Expand All @@ -305,16 +331,16 @@ function registerGeometryCommonTest(geometry, _context) {
}
if (type === 'Point') {
symbol = {
'markerFile':'http://foo.com/foo.png'
'markerFile': 'http://foo.com/foo.png'
};
geometry.setSymbol(symbol);
resource = geometry._getExternalResources();
expect(resource).to.have.length(1);
expect(resource[0][0]).to.be(symbol['markerFile']);
} else {
symbol = {
'polygonPatternFile':'url(\'http://foo.com/foo.png\')',
'linePatternFile':'url(\'http://foo.com/foo2.png\')',
'polygonPatternFile': 'url(\'http://foo.com/foo.png\')',
'linePatternFile': 'url(\'http://foo.com/foo2.png\')',
};
geometry.setSymbol(symbol);
resource = geometry._getExternalResources();
Expand Down

0 comments on commit 0de4575

Please sign in to comment.