Skip to content

Commit 9485ca2

Browse files
authored
New Components - mapbox (#15539)
* mapbox init * new components * update descriptions * pnpm-lock.yaml * remove debug * updates per code review * update coordinate examples
1 parent ec4432c commit 9485ca2

File tree

9 files changed

+316
-22
lines changed

9 files changed

+316
-22
lines changed

components/mapbox/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import mapbox from "../../mapbox.app.mjs";
2+
import fs from "fs";
3+
import FormData from "form-data";
4+
5+
export default {
6+
key: "mapbox-create-tileset",
7+
name: "Create Tileset",
8+
description: "Uploads and creates a new tileset from a data source. [See the documentation](https://docs.mapbox.com/api/maps/mapbox-tiling-service/)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
mapbox,
13+
username: {
14+
type: "string",
15+
label: "Username",
16+
description: "The Mapbox username of the account for which to create a tileset source",
17+
},
18+
tilesetName: {
19+
type: "string",
20+
label: "Tileset Name",
21+
description: "A unique name for the tileset source to be created. Limited to 32 characters. The only allowed special characters are `-` (hyphen) and `_` (underscore)",
22+
},
23+
filePath: {
24+
type: "string",
25+
label: "File Path",
26+
description: "The path to a tileset source file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
27+
},
28+
recipe: {
29+
type: "object",
30+
label: "Recipe",
31+
description: "A recipe that describes how the GeoJSON data you uploaded should be transformed into tiles. A tileset source is raw geographic data formatted as [line-delimited GeoJSON](https://en.wikipedia.org/wiki/JSON_streaming#Line-delimited_JSON), or a supported [raster file format](https://docs.mapbox.com/mapbox-tiling-service/raster/supported-file-formats/). For more information on how to create and format recipes, see the [Recipe reference](https://docs.mapbox.com/mapbox-tiling-service/reference/) and [Recipe examples](https://docs.mapbox.com/mapbox-tiling-service/examples/).",
32+
},
33+
description: {
34+
type: "string",
35+
label: "Description",
36+
description: "A description of the tileset",
37+
optional: true,
38+
},
39+
private: {
40+
type: "boolean",
41+
label: "Private",
42+
description: "Describes whether the tileset must be used with an access token from your Mapbox account. Default is `true`.",
43+
optional: true,
44+
},
45+
},
46+
async run({ $ }) {
47+
const filePath = this.filePath.includes("tmp/")
48+
? this.filePath
49+
: `/tmp/${this.filePath}`;
50+
51+
// Create Tileset Source
52+
try {
53+
const fileData = new FormData();
54+
const content = fs.createReadStream(filePath);
55+
fileData.append("file", content);
56+
57+
await this.mapbox.createTilesetSource({
58+
$,
59+
username: this.username,
60+
id: this.tilesetName,
61+
data: fileData,
62+
headers: fileData.getHeaders(),
63+
});
64+
} catch (e) {
65+
throw new Error(`Error uploading file: \`${filePath}\`. Error: ${e}`);
66+
}
67+
68+
const recipe = typeof this.recipe === "string"
69+
? JSON.parse(this.recipe)
70+
: this.recipe;
71+
72+
// Validate Recipe
73+
const {
74+
valid, errors,
75+
} = await this.mapbox.validateRecipe({
76+
$,
77+
data: recipe,
78+
});
79+
80+
if (!valid) {
81+
throw new Error(`Error validating recipe: ${errors}`);
82+
}
83+
84+
// Create Tileset
85+
const response = await this.mapbox.createTileset({
86+
$,
87+
tilesetId: `${this.username}.${this.tilesetName}`,
88+
data: {
89+
recipe,
90+
name: this.tilesetName,
91+
description: this.description,
92+
private: this.private,
93+
},
94+
});
95+
96+
$.export("$summary", `Created tileset ${this.tilesetName}`);
97+
return response;
98+
},
99+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import mapbox from "../../mapbox.app.mjs";
2+
3+
export default {
4+
key: "mapbox-generate-directions",
5+
name: "Generate Directions",
6+
description: "Generates directions between two or more locations using Mapbox API. [See the documentation](https://docs.mapbox.com/api/navigation/directions/).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
mapbox,
11+
startCoordinate: {
12+
type: "string",
13+
label: "Start Coordinate",
14+
description: "The starting point in the format `longitude,latitude`, E.g. `-85.244869,37.835819`",
15+
},
16+
endCoordinate: {
17+
type: "string",
18+
label: "End Coordinate",
19+
description: "The ending point in the format `longitude,latitude`, E.g. `-85.244869,37.835819`",
20+
},
21+
transportationMode: {
22+
propDefinition: [
23+
mapbox,
24+
"transportationMode",
25+
],
26+
},
27+
steps: {
28+
type: "boolean",
29+
label: "Steps",
30+
description: "Whether to return steps and turn-by-turn instructions (`true`) or not (`false`, default)",
31+
optional: true,
32+
},
33+
alternatives: {
34+
type: "boolean",
35+
label: "Alternatives",
36+
description: "Whether to try to return alternative routes (`true`) or not (`false`, default). An alternative route is a route that is significantly different from the fastest route, but still close in time.",
37+
optional: true,
38+
},
39+
exclude: {
40+
propDefinition: [
41+
mapbox,
42+
"exclude",
43+
],
44+
},
45+
},
46+
async run({ $ }) {
47+
const directions = await this.mapbox.getDirections({
48+
$,
49+
transportationMode: this.transportationMode,
50+
coordinates: `${this.startCoordinate};${this.endCoordinate}`,
51+
params: {
52+
steps: this.steps,
53+
alternatives: this.alternatives,
54+
exclude: this.exclude && this.exclude.join(","),
55+
},
56+
});
57+
$.export("$summary", "Generated directions successfully.");
58+
return directions;
59+
},
60+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import mapbox from "../../mapbox.app.mjs";
2+
3+
export default {
4+
key: "mapbox-geocode-address",
5+
name: "Geocode Address",
6+
description: "Retrieves the geocoded location for a given address. [See the documentation](https://docs.mapbox.com/api/search/geocoding/)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
mapbox,
11+
address: {
12+
type: "string",
13+
label: "Address",
14+
description: "The address (or partial address) to geocode. This could be an address, a city name, etc. Must consist of at most 20 words and numbers separated by spacing and punctuation, and at most 256 characters.",
15+
},
16+
boundingBox: {
17+
type: "string",
18+
label: "Bounding Box",
19+
description: "Limit results to only those contained within the supplied bounding box. Bounding boxes should be supplied as four numbers separated by commas, in `minLon,minLat,maxLon,maxLat` order. The bounding box cannot cross the 180th meridian. You can use the [Location Helper](https://labs.mapbox.com/location-helper) to find a bounding box for use with this API.",
20+
optional: true,
21+
},
22+
proximity: {
23+
type: "string",
24+
label: "Proximity",
25+
description: "Bias the response to favor results that are closer to this location. Provided as two comma-separated coordinates in `longitude,latitude` order.",
26+
optional: true,
27+
},
28+
},
29+
async run({ $ }) {
30+
const response = await this.mapbox.geocode({
31+
$,
32+
params: {
33+
q: this.address,
34+
bBox: this.boundingBox,
35+
proximity: this.proximity,
36+
},
37+
});
38+
$.export("$summary", `Geocoded location for "${this.address}" retrieved successfully`);
39+
return response;
40+
},
41+
};

