Mapbox GL style line color based on property text value - mapbox

I'm trying to style a single GeoJSON source with different line colors based on a feature property using react-map-gl, and I can't find a way to get set the color of lines in a smart way.
Most of all, I would love to apply a function on the dataset to return the color of my own choosing based on a feature property value, but so far I haven't fount anything about it. If you know about it, please point in my direction:)
If I have the following GeoJSON:
{
"type": "FeatureCollection",
"name": "lineData",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "Need": "Urgent" }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 10.653823175868171, 59.676506860589157 ], [ 10.652881996887283, 59.675443989456632 ] ] ] } },
{ "type": "Feature", "properties": { "Need": "Starting" }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 10.658536710768077, 59.680279341285896 ], [ 10.65787427600862, 59.680222775937636 ] ] ] } },
{ "type": "Feature", "properties": { "Need": "Medium" }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 10.653224904719789, 59.67859470385492 ], [ 10.653201052045171, 59.678557551379008 ] ] ] } },
]
}
I would like to be able to style this source data with different line colors based on the property "Need". Say, urgent becomes red, medium becomes yellow, and starting becomes green.
I've read about styling expressions at mapbox, and I believe the "feature-state" is key to solving this, but I cant wrap my head around how to get the color converted from a feature.
If this in the rendering:
<Source id="my-data" type="geojson" data={TheDataFileWithSomeData}>
<Layer {...layerStyleTheLines } />
</Source>
Then I want a layer styling something like this (not working):
const layerStyleTheLines = {
id: 'style_it_to_red',
type: 'line',
paint: {
'line-color': [
[["==", ["feature-state", "Need"], "Urgent"],"red"],
[["==", ["feature-state", "Need"], "Medium"],"yellow"],
[["==", ["feature-state", "Need"], "Starting"],"green"]
],
'line-width': 3,
}
};
Thanks for all your help!

I've read about styling expressions at mapbox, and I believe the "feature-state" is key to solving this, but I cant wrap my head around how to get the color converted from a feature.
You only want feature-state if you're intending to manipulate the feature attributes dynamically, which I don't think you are.
You probably just want regular data-driven styling:
const layerStyleTheLines = {
id: 'style_it_to_red',
type: 'line',
paint: {
'line-color': [
'match', ['get','Need'],
'Urgent', 'red',
'Medium', 'yellow',
'Starting','green',
'black'
],
'line-width': 3,
}
};

Related

$geoWithin query fail to retrieve point included in the queried polygon

I'm trying to query a document using its location with two polygons that are quite similar. Both includes the location of the document but one finds it and the other not. I thought that this was related to "big polygons" but I managed to reduce the polygons enough to rule out this possibility. See previous post. Any idea explaining such a difference?
Playground: https://mongoplayground.net/p/sTEtYD3HU8m:
Document:
{
"_id": {
"$oid": "63ef9379e671073bfb963145"
},
"geometry": {
"type": "Point",
"coordinates": [
2.834,
47.264
]
},
"_class": "org.example.springdatamongodbgeowithinissue.model.SamplingGeometry"
}
Is not in search results for this polygon:
{
"geometry": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[
-16.1,
49.12
],
[
-16.1,
46.156
],
[
16.1,
46.156
],
[
16.1,
49.12
],
[
-16.1,
49.12
]
]
]
}
}
}
}
Still the same issue with Earth's curvature. Your area is long enough to take it into account:
zooming in and's clearly outside:
The map: https://jsfiddle.net/blex18/w9g4bzyk/1/
var map = new google.maps.Map(document.body, {zoom:5,center:{lat:47.264,lng:2.834}});
new google.maps.Polygon({geodesic:true,map:map,geodesic:true,
path:[
{lat:49.12,lng:-16.1},
{lat:46.156,lng:-16.1},
{lat:46.156,lng:16.1},
{lat:49.12,lng:16.1},
{lat:49.12,lng:-16.1}]});
new google.maps.Marker({
position: {lat:47.264,lng:2.834},
map,
title: "Here",
});

