Skip to content

Commit

Permalink
feat(vector): more formats available
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Jun 9, 2021
1 parent ce0ea20 commit 7247a0d
Showing 1 changed file with 63 additions and 26 deletions.
89 changes: 63 additions & 26 deletions packages/geo/src/lib/layer/shared/layers/vector-layer.ts
Expand Up @@ -4,6 +4,7 @@ import { unByKey } from 'ol/Observable';
import { easeOut } from 'ol/easing';
import { asArray as ColorAsArray } from 'ol/color';
import { getVectorContext } from 'ol/render';
import FormatType from 'ol/format/FormatType';

import { FeatureDataSource } from '../../../datasource/shared/datasources/feature-datasource';
import { WFSDataSource } from '../../../datasource/shared/datasources/wfs-datasource';
Expand Down Expand Up @@ -65,22 +66,27 @@ export class VectorLayer extends Layer {
}

const vector = new olLayerVector(olOptions);
const vectorSource = (
(this.dataSource instanceof ClusterDataSource) ? vector.getSource().getSource() : vector.getSource()
) as olSourceVector;
const vectorSource = (this.dataSource instanceof ClusterDataSource
? vector.getSource().getSource()
: vector.getSource()) as olSourceVector;
const url = vectorSource.getUrl();
vectorSource.addEventListener('vectorloading');
vectorSource.addEventListener('vectorloaded');
vectorSource.addEventListener('vectorloaderror');
if (url) {
const loader = (extent, resolution, proj) => {
this.customLoader(vectorSource, url, this.authInterceptor, extent, resolution, proj);
this.customLoader(
vectorSource,
url,
this.authInterceptor,
extent,
resolution,
proj
);
};
if (loader) {
vectorSource.setLoader(loader);
}


}
return vector;
}
Expand All @@ -101,7 +107,7 @@ export class VectorLayer extends Layer {
let style = this.ol
.getStyleFunction()
.call(this, feature)
.find(style2 => {
.find((style2) => {
return style2.getImage();
});
if (!style) {
Expand All @@ -119,20 +125,13 @@ export class VectorLayer extends Layer {
case 'LineString':
// TODO
if (styleClone.getImage()) {
styleClone
.getImage()
.getStroke()
.setColor(newColor);
styleClone.getImage().getStroke().setColor(newColor);
styleClone
.getImage()
.getStroke()
.setWidth(
easeOut(elapsedRatio) *
(styleClone
.getImage()
.getStroke()
.getWidth() *
3)
(styleClone.getImage().getStroke().getWidth() * 3)
);
}
if (styleClone.getStroke()) {
Expand All @@ -147,10 +146,7 @@ export class VectorLayer extends Layer {
case 'Polygon':
// TODO
if (styleClone.getImage()) {
styleClone
.getImage()
.getFill()
.setColor(newColor);
styleClone.getImage().getFill().setColor(newColor);
}
if (styleClone.getFill()) {
styleClone.getFill().setColor(newColor);
Expand Down Expand Up @@ -232,10 +228,26 @@ export class VectorLayer extends Layer {
* @param resolution the current resolution
* @param projection the projection to retrieve the data
*/
private customLoader(vectorSource, url, interceptor, extent, resolution, projection) {
private customLoader(
vectorSource,
url,
interceptor,
extent,
resolution,
projection
) {
vectorSource.dispatchEvent({ type: 'vectorloading' });
const xhr = new XMLHttpRequest();
xhr.open('GET', typeof url === 'function' ? url = url(extent, resolution, projection) : url);
xhr.open(
'GET',
typeof url === 'function'
? (url = url(extent, resolution, projection))
: url
);
const format = vectorSource.getFormat();
if (format.getType() === FormatType.ARRAY_BUFFER) {
xhr.responseType = 'arraybuffer';
}
if (interceptor) {
interceptor.interceptXhr(xhr, url);
}
Expand All @@ -246,10 +258,35 @@ export class VectorLayer extends Layer {
};
xhr.onerror = onError;
xhr.onload = () => {
if (xhr.status === 200) {
vectorSource.addFeatures(
vectorSource.getFormat().readFeatures(xhr.responseText));
vectorSource.dispatchEvent({ type: 'vectorloaded' });
// status will be 0 for file:// urls
if (!xhr.status || (xhr.status >= 200 && xhr.status < 300)) {
const type = format.getType();
let source;
if (type === FormatType.JSON || type === FormatType.TEXT) {
source = xhr.responseText;
} else if (type === FormatType.XML) {
source = xhr.responseXML;
if (!source) {
source = new DOMParser().parseFromString(
xhr.responseText,
'application/xml'
);
}
} else if (type === FormatType.ARRAY_BUFFER) {
source = xhr.response;
}
if (source) {
vectorSource.addFeatures(
format.readFeatures(source, {
extent,
featureProjection: projection
}),
format.readProjection(source)
);
vectorSource.dispatchEvent({ type: 'vectorloaded' });
} else {
onError();
}
} else {
onError();
}
Expand Down

0 comments on commit 7247a0d

Please sign in to comment.