Skip to content

Commit 79c60e1

Browse files
committed
mapbox init
1 parent 1eb4668 commit 79c60e1

File tree

4 files changed

+350
-0
lines changed

4 files changed

+350
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import mapbox from "../../mapbox.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "mapbox-create-tileset",
6+
name: "Create Tileset",
7+
description: "Uploads and creates a new tileset from a data source. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
mapbox,
12+
sourceFile: {
13+
propDefinition: [
14+
"mapbox",
15+
"sourceFile",
16+
],
17+
},
18+
tilesetName: {
19+
propDefinition: [
20+
"mapbox",
21+
"tilesetName",
22+
],
23+
},
24+
description: {
25+
propDefinition: [
26+
"mapbox",
27+
"description",
28+
],
29+
},
30+
privacySettings: {
31+
propDefinition: [
32+
"mapbox",
33+
"privacySettings",
34+
],
35+
},
36+
},
37+
async run({ $ }) {
38+
const response = await this.mapbox.uploadTileset({
39+
sourceFile: this.sourceFile,
40+
tilesetName: this.tilesetName,
41+
description: this.description,
42+
privacySettings: this.privacySettings,
43+
});
44+
$.export("$summary", `Created tileset ${this.tilesetName}`);
45+
return response;
46+
},
47+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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]().",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
mapbox,
11+
startCoordinate: {
12+
propDefinition: [
13+
mapbox,
14+
"startCoordinate",
15+
],
16+
},
17+
endCoordinate: {
18+
propDefinition: [
19+
mapbox,
20+
"endCoordinate",
21+
],
22+
},
23+
waypoints: {
24+
propDefinition: [
25+
mapbox,
26+
"waypoints",
27+
],
28+
},
29+
transportationMode: {
30+
propDefinition: [
31+
mapbox,
32+
"transportationMode",
33+
],
34+
},
35+
routeType: {
36+
propDefinition: [
37+
mapbox,
38+
"routeType",
39+
],
40+
},
41+
},
42+
async run({ $ }) {
43+
const directions = await this.mapbox.getDirections({
44+
startCoordinate: this.startCoordinate,
45+
endCoordinate: this.endCoordinate,
46+
waypoints: this.waypoints,
47+
transportationMode: this.transportationMode,
48+
routeType: this.routeType,
49+
});
50+
$.export("$summary", "Generated directions successfully.");
51+
return directions;
52+
},
53+
};
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]()",
7+
version: "0.0.{{ts}}",
8+
type: "action",
9+
props: {
10+
mapbox,
11+
address: {
12+
propDefinition: [
13+
mapbox,
14+
"address",
15+
],
16+
},
17+
boundingBox: {
18+
propDefinition: [
19+
mapbox,
20+
"boundingBox",
21+
],
22+
optional: true,
23+
},
24+
proximity: {
25+
propDefinition: [
26+
mapbox,
27+
"proximity",
28+
],
29+
optional: true,
30+
},
31+
},
32+
async run({ $ }) {
33+
const response = await this.mapbox.geocode({
34+
address: this.address,
35+
boundingBox: this.boundingBox,
36+
proximity: this.proximity,
37+
});
38+
$.export("$summary", `Geocoded location for address "${this.address}" retrieved successfully`);
39+
return response;
40+
},
41+
};

