Skip to content

Commit

Permalink
chore: Cleanup of Source parameters (#3015)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed May 17, 2024
1 parent 6524f53 commit 2705434
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 37 deletions.
2 changes: 2 additions & 0 deletions modules/flatgeobuf/src/floatgeobuf-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const FlatGeobufSource = {
},

type: 'flatgeobuf-server',
fromUrl: true,
fromBlob: false, // TODO check if supported by library?

testURL: (url: string): boolean => url.toLowerCase().includes('FeatureServer'),
createDataSource: (url, props: FlatGeobufVectorSourceProps): FlatGeobufVectorSource =>
Expand Down
10 changes: 8 additions & 2 deletions modules/loader-utils/src/source-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {DataSource, DataSourceProps} from './lib/sources/data-source';
*/
export interface Source<
DataSourceT extends DataSource = DataSource,
DataSourcePropsT extends DataSourceProps = DataSourceProps
DataSourcePropsT extends DataSourceProps = any
> {
/** Type of source created by this service */
source?: DataSourceT;
Expand All @@ -30,10 +30,16 @@ export interface Source<
extensions: string[];
/** MIME extensions that this service uses */
mimeTypes: string[];
/** MIME extensions that this service uses */
/** Default options */
options: DataSourcePropsT;

/** Type string identifying this service, e.g. 'wms' */
type: string;
/** Can source be created from a URL */
fromUrl: boolean;
/** Can source be created from a Blob or File */
fromBlob: boolean;

/** Check if a URL can support this service */
testURL: (url: string) => boolean;
/** Test data */
Expand Down
12 changes: 6 additions & 6 deletions modules/mvt/src/mvt-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ export const MVTSource = {
extensions: ['mvt'],
mimeTypes: ['application/octet-stream'],
options: {
url: undefined!,
mvt: {
// TODO - add options here
}
},
type: 'mvt',
fromUrl: true,
fromBlob: false,

testURL: (url: string): boolean => true,
createDataSource(url: string, props: MVTTileSourceProps): MVTTileSource {
return new MVTTileSource({...props, url});
return new MVTTileSource(url, props);
}
} as const satisfies Source<MVTTileSource, MVTTileSourceProps>;

/** Properties for a Mapbox Vector Tile Source */
export type MVTTileSourceProps = DataSourceProps & {
/** Root url of tileset */
url: string;
mvt?: {
// TODO - add options here
/** if not supplied, loads tilejson.json, If null does not load metadata */
Expand Down Expand Up @@ -71,10 +71,10 @@ export class MVTTileSource extends DataSource implements ImageTileSource, Vector
extension: string;
mimeType: string | null = null;

constructor(props: MVTTileSourceProps) {
constructor(url: string, props: MVTTileSourceProps) {
super(props);
this.props = props;
this.url = resolvePath(props.url);
this.url = resolvePath(url);
this.metadataUrl = props.mvt?.metadataUrl || `${this.url}/tilejson.json`;
this.extension = props.mvt?.extension || '.png';
this.data = this.url;
Expand Down
10 changes: 6 additions & 4 deletions modules/pmtiles/src/pmtiles-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ export const PMTilesSource = {
mimeTypes: ['application/octet-stream'],
options: {url: undefined!, pmtiles: {}},
type: 'pmtiles',
fromUrl: true,
fromBlob: true,

testURL: (url: string) => url.endsWith('.pmtiles'),
createDataSource: (url: string | Blob, props: PMTilesTileSourceProps) =>
new PMTilesTileSource({...props, url})
new PMTilesTileSource(url, props)
} as const satisfies Source<PMTilesTileSource, PMTilesTileSourceProps>;

export type PMTilesTileSourceProps = DataSourceProps & {
Expand All @@ -60,11 +63,10 @@ export class PMTilesTileSource extends DataSource implements ImageTileSource, Ve
pmtiles: pmtiles.PMTiles;
metadata: Promise<PMTilesMetadata>;

constructor(props: PMTilesTileSourceProps) {
constructor(data: string | Blob, props: PMTilesTileSourceProps) {
super(props);
this.props = props;
const url =
typeof props.url === 'string' ? resolvePath(props.url) : new BlobSource(props.url, 'pmtiles');
const url = typeof data === 'string' ? resolvePath(data) : new BlobSource(data, 'pmtiles');
this.data = props.url;
this.pmtiles = new PMTiles(url);
this.getTileData = this.getTileData.bind(this);
Expand Down
10 changes: 7 additions & 3 deletions modules/wms/src/lib/deprecated/create-image-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ type CreateImageSourceProps = ImageSourceProps &
*
* @deprecated Use createDataSource from @loaders.gl/core
*/
export function createImageSource(props: CreateImageSourceProps, sources = SOURCES): ImageSource {
export function createImageSource(
url: string,
props: CreateImageSourceProps,
sources = SOURCES
): ImageSource {
const {type = 'auto'} = props;
const source: Source | null =
type === 'auto' ? guessSourceType(props.url, sources) : getSourceOfType(type, sources);
type === 'auto' ? guessSourceType(url, sources) : getSourceOfType(type, sources);

if (!source) {
throw new Error('Not a valid image source type');
}
return source.createDataSource(props.url, props) as ImageSource;
return source.createDataSource(url, props) as ImageSource;
}

/** Guess service type from URL */
Expand Down
2 changes: 2 additions & 0 deletions modules/wms/src/services/arcgis/arcgis-feature-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export const ArcGISFeatureServerSource = {
},

type: 'arcgis-feature-server',
fromUrl: true,
fromBlob: false,

testURL: (url: string): boolean => url.toLowerCase().includes('FeatureServer'),
createDataSource: (url, props: ArcGISVectorSourceProps): ArcGISVectorSource =>
Expand Down
16 changes: 9 additions & 7 deletions modules/wms/src/services/arcgis/arcgis-image-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ export const ArcGISImageServerSource = {
extensions: [],
mimeTypes: [],
options: {
url: undefined!,
'arcgis-image-server': {
// TODO - add options here
}
},

type: 'arcgis-image-server',
fromUrl: true,
fromBlob: false,

testURL: (url: string): boolean => url.toLowerCase().includes('ImageServer'),
createDataSource: (url, props: ArcGISImageSourceProps): ArcGISImageSource =>
new ArcGISImageSource(props)
new ArcGISImageSource(url as string, props)
} as const satisfies Source<ArcGISImageSource, ArcGISImageSourceProps>;

export type ArcGISImageSourceProps = ImageSourceProps & {
url: string;
'arcgis-image-server'?: {
// TODO - add options here
};
Expand All @@ -48,11 +48,13 @@ export type ArcGISImageSourceProps = ImageSourceProps & {
* @see https://developers.arcgis.com/rest/services-reference/enterprise/image-service.htm
*/
export class ArcGISImageSource extends ImageSource<ArcGISImageSourceProps> {
url: string;
data: string;

constructor(props: ArcGISImageSourceProps) {
constructor(url: string, props: ArcGISImageSourceProps) {
super(props);
this.data = props.url;
this.url = url;
this.data = url;
}

// ImageSource (normalized endpoints)
Expand Down Expand Up @@ -106,7 +108,7 @@ export class ArcGISImageSource extends ImageSource<ArcGISImageSourceProps> {
// URL creators

metadataURL(options: {parameters?: Record<string, unknown>}): string {
return `${this.props.url}?f=pjson`;
return `${this.url}?f=pjson`;
}

/**
Expand Down Expand Up @@ -158,7 +160,7 @@ export class ArcGISImageSource extends ImageSource<ArcGISImageSourceProps> {
options: Record<string, unknown>,
extra?: Record<string, unknown>
): string {
let url = `${this.props.url}/${path}`;
let url = `${this.url}/${path}`;
let first = true;
for (const [key, value] of Object.entries(options)) {
url += first ? '?' : '&';
Expand Down
2 changes: 2 additions & 0 deletions modules/wms/src/services/ogc/wfs-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const WFSSource = {
},

type: 'wfs',
fromUrl: true,
fromBlob: false,

testURL: (url: string): boolean => url.toLowerCase().includes('wfs'),
createDataSource: (url, props: WFSVectorSourceProps): WFSVectorSource =>
Expand Down
37 changes: 22 additions & 15 deletions modules/wms/src/services/ogc/wms-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,35 @@ export const WMSSource = {
extensions: [],
mimeTypes: [],
options: {
url: undefined!,
wms: {
// TODO - add options here
}
},
type: 'wms',
fromUrl: true,
fromBlob: false,

testURL: (url: string): boolean => url.toLowerCase().includes('wms'),
createDataSource: (url, props: WMSImageSourceProps) =>
new WMSImageSource({...props, url: url as string})
createDataSource: (url, props: WMSImageSourceProps) => new WMSImageSource(url as string, props)
} as const satisfies Source<WMSImageSource, WMSImageSourceProps>;

/** Properties for creating a enw WMS service */
export type WMSImageSourceProps = ImageSourceProps & {
/** Base URL to the service */
url: string;
/** In 1.3.0, replaces references to EPSG:4326 with CRS:84 */
/** @deprecated Use props.wms.substituteCRS84 */
substituteCRS84?: boolean;
/** Default WMS parameters. If not provided here, must be provided in the various request */
/** @deprecated Use props.wms.wmsParameters */
wmsParameters?: WMSParameters;
/** Any additional service specific parameters */
/** @deprecated Use props.wms.vendorParameters */
vendorParameters?: Record<string, unknown>;
wms?: {};
wms?: {
// TODO - move parameters inside WMS scope
/** In 1.3.0, replaces references to EPSG:4326 with CRS:84 */
substituteCRS84?: boolean;
/** Default WMS parameters. If not provided here, must be provided in the various request */
wmsParameters?: WMSParameters;
/** Any additional service specific parameters */
vendorParameters?: Record<string, unknown>;
};
};

// PARAMETER TYPES FOR WMS SOURCE
Expand Down Expand Up @@ -215,17 +221,17 @@ export class WMSImageSource extends ImageSource<WMSImageSourceProps> {
capabilities: WMSCapabilities | null = null;

/** Create a WMSImageSource */
constructor(props: WMSImageSourceProps) {
constructor(url: string, props: WMSImageSourceProps) {
super(props);

// TODO - defaults such as version, layers etc could be extracted from a base URL with parameters
// This would make pasting in any WMS URL more likely to make this class just work.
// const {baseUrl, parameters} = this._parseWMSUrl(props.url);

this.url = props.url;
this.data = props.url;
this.url = url;
this.data = url;

this.substituteCRS84 = props.substituteCRS84 ?? false;
this.substituteCRS84 = props.wms?.substituteCRS84 ?? props.substituteCRS84 ?? false;
this.flipCRS = ['EPSG:4326'];

this.wmsParameters = {
Expand All @@ -239,10 +245,11 @@ export class WMSImageSource extends ImageSource<WMSImageSourceProps> {
transparent: undefined!,
time: undefined!,
elevation: undefined!,
...props.wmsParameters
...props.wmsParameters, // deprecated
...props.wms?.wmsParameters
};

this.vendorParameters = props.vendorParameters || {};
this.vendorParameters = props.wms?.vendorParameters || props.vendorParameters || {};
}

// ImageSource implementation
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,7 @@ __metadata:
dependencies:
"@loaders.gl/schema": "npm:4.3.0-alpha.2"
"@loaders.gl/worker-utils": "npm:4.3.0-alpha.2"
"@probe.gl/log": "npm:^4.0.2"
"@probe.gl/stats": "npm:^4.0.2"
peerDependencies:
"@loaders.gl/core": ^4.0.0
Expand Down

0 comments on commit 2705434

Please sign in to comment.