Creating 3rd Party Raster Tiles for Mapbox - mapbox

I am trying to use Mapbox to integrate high-res drone imagery onto our website. It works well when I create the tiles in Mapbox Studio, but it has a size limit and only accepts 8-bit imagery. I have tried creating my own tiles using gdal2tiles, but they won't load on the map. I'm not sure if this is a compatibility issue, my gdal tiling settings, or a problem with the code itself. Any help would be greatly appreciated!
I have tried modifying the code given by Mapbox for 3rd party raster tiles, but it did not work.
gdal2tiles
gdal2tiles.generate_tiles(infile, outdir, np_processes=4, zoom='0-22', srs='EPSG:3857')
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibHVrYXNmcmFzZXIiLCJhIjoiY2p5ZnN3Nm12MWZrdDNscW85aHAwbW52eiJ9.fbdPTtQHTUWaLTex9dCO0g';
var map = new mapboxgl.Map({
container: 'map', // container id
style: {
"version": 8,
"sources": {
"raster-tiles": {
"type": "raster",
"tiles": ["./{z}/{x}/{y}.png"],
"tileSize": 256
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "raster-tiles",
"minzoom": 0,
"maxzoom": 22
}]
},
I have tried running this html locally in the same folder as the tiles, and uploading both to our AWS server, but neither works. In both cases the background map loads, but the tiles do not.

gdal2tiles outputs tms tiles and not xyz tiles so try setting a scheme on the source like this:
new mapboxgl.Map({
container: 'map', // container id
style: {
"version": 8,
"sources": {
"raster-tiles": {
"type": "raster",
"tiles": ["./{z}/{x}/{y}.png"],
"tileSize": 256,
"scheme": "tms"
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "raster-tiles",
"minzoom": 0,
"maxzoom": 22
}]
}
});

Related

Mapbox GL-JS : Adding new tileset overlay under labels

I have a tileset that I am loading into my map (map is called topleftmap). I want the city and other labels to load over the tiles, not under - sort of like in this example from Mapbox : https://docs.mapbox.com/mapbox-gl-js/example/geojson-layer-in-stack/
How do I do this with tilesets? I have an external JS file I use to load in the tilesets like this :
topleftmapbox.addLayer({
"id": "currentProduct",
"type": "raster",
"source": {
"type": "raster",
"tiles": ["tiles/" + productSelection + "/" + productLocation + "/{z}/{x}/{y}.png"],
"minzoom": 8,
"maxzoom": 10,
"scheme": "tms",
"tileSize": 256,
}});
So of course that works fine, but the labels are UNDER the tiles. I have tried this below, (using the example from Mapbox) but it doesn't seem to work :
var layers = topleftmapbox.getStyle().layers;
// Find the index of the first symbol layer in the map style
var firstSymbolId;
for (var i = 0; i < layers.length; i++) {
if (layers[i].type === 'symbol') {
firstSymbolId = layers[i].id;
break;
}
}
topleftmapbox.addLayer({
"id": "currentProduct",
"type": "raster",
"source": {
"type": "raster",
"tiles": ["tiles/" + productSelection + "/" + productLocation + "/{z}/{x}/{y}.png"],
"minzoom": 8,
"maxzoom": 10,
"scheme": "tms",
"tileSize": 256,
},firstSymbolId});
Lastly, here is an image showing what it looks like. My tiles are slightly transparent, but the labels should be bright white, like the ones away from the data. Any thoughts?
You're doing everything right. You just have a slight syntax mistake:
},firstSymbolId});
This should be:
}}, firstSymbolId);

react-map-gl without API key using osm tiles

Is it possible?
This tells me it is, but dont know why it defines and API key.
but I cannot get it to work with react-map-gl StaticMap class. The property I can see from that class is just mapStyle which would take a standard Mapbox vector tiles path/name. Does it take an object? My code does not give me an error or show the tiles I request.
<DeckGL>
<StaticMap
mapStyle= {{
"version": 7,
"sources": {
"simple-tiles": {
"type": "raster",
"tiles":["http://a.tile.openstreetmap.org/{z}/{x}/{y}.png", "http://b.tile.openstreetmap.org/{z}/{x}/{y}.png"],
"tileSize": 256
},
"power": {
"type": "vector",
"tiles": ["http://gpstrails.info/ex/leaflet/power/osm/{z}/{x}/{y}.json"]
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "simple-tiles",
"minzoom": 0,
"maxzoom": 22
},
{
"id": "road",
"source": "power",
"source-layer": "power",
"type": "line",
"layout": {
"line-join": "round",
"line-cap": "round",
},
"paint": {
"line-color": "red",
"line-width": 4,
}
}]
}}/>
</DeckGL>
Thank you
Edit: from the correct answer, and to keep things in SO, this is the json living on the S3:
{
"version": 8,
"name": "OSM",
"metadata": {
},
"sources": {
"openmaptiles": {
"type": "vector",
"url": "https://free.tilehosting.com/data/v3.json?key={key}"
},
"osm": {
"type": "raster",
"tiles": [
"http://tile.openstreetmap.org/{z}/{x}/{y}.png"
],
"minzoom": 0,
"maxzoom": 14
},
"91y5159eg": {
"type": "vector",
"url": "http://localhost:3000/tilejson.json"
}
},
"sprite": "https://openmaptiles.github.io/klokantech-basic-gl-style/sprite",
"glyphs": "https://free.tilehosting.com/fonts/{fontstack}/{range}.pbf?key=undefined",
"layers": [
{
"id": "osm",
"type": "raster",
"source": "osm"
}
],
"id": "klokantech-basic"
}
UPDATE: Mapbox changed their license in 2.0 so the accepted answer is correct for versions < 2.0. Mapbox > 2.0 will complain if no access_token is provided.
The trick is in the style that's used. A style is a JSON object, whose specification you can read more about here. You can generate custom styles using tools such as Maputnik, a visual editor that generates style-compliant files for use in MapboxGL maps. Once you have an appropriate style generated, you can use it in React Map GL.
Here's what the basic component would look like, as altered from the example in the Github repo:
<ReactMapGL
mapStyle="https://s3.amazonaws.com/cdn.brianbancroft.io/assets/osmstyle.json"
{...this.state.viewport}
onViewportChange={viewport => this.setState({ viewport })}
/>
Note that this is just an abstract example. The tile loads from OSM here are a bit too slow to be useful in production. But it should illustrate how to make maps without relying on the services side of Mapbox.