components/mapbox/app/mapbox.app.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const TRANSPORTATION_MODES = [
2+
"driving",
3+
"walking",
4+
"cycling",
5+
"driving-traffic",
6+
];
7+
8+
const EXCLUDE_OPTIONS = [
9+
"motorway",
10+
"toll",
11+
"ferry",
12+
"unpaved",
13+
"cash_only_tolls",
14+
"country_border",
15+
"state_border",
16+
];
17+
18+
export default {
19+
TRANSPORTATION_MODES,
20+
EXCLUDE_OPTIONS,
21+
};

components/mapbox/mapbox.app.mjs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { axios } from "@pipedream/platform";
2+
import constants from "./common/constants.mjs";
3+
4+
export default {
5+
type: "app",
6+
app: "mapbox",
7+
propDefinitions: {
8+
transportationMode: {
9+
type: "string",
10+
label: "Transportation Mode",
11+
description: "The mode of transportation",
12+
options: constants.TRANSPORTATION_MODES,
13+
},
14+
exclude: {
15+
type: "string[]",
16+
label: "Exclude",
17+
description: "Exclude certain road types and custom locations from routing",
18+
options: constants.EXCLUDE_OPTIONS,
19+
optional: true,
20+
},
21+
},
22+
methods: {
23+
_baseUrl() {
24+
return "https://api.mapbox.com";
25+
},
26+
_makeRequest({
27+
$ = this,
28+
path,
29+
params,
30+
...otherOpts
31+
}) {
32+
return axios($, {
33+
url: `${this._baseUrl()}${path}`,
34+
params: {
35+
access_token: this.$auth.access_token,
36+
...params,
37+
},
38+
...otherOpts,
39+
});
40+
},
41+
geocode(opts = {}) {
42+
return this._makeRequest({
43+
path: "/search/geocode/v6/forward",
44+
...opts,
45+
});
46+
},
47+
getDirections({
48+
transportationMode, coordinates, ...opts
49+
}) {
50+
return this._makeRequest({
51+
path: `/directions/v5/mapbox/${transportationMode}/${coordinates}`,
52+
...opts,
53+
});
54+
},
55+
createTilesetSource({
56+
username, id, ...opts
57+
}) {
58+
return this._makeRequest({
59+
method: "POST",
60+
path: `/tilesets/v1/sources/${username}/${id}`,
61+
...opts,
62+
});
63+
},
64+
validateRecipe(opts = {}) {
65+
return this._makeRequest({
66+
method: "PUT",
67+
path: "/tilesets/v1/validateRecipe",
68+
...opts,
69+
});
70+
},
71+
createTileset({
72+
tilesetId, ...opts
73+
}) {
74+
return this._makeRequest({
75+
method: "POST",
76+
path: `/tilesets/v1/${tilesetId}`,
77+
...opts,
78+
});
79+
},
80+
},
81+
};

components/mapbox/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
22
"name": "@pipedream/mapbox",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Pipedream Mapbox Components",
5-
"main": "dist/app/mapbox.app.mjs",
5+
"main": "mapbox.app.mjs",
66
"keywords": [
77
"pipedream",
88
"mapbox"
99
],
10-
"files": [
11-
"dist"
12-
],
1310
"homepage": "https://pipedream.com/apps/mapbox",
1411
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1512
"publishConfig": {
1613
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"form-data": "^4.0.1"
1718
}
1819
}

pnpm-lock.yaml

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)