Skip to content

Commit

Permalink
feat(utils): Extent buffer
Browse files Browse the repository at this point in the history
Register equidistant worldwide projection, EPSG:4087
  • Loading branch information
FilipLeitner authored and jmacura committed Jul 29, 2024
1 parent 419862e commit 0f44b5c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
6 changes: 5 additions & 1 deletion projects/hslayers/services/add-data/url/wms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,11 @@ export class HsUrlWmsService implements HsUrlTypeServiceModel {
removable: true,
abstract: layer.Abstract,
metadata,
extent: this.getLayerExtent(layer, options.crs),
extent: this.HsLayerUtilsService.bufferExtent(
//Full world in case of layers added via URL panel.
this.getLayerExtent(layer, options.crs),
this.hsMapService.getCurrentProj(),
),
path: options.path,
dimensions: dimensions,
legends: legends,
Expand Down
10 changes: 10 additions & 0 deletions projects/hslayers/services/map/map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,16 @@ export class HsMapService {
'http://www.opengis.net/gml/srs/epsg.xml#3031',
proj4.defs('EPSG:3031'),
);

proj4.defs(
'EPSG:4087',
'+proj=cea +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs',
);
proj4.defs(
'http://www.opengis.net/gml/srs/epsg.xml#4087',
proj4.defs('EPSG:4087'),
);

register(proj4);
if (this.hsConfig.componentsEnabled?.mapControls == false) {
this.removeAllControls();
Expand Down
45 changes: 43 additions & 2 deletions projects/hslayers/services/utils/layer-utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
WMTS,
XYZ,
} from 'ol/source';
import {Extent, isEmpty} from 'ol/extent';
import {Feature, View} from 'ol';
import {default as FeatureFormat} from 'ol/format/Feature';
import {Geometry} from 'ol/geom';
import {Image as ImageLayer, Layer, Vector as VectorLayer} from 'ol/layer';
import {METERS_PER_UNIT} from 'ol/proj';
import {METERS_PER_UNIT, Projection, transform, transformExtent} from 'ol/proj';
import {Tile as TileLayer} from 'ol/layer';
import {isEmpty} from 'ol/extent';

import {HsLayerDescriptor} from 'hslayers-ng/types';
import {HsUtilsService} from './utils.service';
Expand Down Expand Up @@ -498,4 +498,45 @@ export class HsLayerUtilsService {
)
: [];
}

/**
* Buffer extent by `BUFFER_FACTOR`
* NOTE: Not using OL because we want to extend witdh and height independently
*/
bufferExtent(extent: Extent, currentMapProj: Projection) {
const BUFFER_FACTOR = 0.1;

const inMeters = currentMapProj.getUnits() === 'm';
const bounds = transform([180, 90], 'EPSG:4326', currentMapProj);

//Transform into projection suitable for area manipulation* if necessary
const transformed = inMeters
? extent
: transformExtent(extent, currentMapProj, 'EPSG:4087');

//Calculate buffer values
const extentWidth = Math.abs(extent[2] - extent[0]);
const extentHeight = Math.abs(extent[3] - extent[1]);
const bufferWidth = extentWidth * BUFFER_FACTOR;
const bufferHeight = extentHeight * BUFFER_FACTOR;

//Buffer extent and transform back to currentMapProj
const extended = transformExtent(
[
transformed[0] - bufferWidth,
transformed[1] - bufferHeight,
transformed[2] + bufferWidth,
transformed[3] + bufferHeight,
],
inMeters ? currentMapProj : 'EPSG:4087',
currentMapProj,
);
return [
// Apply safeguards to ensure the extent does not go out of bounds
Math.max(extended[0], bounds[1] * -1),
Math.max(extended[1], bounds[0] * -1),
Math.min(extended[2], bounds[1]),
Math.min(extended[3], bounds[0]),
];
}
}

0 comments on commit 0f44b5c

Please sign in to comment.