Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Create style json plugin #969

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
pluginVersion = [
checkstyle: '8.10.1',
gradle : '3.4.0',
kotlin : '1.3.20',
kotlin : '1.3.31',
dokka : '0.9.17'
]

Expand Down
1 change: 1 addition & 0 deletions plugin-stylejson/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
37 changes: 37 additions & 0 deletions plugin-stylejson/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
compileSdkVersion androidVersions.compileSdkVersion

defaultConfig {
minSdkVersion androidVersions.minSdkVersion
targetSdkVersion androidVersions.targetSdkVersion
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

configurations {
javadocDeps
}

lintOptions {
abortOnError false
}

testOptions {
unitTests.returnDefaultValues true
}
}

dependencies {
implementation dependenciesList.kotlin
implementation dependenciesList.mapboxMapSdk
javadocDeps dependenciesList.mapboxMapSdk
testImplementation dependenciesList.junit
testImplementation dependenciesList.mockito
}

apply from: "${rootDir}/gradle/javadoc.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
apply from: "${rootDir}/gradle/checkstyle.gradle"
5 changes: 5 additions & 0 deletions plugin-stylejson/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
VERSION_NAME=0.1.0-SNAPSHOT
POM_ARTIFACT_ID=mapbox-android-plugin-stylejson
POM_NAME=Mapbox Android StyleJson Plugin
POM_DESCRIPTION=Mapbox Android StyleJson Plugin
POM_PACKAGING=aar
21 changes: 21 additions & 0 deletions plugin-stylejson/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions plugin-stylejson/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapbox.pluginstylejson" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.mapbox.pluginstylejson

import com.google.gson.JsonObject
import com.mapbox.mapboxsdk.style.layers.*

