How to serve styled vector map data via CartoCSS - leaflet

I've got an example data set of lines and points, representing peoples' journey to work and where they feel at risk from road accidents.
Now, the data have attributes such as Place (Home, Work, Danger) and the severity of accidents they fear in different places. So the wider question is how to visualise this on a web map. For that I've looked into Google Maps, Leaflet and OpenLayers3. Each of these looks great and allow nice styling but none, to my eye, have the neatness of CartoCSS for fast, complex and intuitive styling, like the code below used to create the image below:
So I tried it out in tile mill and this is what I came up with, after converting the things to raster tiles via TileMill.
But this is a silly way to do things: it cannot overlay a base layer and would be much more efficient to just serve the vector data and style them in the client side, perhaps using something like mapbox.js. But I cannot work out how to use the lovely CartoCSS language in the client side to style GeoJSON files. (How) Is this possible?
p.s. here's how I styled the above map
#tlines {
line-width:1;
line-color: #0b0;
}
#tpoints.points{
marker-width: 6;
}
#tpoints.points[Place='Danger'] {
marker-fill:#db0d0d;
}
#tpoints.points[Place='Home'] {
marker-fill:#db0;
}
#tpoints.points[Severity='1']{
marker-width: 4;}
#tpoints.points[Severity='2']{
marker-width: 6;}
#tpoints.points[Severity='3']{
marker-width: 10;}

I cannot work out how to use the lovely CartoCSS language in the client side to style GeoJSON files. (How) Is this possible?
It is not possible to use CartoCSS for styling features browser side.
To learn how to style GeoJSON in Mapbox.js/Leaflet.js take a look at the docs here:
Styling GeoJSON in Mapbox.js https://www.mapbox.com/mapbox.js/api/v1.6.2/l-geojson/ (works exactly as in Leaflet.js as Mapbox.js is based on Leaflet.js)
You can embed style options directly into the GeoJSON object with L.mapbox.simplestyle.style https://www.mapbox.com/mapbox.js/api/v1.6.2/l-mapbox-simplestyle-style/

Alex rightly points out that vector styling in Leaflet/Mapbox.js is a different creature than CartoCSS styling a la Tilemill or Mapbox Studio. However, it is possible to style features in a browser using CartoCSS; it just requires one of two things:
A different platform: CartoDB has an excellent JS library for styling hosted data with CartoCSS, based on leaflet and fully compatible with Mapbox basemaps.
Hosting your own tile server, either the CartoDB server or a combination of Mapbox-built tools.

If you are using GeoJson as data source, you may try the Leaflet.geojsonCSS plug-in for Leaflet.js. The Geojson CSS provides the styling definition for each feature in the collection in a css-like format. Using this requires to add the styling definition in the geojson beforehand.

Related

vector tiles, mapbox style.json

I'd like to use the here maps vector tiles in a mapbox gl app. However, in order to do so, I need to define a style.json, see here for the spec, and here for an example.
Does here have a base style.json that I can use?
On the examples page, where using Mapbox is discussed, the documentation simply says: style: "map_style.json", // you should use your own style. It's a bit of a PITA (pain in the a**) to build from scratch, though.
Thanks for any help.
map_style.json -
this could be different for different use case and plain "JSON" file. This needs to be according to the map properties that needs to be displayed to use the other container style.

Marker's descriptions do not show up in Mapbox-js

