Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] updated script, started to add dds guide to darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
jmkiley committed Mar 14, 2017
1 parent 172d0fd commit ceabb7d
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 6 deletions.
137 changes: 137 additions & 0 deletions platform/darwin/docs/guides/Data-Driven Styling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<!---
To do - JK:
- [ ] Fill it out with more detailed explanations
- [ ] Add examples for camera or composite style functions
- [ ] Cluster earthquakes as an example for camera function?
- [ ] Clean up & optimize screenshots - replace with screenshots showing circles below symbol layer
- [ ] Link API docs
- [ ] Add cocoaprefix so this can be moved to darwin
---->

<%
const os = locals.os;
const iOS = os === 'iOS';
const macOS = os === 'macOS';
const cocoaPrefix = iOS ? 'UI' : 'NS';
const layers = locals.layers;
const renamedProperties = locals.renamedProperties;
-%>
<!--
This file is generated.
Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`.
-->

##Data-Driven Styling

Mapbox’s data-driven styling features allow you to use data properties to style your maps. You can style objects within the same layer differently based on their individual attributes. This enables you to style icons, routes, parks, and more based on attributes.

![available bikes](img/data-driven-styling/citibikes.png)

_Insert cool overview of things you can do! Try to include polylines & polygons_

### How to use Data-Driven Styling
This guide uses earthquake data from the [U.S. Geological Survey](https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php) to style a map based on attributes. For more information about how to work with GeoJSON data in our iOS SDK, please see our [working with GeoJSON data](https://www.mapbox.com/ios-sdk/api/3.5.0-beta.1/working-with-geojson-data.html) guide.

`MGLStyleFunction`

There are three subclasses of `MGLStyleFunction`:

* `MGLCameraStyleFunction` - For a style value that changes with zoom level. For example, you can make the radius of a circle to increase based on zoom level. In iOS SDK v3.4, this was a `MGLStyleFunction`.
* `MGLSourceStyleFunction` - For a style value that changes with the attributes of a feature. You can adjust the radius of a circle based on the magnitude of an earthquake, for example.
* `MGLCompositeStyleFunction` - For a style value that changes with both zoom level and attribute values. For example, you can add a circle layer where each circle has a radius based on both zoom level and the magnitude of an earthquake.

The documentation for individual style properties will note which style functions are enabled for that property.

####Stops

Stops are key-value pairs that that determine a style value. With a `MGLCameraSourceFunction` stop, you can use a dictionary with a zoom level for a key and a `MGLStyleValue` for the value. For example, you can use a stops dictionary with zoom levels 0, 10, and 20 as keys, and yellow, orange, and red as the values. A `MGLSourceStyleFunction` uses the relevant attribute value as the key.

####`MGLInterpolationMode`

The effect a key has on the style value is determined by the interpolation mode. There are four interpolation modes that can be used with a source style function: exponential, interval, categorical, and identity. You can also use exponential and interval interpolation modes with a camera style function.

#####Exponential

`MGLInterpolationModelExponential` creates a linear effect based on the values. The key value is the base for interpolation, and the style value is based on where an attribute value falls between two keys.

The stops dictionary below, for example, shows colors that continuously shift from yellow to orange to red to blue to white based on the attribute value.

``` swift
let url = URL(string: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson")
let symbolSource = MGLSource(identifier: "source")
let symbolLayer = MGLSymbolStyleLayer(identifier: "place-city-sm", source: symbolSource)

let source = MGLShapeSource(identifier: "earthquakes", url: url, options: nil)
style.addSource(source)

let stops = [0 : MGLStyleValue(rawValue: UIColor.yellow),
2.5 : MGLStyleValue(rawValue: UIColor.orange),
5: MGLStyleValue(rawValue: UIColor.red),
7.5 : MGLStyleValue(rawValue: UIColor.blue),
10 : MGLStyleValue(rawValue: UIColor.white)]

let layer = MGLCircleStyleLayer(identifier: "circles", source: source)
layer.circleColor = MGLStyleValue<UIColor>(interpolationMode: .exponential,
sourceStops: stops,
attributeName: "mag",
options: [.defaultValue: MGLStyleValue<UIColor>(rawValue: .green)])
layer.circleRadius = MGLStyleValue(rawValue: 10)
style.insertLayer(layer, below: symbolLayer)
```

![exponential mode](img/data-driven-styling/exponential.png)

#####Interval

`MGLInterpolationModeInterval` creates a range using the keys stops dictionary. The range is from the given key to just less than the next key. The attribute values that fall into that range are then styled using the style value assigned to that key.

When we use the stops dictionary given above with an interval interpolation mode, we create ranges where earthquakes with a magnitude of 0 to just less than 2.5 would be yellow, 2.5 to just less than 5 would be orange, and so on.

``` swift
layer.circleColor = MGLStyleValue<UIColor>(interpolationMode: .interval,
sourceStops: stops,
attributeName: "mag",
options: [.defaultValue: MGLStyleValue<UIColor>(rawValue: .green)])
```
![interval mode](img/data-driven-styling/interval.png)
<!-- Clarify difference between interval and categorical -->
#####Categorical

Returns the output value that is equal to the stop for the function input. We’re going to use a different stops dictionary than we did for the previous two modes.

There are three main types of events in the dataset: earthquakes, explosions, and quarry blasts. In this case, the color of the circle layer will be determined by the type of event, with a default value of green to catch any events that do not fall into any of those categories.

``` swift
let categoricalStops = ["earthquake" : MGLStyleValue(rawValue: UIColor.orange),
"explosion" : MGLStyleValue(rawValue: UIColor.red),
"quarry blast": MGLStyleValue(rawValue: UIColor.yellow)]

layer.circleColor = MGLStyleValue<UIColor>(interpolationMode: .categorical,
sourceStops: categoricalStops,
attributeName: "type",
options: [.defaultValue: MGLStyleValue(rawValue: UIColor.blue)])

```

![categorical mode](img/data-driven-styling/categorical1.png) ![categorical mode](img/data-driven-styling/categorical2.png)

#####Identity

`MGLInterpolationModeIdentity` uses the attribute’s value as the style value. For example, you can set the `circleRadius` to the earthquake’s magnitude. Since the attribute value itself will be used as the style value, `sourceStops` can be set to `nil`.

``` swift
layer.circleRadius = MGLStyleFunction<NSNumber>(interpolationMode: .identity,
sourceStops: nil,
attributeName: "mag",
options: [.defaultValue: MGLStyleValue<NSNumber>(rawValue: 0)])

```

![identity mode](img/data-driven-styling/identity.png)

_Insert Conclusion_



Resources:
[USGS](https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php)
2 changes: 1 addition & 1 deletion platform/darwin/docs/guides/For Style Authors.md.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
-%>
<!--
This file is generated.
Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`.
Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code`.
-->
# Information for Style Authors

Expand Down
11 changes: 11 additions & 0 deletions platform/darwin/scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ const layerH = ejs.compile(fs.readFileSync('platform/darwin/src/MGLStyleLayer.h.
const layerM = ejs.compile(fs.readFileSync('platform/darwin/src/MGLStyleLayer.mm.ejs', 'utf8'), { strict: true});
const testLayers = ejs.compile(fs.readFileSync('platform/darwin/test/MGLStyleLayerTests.mm.ejs', 'utf8'), { strict: true});
const guideMD = ejs.compile(fs.readFileSync('platform/darwin/docs/guides/For Style Authors.md.ejs', 'utf8'), { strict: true });
const ddsGuideMD = ejs.compile(fs.readFileSync('platform/darwin/docs/guides/Data-Driven Styling.md', 'utf8'), { strict: true });

const layers = _(spec.layer.type.values).map((value, layerType) => {
const layoutProperties = Object.keys(spec[`layout_${layerType}`]).reduce((memo, name) => {
Expand Down Expand Up @@ -614,3 +615,13 @@ fs.writeFileSync(`platform/macos/docs/guides/For Style Authors.md`, guideMD({
renamedProperties: renamedPropertiesByLayerType,
layers: layers,
}));
fs.writeFileSync(`platform/ios/docs/guides/Data-Driven Styling.md`, ddsGuideMD({
os: 'iOS',
renamedProperties: renamedPropertiesByLayerType,
layers: layers,
}));
fs.writeFileSync(`platform/macos/docs/guides/Data-Driven Styling.md`, ddsGuideMD({
os: 'macOS',
renamedProperties: renamedPropertiesByLayerType,
layers: layers,
}));
5 changes: 5 additions & 0 deletions platform/ios/docs/guides/Data-Driven Styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ To do - JK:
- [ ] Add cocoaprefix so this can be moved to darwin
---->

<!--
This file is generated.
Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`.
-->

##Data-Driven Styling

Mapbox’s data-driven styling features allow you to use data properties to style your maps. You can style objects within the same layer differently based on their individual attributes. This enables you to style icons, routes, parks, and more based on attributes.
Expand Down
119 changes: 115 additions & 4 deletions platform/ios/docs/guides/For Style Authors.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
This file is generated.
Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code-darwin`.
Edit platform/darwin/scripts/generate-style-code.js, then run `make style-code`.
-->
# Information for Style Authors

Expand Down Expand Up @@ -115,6 +115,7 @@ layer | style layer
property | attribute
SDF icon | template image
source | content source
function type | interpolation mode

## Specifying the map’s content

Expand Down Expand Up @@ -189,6 +190,28 @@ layer objects. The property names generally correspond to the style JSON
properties, except for the use of camelCase instead of kebab-case. Properties
whose names differ from the style specification are listed below:

### Circle style functions

The runtime styling API introduced `MGLStyleFunction` to the iOS SDK.
[Data-driven styling](data-driven-styling.html) expands `MGLStyleFunction` and
introduces `MGLInterpolationMode`. Individual style property documentation
includes which subclasses of `MGLStyleFunction` are enabled for that property.

In style specification | In the SDK
-----------------------------|-----------
`zoom function` | `MGLCameraStyleFunction`
`property function` | `MGLSourceStyleFunction`
`zoom-and-property functions`| `MGLCompositeStyleFunction`

### Circle interpolation mode

In style specification | In the SDK
-----------------------------|-----------
`exponential` | `MGLInterpolationModeExponential`
`interval` | `MGLInterpolationModeInterval`
`categorical` | `MGLInterpolationModeCategorical`
`identity` | `MGLInterpolationModeIdentity`

### Circle style layers

In style JSON | In Objective-C | In Swift
Expand All @@ -197,6 +220,28 @@ In style JSON | In Objective-C | In Swift
`circle-translate` | `MGLCircleStyleLayer.circleTranslation` | `MGLCircleStyleLayer.circleTranslation`
`circle-translate-anchor` | `MGLCircleStyleLayer.circleTranslationAnchor` | `MGLCircleStyleLayer.circleTranslationAnchor`

### Fill style functions

The runtime styling API introduced `MGLStyleFunction` to the iOS SDK.
[Data-driven styling](data-driven-styling.html) expands `MGLStyleFunction` and
introduces `MGLInterpolationMode`. Individual style property documentation
includes which subclasses of `MGLStyleFunction` are enabled for that property.

In style specification | In the SDK
-----------------------------|-----------
`zoom function` | `MGLCameraStyleFunction`
`property function` | `MGLSourceStyleFunction`
`zoom-and-property functions`| `MGLCompositeStyleFunction`

### Fill interpolation mode

In style specification | In the SDK
-----------------------------|-----------
`exponential` | `MGLInterpolationModeExponential`
`interval` | `MGLInterpolationModeInterval`
`categorical` | `MGLInterpolationModeCategorical`
`identity` | `MGLInterpolationModeIdentity`

### Fill style layers

In style JSON | In Objective-C | In Swift
Expand All @@ -205,6 +250,28 @@ In style JSON | In Objective-C | In Swift
`fill-translate` | `MGLFillStyleLayer.fillTranslation` | `MGLFillStyleLayer.fillTranslation`
`fill-translate-anchor` | `MGLFillStyleLayer.fillTranslationAnchor` | `MGLFillStyleLayer.fillTranslationAnchor`

### Line style functions

The runtime styling API introduced `MGLStyleFunction` to the iOS SDK.
[Data-driven styling](data-driven-styling.html) expands `MGLStyleFunction` and
introduces `MGLInterpolationMode`. Individual style property documentation
includes which subclasses of `MGLStyleFunction` are enabled for that property.

In style specification | In the SDK
-----------------------------|-----------
`zoom function` | `MGLCameraStyleFunction`
`property function` | `MGLSourceStyleFunction`
`zoom-and-property functions`| `MGLCompositeStyleFunction`

### Line interpolation mode

In style specification | In the SDK
-----------------------------|-----------
`exponential` | `MGLInterpolationModeExponential`
`interval` | `MGLInterpolationModeInterval`
`categorical` | `MGLInterpolationModeCategorical`
`identity` | `MGLInterpolationModeIdentity`

### Line style layers

In style JSON | In Objective-C | In Swift
Expand All @@ -213,6 +280,28 @@ In style JSON | In Objective-C | In Swift
`line-translate` | `MGLLineStyleLayer.lineTranslation` | `MGLLineStyleLayer.lineTranslation`
`line-translate-anchor` | `MGLLineStyleLayer.lineTranslationAnchor` | `MGLLineStyleLayer.lineTranslationAnchor`

### Raster style functions

The runtime styling API introduced `MGLStyleFunction` to the iOS SDK.
[Data-driven styling](data-driven-styling.html) expands `MGLStyleFunction` and
introduces `MGLInterpolationMode`. Individual style property documentation
includes which subclasses of `MGLStyleFunction` are enabled for that property.

In style specification | In the SDK
-----------------------------|-----------
`zoom function` | `MGLCameraStyleFunction`
`property function` | `MGLSourceStyleFunction`
`zoom-and-property functions`| `MGLCompositeStyleFunction`

### Raster interpolation mode

In style specification | In the SDK
-----------------------------|-----------
`exponential` | `MGLInterpolationModeExponential`
`interval` | `MGLInterpolationModeInterval`
`categorical` | `MGLInterpolationModeCategorical`
`identity` | `MGLInterpolationModeIdentity`

### Raster style layers

In style JSON | In Objective-C | In Swift
Expand All @@ -221,6 +310,28 @@ In style JSON | In Objective-C | In Swift
`raster-brightness-min` | `MGLRasterStyleLayer.minimumRasterBrightness` | `MGLRasterStyleLayer.minimumRasterBrightness`
`raster-hue-rotate` | `MGLRasterStyleLayer.rasterHueRotation` | `MGLRasterStyleLayer.rasterHueRotation`

### Symbol style functions

The runtime styling API introduced `MGLStyleFunction` to the iOS SDK.
[Data-driven styling](data-driven-styling.html) expands `MGLStyleFunction` and
introduces `MGLInterpolationMode`. Individual style property documentation
includes which subclasses of `MGLStyleFunction` are enabled for that property.

In style specification | In the SDK
-----------------------------|-----------
`zoom function` | `MGLCameraStyleFunction`
`property function` | `MGLSourceStyleFunction`
`zoom-and-property functions`| `MGLCompositeStyleFunction`

### Symbol interpolation mode

In style specification | In the SDK
-----------------------------|-----------
`exponential` | `MGLInterpolationModeExponential`
`interval` | `MGLInterpolationModeInterval`
`categorical` | `MGLInterpolationModeCategorical`
`identity` | `MGLInterpolationModeIdentity`

### Symbol style layers

In style JSON | In Objective-C | In Swift
Expand Down Expand Up @@ -253,9 +364,9 @@ In style JSON | In Objective-C | In Swift

Each property representing a layout or paint attribute is set to an
`MGLStyleValue` object, which is either an `MGLConstantStyleValue` object (for
constant values) or an `MGLStyleFunction` subclass. The style value object is a
container for the raw value or function parameters that you want the attribute
to be set to.
constant values) or an `MGLStyleFunction` object (for zoom level functions). The
style value object is a container for the raw value or function parameters that
you want the attribute to be set to.

In contrast to the JSON type that the style specification defines for each
layout or paint property, the style value object often contains a more specific
Expand Down
Loading

0 comments on commit ceabb7d

Please sign in to comment.