class LayerJsonParser {

internal fun parserBackgroundLayer(id: String, json: JsonObject): BackgroundLayer {
return BackgroundLayer(id)
}

internal fun parserCircleLayer(id: String, json: JsonObject): CircleLayer {
return CircleLayer(id, "")
}

internal fun parserCustomLayer(id: String, json: JsonObject): CustomLayer {
return CustomLayer(id, 0)
}

internal fun parserFillExtrusionLayer(id: String, json: JsonObject): FillExtrusionLayer {
return FillExtrusionLayer(id, "")
}

internal fun parserFillLayer(id: String, json: JsonObject): FillLayer {
return FillLayer(id, "")
}

internal fun parserHeatmapLayer(id: String, json: JsonObject): HeatmapLayer {
return HeatmapLayer(id, "")
}

internal fun parserHillshadeLayer(id: String, json: JsonObject): HillshadeLayer {
return HillshadeLayer(id, "")
}

internal fun parserLineLayer(id: String, json: JsonObject): LineLayer {
return LineLayer(id, "")
}

internal fun parserRasterLayer(id: String, json: JsonObject): RasterLayer {
return RasterLayer(id, "")
}

internal fun parserSymbolLayer(id: String, json: JsonObject): SymbolLayer {
return SymbolLayer(id, "")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.mapbox.pluginstylejson

import com.google.gson.JsonObject
import com.mapbox.geojson.*
import com.mapbox.mapboxsdk.style.sources.*
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_FEATURE
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_FEATURE_COLLECTION
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_GEOMETRY_COLLECTION
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_LINE_STRING
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_MULTI_POINT
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_MULTI_POLYGON
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_NULTI_LINE_STRING
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_POINT
import com.mapbox.pluginstylejson.StyleJsonContants.GEO_JSON_POLYGON

class SourceJsonParser {

internal fun parserGeoJsonSource(id: String, json: JsonObject): GeoJsonSource {
val geoJsonSource = GeoJsonSource(id)
if (json.has("data")) {
val data = json.getAsJsonObject("data")
val dataString = data.asString
if (dataString.startsWith("http")) {
geoJsonSource.url = dataString
} else {
val type = data.get("type").asString
when (type) {
GEO_JSON_FEATURE -> geoJsonSource.setGeoJson(Feature.fromJson(dataString))
GEO_JSON_FEATURE_COLLECTION -> geoJsonSource.setGeoJson(FeatureCollection.fromJson(dataString))
GEO_JSON_GEOMETRY_COLLECTION -> geoJsonSource.setGeoJson(GeometryCollection.fromJson(dataString))
GEO_JSON_LINE_STRING -> geoJsonSource.setGeoJson(LineString.fromJson(dataString))
GEO_JSON_NULTI_LINE_STRING -> geoJsonSource.setGeoJson(MultiLineString.fromJson(dataString))
GEO_JSON_MULTI_POINT -> geoJsonSource.setGeoJson(MultiPoint.fromJson(dataString))
GEO_JSON_MULTI_POLYGON -> geoJsonSource.setGeoJson(MultiPolygon.fromJson(dataString))
GEO_JSON_POINT -> geoJsonSource.setGeoJson(Point.fromJson(dataString))
GEO_JSON_POLYGON -> geoJsonSource.setGeoJson(Polygon.fromJson(dataString))
}
}
}

return geoJsonSource
}

internal fun parserVectorSource(id: String, json: JsonObject): VectorSource {
return VectorSource(id, "")
}

internal fun parserCustomGeometrySource(id: String, json: JsonObject): CustomGeometrySource {
return CustomGeometrySource(id, null)
}

internal fun parserImageSource(id: String, json: JsonObject): ImageSource {
return ImageSource(id, null, 0)
}

internal fun parserRasterDemSource(id: String, json: JsonObject): RasterDemSource {
return RasterDemSource(id, "")
}

internal fun parserRasterSource(id: String, json: JsonObject): RasterSource {
return RasterSource(id, "")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mapbox.pluginstylejson

object StyleJsonContants {
val LAYER_BACKGROUND = "background"
val LAYER_CYCLE = "cycle"
val LAYER_CUSTOM = "custom"
val LAYER_FILL_EXTRUSION = "fillextrusion"
val LAYER_FILL = "fill"
val LAYER_HEATMAP = "heatmap"
val LAYER_HILLSHADE = "hillshade"
val LAYER_LINE = "line"
val LAYER_RASTER = "raster"
val LAYER_SYMBOL = "symbol"
val SOURCE_CUSTOM_GEOMETRY = "custom geometry"
val SOURCE_GEO_JSON = "geojson"
val SOURCE_IMAGE = "image"
val SOURCE_RASTER_DEM = "rasterdem"
val SOURCE_RASTER = "raster"
val SOURCE_VECTOR = "vector"

const val GEO_JSON_FEATURE = "Feature"
const val GEO_JSON_FEATURE_COLLECTION = "FeatureCollection"
const val GEO_JSON_GEOMETRY_COLLECTION = "GeometryCollection"
const val GEO_JSON_LINE_STRING = "LineString"
const val GEO_JSON_NULTI_LINE_STRING = "MultiLineString"
const val GEO_JSON_MULTI_POINT = "MultiPoint"
const val GEO_JSON_MULTI_POLYGON = "MultiPolygon"
const val GEO_JSON_POINT = "Point"
const val GEO_JSON_POLYGON = "Polygon"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.mapbox.pluginstylejson

import com.google.gson.Gson
import com.google.gson.JsonObject
import com.mapbox.mapboxsdk.style.layers.Layer
import com.mapbox.mapboxsdk.style.sources.Source
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_BACKGROUND
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_CUSTOM
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_CYCLE
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_FILL
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_FILL_EXTRUSION
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_HEATMAP
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_HILLSHADE
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_LINE
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_RASTER
import com.mapbox.pluginstylejson.StyleJsonContants.LAYER_SYMBOL
import com.mapbox.pluginstylejson.StyleJsonContants.SOURCE_CUSTOM_GEOMETRY
import com.mapbox.pluginstylejson.StyleJsonContants.SOURCE_GEO_JSON
import com.mapbox.pluginstylejson.StyleJsonContants.SOURCE_IMAGE
import com.mapbox.pluginstylejson.StyleJsonContants.SOURCE_RASTER
import com.mapbox.pluginstylejson.StyleJsonContants.SOURCE_RASTER_DEM
import com.mapbox.pluginstylejson.StyleJsonContants.SOURCE_VECTOR

class StyleJsonPlugin {
private val gson = Gson()
private val layerJsonParser = LayerJsonParser()
private val sourceJsonParser = SourceJsonParser()

fun parserLayer(content: String): Layer? {
val source = gson.fromJson<JsonObject>(content, JsonObject::class.java)
val id = source.get("id")
id?.let {
val type = source.get("type")
type?.let {
return when (type.asString) {
LAYER_BACKGROUND -> layerJsonParser.parserBackgroundLayer(id.asString, source)
LAYER_CYCLE -> layerJsonParser.parserCircleLayer(id.asString, source)
LAYER_CUSTOM -> layerJsonParser.parserCustomLayer(id.asString, source)
LAYER_FILL_EXTRUSION -> layerJsonParser.parserFillExtrusionLayer(id.asString, source)
LAYER_FILL -> layerJsonParser.parserFillLayer(id.asString, source)
LAYER_HEATMAP -> layerJsonParser.parserHeatmapLayer(id.asString, source)
LAYER_HILLSHADE -> layerJsonParser.parserHillshadeLayer(id.asString, source)
LAYER_LINE -> layerJsonParser.parserLineLayer(id.asString, source)
LAYER_RASTER -> layerJsonParser.parserRasterLayer(id.asString, source)
LAYER_SYMBOL -> layerJsonParser.parserSymbolLayer(id.asString, source)
else -> null
}
}
}
return null
}

fun parserSource(id: String, content: String): Source? {
val source = gson.fromJson<JsonObject>(content, JsonObject::class.java)
val type = source.get("type")
type?.let {
return when (type.asString) {
SOURCE_CUSTOM_GEOMETRY -> sourceJsonParser.parserCustomGeometrySource(id, source)
SOURCE_GEO_JSON -> sourceJsonParser.parserGeoJsonSource(id, source)
SOURCE_IMAGE -> sourceJsonParser.parserImageSource(id, source)
SOURCE_RASTER_DEM -> sourceJsonParser.parserRasterDemSource(id, source)
SOURCE_RASTER -> sourceJsonParser.parserRasterSource(id, source)
SOURCE_VECTOR -> sourceJsonParser.parserVectorSource(id, source)
else -> null
}
}
return null
}

}
3 changes: 3 additions & 0 deletions plugin-stylejson/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">pluginstylejson</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mapbox.pluginstylejson;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ include ':plugin-localization'
include ':plugin-annotation'
include ':plugin-markerview'
include ':ktx-mapbox-maps'
include ':plugin-scalebar'
include ':plugin-scalebar'
include ':plugin-stylejson'