I have a dataset, created in mapbox from an original GEOJson file, and later associated to a tileset (aslo in MapBox Studio). It was parsed fine, and I could inspect it in Studio>Dataset, all properties were parsed.
This tileset is then used as a layer on a stylep; the points are displayed, but the point's descriptions are not displayed when using MapBox-gl-js.
What do I have to do to display those descriptions when clicking on a waypoint?
note: Adding them by hand when creating the map isn't my preferred solution. Can this be done transparently from the original dataset saved on mapbox servers?
You've tagged this post as mapbox-gl-js, but refer to MapBox-js - which library are you using?
Assuming you mean Mapbox GL JS (https://docs.mapbox.com/mapbox-gl-js), I modified this example of displaying data from a GeoJSON source to use a tileset from Studio instead - but it's the same underlying data. You can see it here.

Mapbox Studio: can you change the style after the fact?

Is it possible to change the base style of a custom style after the fact in Mapbox Studio? If yes, how? I looked around the UI and in the documentation but couldn't find how.
Say I created a private style from the Vintage designer style (for example's sake), then added and customized layers by importing tilesets, can I switch to the another base style (e.g. North Star), or do I need to start a new custom style from the start?
Yes. Mapbox Studio is just a GUI for generating Javascript; it outputs a giant JSON file.
(A Mapbox Style is just a JSON object.)
Each of your data layers is included in that JSON (and can be copied and pasted into another style JSON).
Use these instructions to learn the process:
https://mapbox.com/help/transfer-styles-between-accounts
Hope that helps!

Add marker in Mapbox URL (not via JS)

I'd like to generate a link to a Mapbox map, centered on a particular latitude and longitude, that also contains a marker on that spot. This is trivial when generating a static map:
https://api.mapbox.com/v4/mapbox.streets/pin-m(<lat>,<lon>)/<lat>,<lon>,<zoom>/320x160.png?access_token=<access token>
with the pin-m() segment specifying the marker. When generating a link to a full, interactive (zoomable/pannable) map, however, specifying a marker doesn't seem to be an option. This URL goes to the map I want:
https://api.mapbox.com/v4/mapbox.streets/zoompan.html?access_token=<access token>#<zoom>/<lat>/<lon>
but there's no marker. I looked through all the documentation and couldn't find any reference to a way to do this. From the docs it looks like the only way to add a marker to the map is to host a page and generate the map via JS in a script. I can do this, but I'd much prefer to be able to just add a parameter to a URL instead of adding a whole new route/controller/view/associated specs for our Rails app for what seems to me to be a very minor addition.
Does anybody know of an undocumented way to do this? Or a documented way that I've just overlooked?
You aren't able to add a marker through a url parameter like the static API allows. You could add a marker by following this example for Mapbox JS or you could use the newer Mapbox GL JS to center the map around a marker like in this example.
I Hope this helps

Unsure about the best way to fully separate my Dojo layers

Hey so I'm finding the documentation around building dojo a little hazy around layers.
For my Dojo 1.7+ application I would like a layer that contains only Dojo, and a layer that only contains my code, so I can place the appropriate copyright/license headers at the top.
Looking at build profile template, I see:
layers : {
"dojo/dojo":{
include:["dojo/dojo","dojo/i18n","dojo/ready","dojo/domReady"]
},
"myapp/core":{
include:["myapp/core/module1","myapp/core/module2","myapp/core/module3"],
exclude:["dojo/dojo"]
}
}
But when I look inside my 'myapp/core' layer js file I see lots of occurrences of
'define("dojo*'.
I started tackling this by finding each occurrence of the dojo define and putting that in the dojo/dojo layer include list, but that doesn't seem like the appropriate way of doing layers, is it? At the very least can't I just include certain packages? Am I making a big misunderstanding here?
Bonus it seems like the layer property 'copyrightFile' no longer works. Has that been deprecated, or changed?
Thanks
Are all these ("dojo/i18n","dojo/ready","dojo/domReady") dependencies the only dependencies that your external modules need? If myapp/core/module2 requires a dojo module that is not included in the dojo core layer, then it will be included in the myapp/core layer.
I have gone down the path you are going found it difficult to maintain the separation of code over time. I would create a single layer with both dojo and your code.
Use a layer to encapsulate code that is for specific area of functionality. For example, I have a graphical workflow editor that has it's own layer because it includes a bunch of svg code that doesn't need to be present in the rest of the application.