Adding a lot of georeferenced images makes mapbox very slow

I have a map for which I have to add a lot of (about 150) images on a map.
I've seen their example Add a raster image to a map layer, which shows that you need to add a source with coordinates, then add a layer to display your image on the map.
I made a loop to add all my images, but it makes my map really laggy when moving on it; see demo here.
I was working with Leaflet before, and it was handling that nicely.
Is Mapbox supposed to work fine with a lot of images loaded, and is there strategies to make it render smoothly?
One attempt that I made is that I was wondering if this was due to a excessive amount of layers; and searched how to add multiple images on a same layer (same question here); but haven't found a way to do it: this does not work:
//add source
map.addSource("imagesSource", {
type: "geojson",
data: {
"type": "FeatureCollection",
"features": [
[
{
"type": "image",
"url": "http://url/image1.png",
"coordinates": [
[
4.3813338557822,
50.842573058675
],
[
4.418685805334,
50.842573058675
],
[
4.418685805334,
50.825893099756
],
[
4.3813338557822,
50.825893099756
]
]
},
{
"type": "image",
"url": "http://url/image2.png",
"coordinates": [
[
4.3981741530879,
50.835614531226
],
[
4.402177428321,
50.835614531226
],
[
4.402177428321,
50.833826839538
],
[
4.3981741530879,
50.833826839538
]
]
},
{
"type": "image",
"url": "http://url/image3.png",
"coordinates": [
[
4.3813338557822,
50.842573058675
],
[
4.418685805334,
50.842573058675
],
[
4.418685805334,
50.825893099756
],
[
4.3813338557822,
50.825893099756
]
]
}
]
]
}
});
//add layer
map.addLayer({
"id": 'imagesLayer',
"source": 'imagesSource',
"type": "raster",
"paint": {"raster-opacity": 0.85}
})
NB There is another example named Add a georeferenced image, which is confusing since this one is for adding a tileset; which is not my case: I want to be able to handle each image individually (toggle a selection, etc).

dc.js and dc.leaflet.js; wrong type of map returned

I'm trying to make a dashboard using dc.js. It has a few charts and a choroplethChart. It all worked fine, but I needed to add leaflet to the map. I've followed this sample and used dc.leaflet.js library, but instead of choroplethChart it returns Markers (see picture)
(this is how it looked before using leaflet)
The code is below and this is where geojson resides:
var usChart = dc_leaflet.choroplethChart("#us-chart");
usChart.width(1000)
.height(450)
.dimension(stateDim)
.group(totalDemandByStation)
.center([ 51.4963, -0.143 ])
.zoom(11)
.geojson(statesJson)
.colors(["#E2F2FF", "#C4E4FF", "#9ED2FF", "#81C5FF", "#6BBAFF", "#51AEFF", "#36A2FF", "#1E96FF", "#0089FF", "#0061B5"])
.colorDomain([0, max_state])
.colors(['#fff7ec','#fee8c8','#fdd49e','#fdbb84','#fc8d59','#ef6548','#d7301f','#b30000','#7f0000'])
.colorAccessor(function(d,i) {
return d.value;
})
.featureKeyAccessor(function(feature) {
return feature.properties.name;
})
.renderPopup(true)
.popup(function(d,feature) {
return feature.properties.name+" : "+d.value;
})
.legend(dc_leaflet.legend().position('bottomright'));
//https://github.com/dc-js/dc.js/issues/419
usChart.on("preRender", function(chart) {
chart.colorDomain(d3.extent(chart.data(), chart.valueAccessor()));
})
usChart.on("preRedraw", function(chart) {
chart.colorDomain(d3.extent(chart.data(), chart.valueAccessor()));
})
I'm not an expert here, but the choropleth is expecting map data rather than point data. The features in your geojson are points:
{
"crs": {
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
},
"type": "name"
},
"features": [
{
"geometry": {
"coordinates": [
-0.013071299999987,
51.510716
],
"type": "Point"
},
"properties": {
"id": "940GZZDLALL",
"labelX": 30,
"lines": [
{
"name": "DLR"
}
],
"name": "All Saints",
"tfl_intid": 850
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
0.061052699999989,
51.51427850000001
],
"type": "Point"
},
"properties": {
"id": "940GZZDLBEC",
"labelX": -30,
"lines": [
{
"name": "DLR"
}
],
"name": "Beckton",
"tfl_intid": 895
},
"type": "Feature"
},
...
To draw a choropleth, Leaflet will need features whose types are Polygon.
So my guess is that Leaflet is punting and drawing markers