What is the proper way to select and style markers on Mapbox GL JS?

I am working with a Mapbox map that has points. I would like to know the correct procedure for adding a marker-symbol. Here is my GeoJSON:
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-89.12312324,
13.686886
]
},
"properties": {
"title": "Random Location",
"description": "Individual"
}
}
]
Here is an example from the Mapbox docs:
map.addLayer({
"id": "airport",
"source": "airports",
"source-layer": "ne_10m_airports",
"type": "symbol",
"layout": {
"icon-image": "airport-15",
"icon-padding": 0
},
"filter": ["in", "abbrev", ""]
});
When I use this
"layout": {
"icon-image": "marker-11",
"icon-allow-overlap": true,
}
I get little brown dots instead of the classic marker.
I am using
<script src='https://api.mapbox.com/mapbox-gl-js/v0.18.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v0.18.0/mapbox-gl.css' rel='stylesheet' />
and I am using the
style: 'mapbox://styles/mapbox/outdoors-v9', //stylesheet location
My entire script looks like this:
mapboxgl.accessToken = 'pk.mylongAkey';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/outdoors-v9', //stylesheet location
center: [-88.866245, 13.770391], // starting position
zoom: 6 // starting zoom
});
var url = '/maps/index.json';
var source = new mapboxgl.GeoJSONSource({
data: url
});
map.on('load', function () {
map.addSource('location', source);
map.addLayer({
"id": "map",
"type": "symbol",
"source": "location",
"source-layer": 'location',
"layout": {
"icon-image": "marker-11",
"icon-allow-overlap": true,
}
});
});
map.on('click', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['map'] });
if (!features.length) {
return;
}
var feature = features[0];
// Populate the popup and set its coordinates
// based on the feature found.
var popup = new mapboxgl.Popup()
.setLngLat(feature.geometry.coordinates)
.setHTML(feature.properties.title)
.addTo(map);
});
// Use the same approach as above to indicate that the symbols are clickable
// by changing the cursor style to 'pointer'.
map.on('mousemove', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['map'] });
map.getCanvas().style.cursor = (features.length) ? 'pointer' : '';
});
Am I missing something? Also, the popups don't popup above the points, they popup over top of the icon. You can't tell with this little brown dot, but with the rocket for example the popup is in the middle of the rocket. Thanks!
Here is a screenshot
I am working with a Mapbox map that has points. I would like to know the correct procedure for adding a marker-symbol. Is it better to do it in the GeoJSON like:
...
Or is better to use layout like this:
...
Embedding style information in the GeoJSON (the former) is a specification called simplestyle, which is not supported in GL JS. Using layout (the latter) is the only way to style features in GL JS.
I get little brown dots instead of the classic marker.
Could you please provide a screenshot of this?
In the Mapbox Studio Editor, you need to make sure that you actually have an icon called "marker-11" in your icon library. If not, it doesn't know what you are referencing and will default to a dot.
Otherwise, everything else looks fine.

satellite tile source not working

https://www.mapbox.com/mapbox-gl-style-spec/#sources-raster
This results in black tiles without error message, I have no idea why?
var style = {
"version": 8,
"sprite": "mapbox://sprites/mapbox/bright-v8",
"glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
"sources": {
"mapbox-satellite": {
"type": "raster",
"url": "mapbox://mapbox.satellite",
"tileSize": 256
}
},
"layers": [{
"id": "new-layer",
"type": "background",
"paint": {"background-color": "#111"},
"interactive": true
}],
}
mapboxgl.accessToken = ...
var map = new mapboxgl.Map({
container: 'map',
style: style,
//style: 'mapbox://styles/mapbox/satellite-v8',
center: [4.7095, 52.0393],
zoom: 9,
hash: true
});
It looks like you've only added the raster source. If you want the satellite tiles to be drawn on your map, you must also add a raster layer which uses the raster source. See https://www.mapbox.com/mapbox-gl-style-spec/#layers-raster

Adding custom points to mapbox studio

In Mapbox studio classic, you just click where you want a new "marker" and it creates one. I want to do the same thing in the new mapbox studio but that feature doesn't seem to exist. Please note, I do not have a dataset to upload, I need to create a dataset through Mapbox Studio.
If anybody has any insight for me that would be super!
You can add your data to Mapbox Studio along with this custom svg marker. I styled it similar to the old Leaflet L.marker: https://github.com/Ccantey/icons/blob/master/svgs/placeMarker-Blue-Shadow.svg
Then in the layout properties you can set "marker-symbol"/"icon-image" to "myMarker-Blue-Shadow":
map.addSource("pointclick", {
"type": "geojson",
"data": {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [e.lngLat.lng, e.lngLat.lat]
},
"properties": {
"title": "mouseclick",
"marker-symbol": "myMarker-Blue-Shadow"
}
}
});
map.addLayer({
"id": "pointclick",
type: 'symbol',
source: 'pointclick',
"layout": {
"icon-image": "{marker-symbol}",
"icon-size":1,
"icon-offset": [0, -13]
},
"paint": {}
});