components/mapbox/mapbox.app.mjs

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
import { axios } from "@pipedream/platform";
2+
3+
export default {
4+
type: "app",
5+
app: "mapbox",
6+
version: "0.0.{{ts}}",
7+
propDefinitions: {
8+
// Geocoding Props
9+
address: {
10+
type: "string",
11+
label: "Address",
12+
description: "The address to geocode",
13+
},
14+
boundingBox: {
15+
type: "string",
16+
label: "Bounding Box",
17+
description: "Optional bounding box in the format minLng,minLat,maxLng,maxLat",
18+
optional: true,
19+
},
20+
proximity: {
21+
type: "string",
22+
label: "Proximity",
23+
description: "Optional proximity point in the format longitude,latitude",
24+
optional: true,
25+
},
26+
// Directions Props
27+
startCoordinate: {
28+
type: "string",
29+
label: "Start Coordinate",
30+
description: "The starting point in the format longitude,latitude",
31+
},
32+
endCoordinate: {
33+
type: "string",
34+
label: "End Coordinate",
35+
description: "The ending point in the format longitude,latitude",
36+
},
37+
waypoints: {
38+
type: "string[]",
39+
label: "Waypoints",
40+
description: "Optional waypoints as an array of longitude,latitude strings",
41+
optional: true,
42+
},
43+
transportationMode: {
44+
type: "string",
45+
label: "Transportation Mode",
46+
description: "Optional transportation mode",
47+
options: [
48+
{
49+
label: "Driving",
50+
value: "driving",
51+
},
52+
{
53+
label: "Walking",
54+
value: "walking",
55+
},
56+
{
57+
label: "Cycling",
58+
value: "cycling",
59+
},
60+
{
61+
label: "Driving Traffic",
62+
value: "driving-traffic",
63+
},
64+
],
65+
optional: true,
66+
},
67+
routeType: {
68+
type: "string",
69+
label: "Route Type",
70+
description: "Optional route type",
71+
options: [
72+
{
73+
label: "Fastest",
74+
value: "fastest",
75+
},
76+
{
77+
label: "Shortest",
78+
value: "shortest",
79+
},
80+
],
81+
optional: true,
82+
},
83+
// Tileset Upload Props
84+
sourceFile: {
85+
type: "string",
86+
label: "Source File",
87+
description: "The source file for the tileset",
88+
},
89+
tilesetName: {
90+
type: "string",
91+
label: "Tileset Name",
92+
description: "The name of the new tileset",
93+
},
94+
description: {
95+
type: "string",
96+
label: "Description",
97+
description: "Optional description for the tileset",
98+
optional: true,
99+
},
100+
privacySettings: {
101+
type: "string",
102+
label: "Privacy Settings",
103+
description: "Optional privacy settings for the tileset",
104+
options: [
105+
{
106+
label: "Public",
107+
value: "public",
108+
},
109+
{
110+
label: "Private",
111+
value: "private",
112+
},
113+
],
114+
optional: true,
115+
},
116+
},
117+
methods: {
118+
_baseUrl() {
119+
return "https://api.mapbox.com";
120+
},
121+
async _makeRequest(opts = {}) {
122+
const {
123+
$ = this, method = "GET", path = "/", headers = {}, params = {}, data, ...otherOpts
124+
} = opts;
125+
return axios($, {
126+
method,
127+
url: `${this._baseUrl()}${path}`,
128+
headers: {
129+
...headers,
130+
},
131+
params: {
132+
access_token: this.$auth.access_token,
133+
...params,
134+
},
135+
data,
136+
...otherOpts,
137+
});
138+
},
139+
async geocode(opts = {}) {
140+
const {
141+
address, boundingBox, proximity,
142+
} = opts;
143+
const params = {
144+
types: "address",
145+
};
146+
if (boundingBox) {
147+
params.bbox = boundingBox;
148+
}
149+
if (proximity) {
150+
params.proximity = proximity;
151+
}
152+
return this._makeRequest({
153+
path: `/geocoding/v5/mapbox.places/${encodeURIComponent(address)}.json`,
154+
params,
155+
});
156+
},
157+
async getDirections(opts = {}) {
158+
const {
159+
startCoordinate, endCoordinate, waypoints, transportationMode, routeType,
160+
} = opts;
161+
let coordinates = `${startCoordinate};${endCoordinate}`;
162+
if (waypoints && waypoints.length > 0) {
163+
coordinates = `${startCoordinate};${waypoints.join(";")};${endCoordinate}`;
164+
}
165+
const params = {};
166+
if (transportationMode) {
167+
params.profile = transportationMode;
168+
}
169+
if (routeType) {
170+
params.alternatives = routeType === "fastest"
171+
? "true"
172+
: "false";
173+
}
174+
return this._makeRequest({
175+
path: `/directions/v5/mapbox.${transportationMode || "driving"}/${coordinates}`,
176+
params,
177+
});
178+
},
179+
async uploadTileset(opts = {}) {
180+
const {
181+
sourceFile, tilesetName, description, privacySettings,
182+
} = opts;
183+
const params = {
184+
tileset: tilesetName,
185+
};
186+
if (description) {
187+
params.description = description;
188+
}
189+
if (privacySettings) {
190+
params.private = privacySettings === "private"
191+
? true
192+
: false;
193+
}
194+
return this._makeRequest({
195+
method: "POST",
196+
path: `/uploads/v1/${encodeURIComponent(this.$auth.username)}`,
197+
params,
198+
data: {
199+
data: sourceFile,
200+
tileset: {
201+
name: tilesetName,
202+
description: description || "",
203+
private: privacySettings === "private",
204+
},
205+
},
206+
});
207+
},
208+
},
209+
};

0 commit comments

Comments
 (0)