Color only the edge of a circle mapbox gl js

I want to show the outline of a circle on an interactive map (no fill) however, the paint options in mapbox-gl-js seem limited to fill only.
https://www.mapbox.com/mapbox-gl-style-spec/#layers-circle
var styles = [{
"id": 'points',
"interactive": true,
"type": "circle",
"source": "geojson",
"paint": {
"circle-radius": 5,
"circle-color": "#000
},
"filter": ["in", "$type", "Point"]
}, {
"type": "line",
"source": "geojson",
"layout": {
"line-cap": "round",
"line-join": "round"
},
"paint": {
"line-color": "#000",
"line-width": 2.5
},
"filter": ["in", "$type", "LineString"]
}];
Am i missing something or is this just not possible?
This is now possible, with circle-opacity.
E.g.:
"paint": {
"circle-opacity": 0,
"circle-stroke-width": 1,
"circle-stroke-color": #000
}
Not currently possible. Current top workaround appears to be layering two circles of slightly different sizes.
https://github.com/mapbox/mapbox-gl-js/issues/1713
https://github.com/mapbox/mapbox-gl-style-spec/issues/379
I'm having trouble running custom color 'match' and having opacity controls running simultaneously.
I can get both working, but not at the same time. See code below.
var coorAddresses = [ [ -75.7040473, 45.418067,"Medium" ], [-75.7040473, 45.418067, "Medium"], [-79.32930440000001, 43.7730495, "Unknown"]]
$.getJSON(coodAddresses, function(data) {
for(var itemIndex in data) {
// push new feature to the collection
featureCollection.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [data[itemIndex][0], data[itemIndex][1]]
},
"properties": {
"size_by": data[itemIndex][2],
"color_by": data[itemIndex][2]
},
});
}
});
map.on('load', function () {
map.addLayer({
"id": "points",
"type": "circle",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": featureCollection
}
},
"paint": {
"circle-color": [
'match',
['get', 'size_by'],
'Easy',
'#e4f400',
'Medium',
'#f48a00',
'Unknown',
'#6af400',
/* other */ '#00e4f4'
],
"circle-radius": [
'match',
['get', 'size_by'],
'Easy',
4,
'Medium',
7,
'Unknown',
2,
/* other */ 1000
],
// "circle-opacity": 0, // color does not show if i uncomment these lines
// "circle-stroke-width": 1, // do not get desired 'hollow' circle unless these lines run
}});
Trying to troubleshoot.

How to make a GeometryCollection in GeoJSON with a single point + polygon?

