FarmMapsLib/projects/common-map/src/fm-map/components/feature-thumbnail/feature-thumbnail.component.ts

96 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-01-19 12:21:44 +00:00
import { Component, Input, AfterViewInit, ViewChild } from '@angular/core';
2021-03-20 11:00:04 +00:00
import { Feature} from 'ol';
2021-10-05 14:13:25 +00:00
import { Geometry,Polygon,MultiPolygon } from 'ol/geom';
2021-03-20 11:00:04 +00:00
import * as extent from 'ol/extent';
import * as render from 'ol/render';
import * as style from 'ol/style';
@Component({
selector: 'fm-map-feature-thumbnail',
templateUrl: './feature-thumbnail.component.html',
styleUrls: ['./feature-thumbnail.component.scss']
})
2022-01-19 12:21:44 +00:00
export class GeometryThumbnailComponent implements AfterViewInit {
2021-03-20 11:00:04 +00:00
constructor() { }
@ViewChild('canvas') canvas;
@ViewChild('container') container;
2022-01-19 12:21:44 +00:00
private geometry:Geometry = null;
@Input() set feature(value:Feature<Geometry>) {
2022-01-19 12:21:44 +00:00
if(value) {
this.geometry = value.getGeometry();
} else {
this.geometry = null;
}
this.render(this.canvas,
this.geometryStyle,
this.geometry,
this.width,
this.height);
2023-03-06 13:04:14 +00:00
}
2022-01-19 12:21:44 +00:00
private defaultStyle:style.Style = new style.Style({
stroke: new style.Stroke({ color: 'black',width:1.5 })
});
private geometryStyle:style.Style = this.defaultStyle;
@Input() set fillColor(value:string) {
if(style) {
this.geometryStyle = new style.Style({
stroke: new style.Stroke({ color: 'black',width:1.5 }),
fill: new style.Fill({color: value})
});
} else {
this.geometryStyle = this.defaultStyle
}
this.render(this.canvas,
this.geometryStyle,
this.geometry,
this.width,
this.height);
2021-03-20 11:00:04 +00:00
}
2023-03-06 13:04:14 +00:00
private width = 0;
private height = 0;
2022-01-19 12:21:44 +00:00
render(canvas,style:style.Style,geometry:Geometry,width:number,height:number) {
if(canvas && canvas.nativeElement && geometry && style) {
2023-03-06 13:04:14 +00:00
const renderContext = render.toContext(canvas.nativeElement.getContext( '2d'),{ size: [width, height] });
2022-01-19 12:21:44 +00:00
2023-03-06 13:04:14 +00:00
const geom = geometry.clone() as Polygon,
2022-01-19 12:21:44 +00:00
line = geom.getCoordinates()[0],
e = extent.boundingExtent( line );
2023-03-06 13:04:14 +00:00
const dxy = extent.getCenter(e),
2022-01-19 12:21:44 +00:00
sxy = [
(width - 2 ) / extent.getWidth(e),
(height - 2 ) / extent.getHeight(e)
];
2023-03-06 13:04:14 +00:00
const dx = dxy[0],
2022-01-19 12:21:44 +00:00
dy = dxy[1],
sx = sxy[0],
sy = sxy[1];
geom.translate( -dx, -dy );
geom.scale( Math.min(sx, sy), -Math.min(sx, sy));
geom.translate(width / 2,height / 2 );
renderContext.setStyle( style );
renderContext.drawGeometry( geom );
}
2021-03-20 11:00:04 +00:00
}
ngAfterViewInit() {
2022-01-19 12:21:44 +00:00
this.width = this.container.nativeElement.offsetWidth;
this.height = this.container.nativeElement.offsetHeight;
this.render(this.canvas,
this.geometryStyle,
this.geometry,
this.width,
this.height);
2021-03-20 11:00:04 +00:00
}
2022-01-19 12:21:44 +00:00
2021-03-20 11:00:04 +00:00
}