How do you add a point to a polygon as a single feature? According to the GeoJson specs, this is known as a "GeometryCollection".
Example of a 'GeometryCollection':
{ "type": "GeometryCollection",
"geometries": [
{ "type": "Point",
"coordinates": [100.0, 0.0]
},
{ "type": "LineString",
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
}
]
}
I tried adding a point to a polygon feature, but I couldn't get it to show on my mapbox map because I guess it is invalid GeoJson.
Anyone know what the proper way of doing this is? There are not many examples to follow on the web.
My take: [jsfilddle]
var myRegions = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometries": [
{
"type": "Point",
"coordinates": [
61.34765625,
48.63290858589535
]
},
{
"type": "Polygon",
"coordinates": [
[
[
59.94140624999999,
50.65294336725709
],
[
54.931640625,
50.90303283111257
],
[
51.943359375,
51.04139389812637
],
[
50.9765625,
48.19538740833338
],
[
52.55859375,
46.46813299215554
],
[
52.998046875,
43.8028187190472
],
[
54.4921875,
42.391008609205045
],
[
57.041015625,
43.29320031385282
],
[
59.8974609375,
45.398449976304086
],
[
62.5341796875,
44.08758502824516
],
[
65.6982421875,
45.73685954736049
],
[
68.37890625,
48.3416461723746
],
[
65.8740234375,
49.18170338770663
],
[
63.720703125,
49.97948776108648
],
[
63.80859374999999,
52.348763181988076
],
[
61.4794921875,
52.32191088594773
],
[
59.9853515625,
51.86292391360244
],
[
61.9189453125,
51.09662294502995
],
[
60.5126953125,
50.51342652633956
],
[
59.94140624999999,
50.65294336725709
]
]
]
}
]
}
]
};
As said in GeoJSON spec, a Feature object has exactly one geometry member, which is a Geometry object (or null).
A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value.
Among the possible geometry's you can indeed use a GeometryCollection, which must have a member geometries. The latter is an array of other geometries, i.e. your point, polygon, etc., or even another GeometryCollection.
A geometry collection must have a member with the name "geometries". The value corresponding to "geometries" is an array. Each element in this array is a GeoJSON geometry object.
So in your case you could simply do something like:
var myRegions = {
"type": "FeatureCollection",
"features": [{
"type": "Feature", // single feature
"properties": {},
"geometry": { // unique geometry member
"type": "GeometryCollection", // the geometry can be a GeometryCollection
"geometries": [ // unique geometries member
{ // each array item is a geometry object
"type": "Point",
"coordinates": [
61.34765625,
48.63290858589535
]
},
{
"type": "Polygon",
"coordinates": [
[
[
59.94140624999999,
50.65294336725709
],
// more points…
[
59.94140624999999,
50.65294336725709
]
]
]
}
]
}
}]
};
Updated jsfiddle: http://jsfiddle.net/rh8ok5t8/18/
I'm unsure to what you're actually trying to accomplish because you say you want to create a geometrycollection but in your example you're creating a featurecollection which is not the same by far.
A featurecollection is a collection of features:
A GeoJSON object with the type "FeatureCollection" is a feature collection object. An object of type "FeatureCollection" must have a member with the name "features". The value corresponding to "features" is an array.
http://geojson.org/geojson-spec.html#feature-collection-objects
Here's an example of a featurecollection:
{
type: "FeatureCollection",
features: [{
"type": "Feature",
"properties": {
"value": "foo"
},
"geometry": {
"type": "Point",
"coordinates": [0,0]
}
}, {
"type": "Feature",
"properties": {
"value": "bar"
},
"geometry": {
"type": "Polygon",
"coordinates": [[[45, 45], [45, -45], [-45, -45], [-45, 45], [45,45]]]
}
}]
}
A geometrycollection is a single feature (which you could contain in a featurecollection):
A GeoJSON object with the type "Feature" is a feature object. A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value. A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value). If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id".
http://geojson.org/geojson-spec.html#feature-objects
with multiple geometries:
A GeoJSON object with type "GeometryCollection" is a geometry object which represents a collection of geometry objects. A geometry collection must have a member with the name "geometries". The value corresponding to "geometries" is an array. Each element in this array is a GeoJSON geometry object.
http://geojson.org/geojson-spec.html#geometry-collection
And here's an example of a geometrycollection feature:
{
"type": "GeometryCollection",
"properties": {
"value": "foo"
},
"geometries": [{
"type": "Point",
"coordinates": [0, 0]
}, {
"type": "Polygon",
"coordinates": [[[45, 45], [45, -45], [-45, -45], [-45, 45], [45,45]]]
}]
}