Skip to content

Commit

Permalink
add StyleJsonPlugin.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin committed Jun 3, 2019
1 parent 68234f6 commit 8483e57
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 10 deletions.
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
Expand Up @@ -3,8 +3,7 @@ package com.mapbox.pluginstylejson
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.mapbox.geojson.*
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.mapbox.mapboxsdk.style.sources.VectorSource
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
Expand All @@ -15,14 +14,13 @@ 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 StyleJsonParser {
private val gson = Gson()
class SourceJsonParser {
private val gson = Gson()

internal fun parserGeoJsonSource(id: String, json: String): GeoJsonSource {
internal fun parserGeoJsonSource(id: String, json: JsonObject): GeoJsonSource {
val geoJsonSource = GeoJsonSource(id)
val source = gson.fromJson<JsonObject>(json, JsonObject::class.java)
if (source.has("data")) {
val data = source.getAsJsonObject("data")
if (json.has("data")) {
val data = json.getAsJsonObject("data")
val dataString = data.asString
if (dataString.startsWith("http")) {
geoJsonSource.url = dataString
Expand All @@ -45,7 +43,23 @@ class StyleJsonParser {
return geoJsonSource
}

internal fun parserVectorSource(id: String, json: String): VectorSource {
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
Expand Up @@ -16,7 +16,6 @@ object StyleJsonContants {
val SOURCE_IMAGE = "image"
val SOURCE_RASTER_DEM = "rasterdem"
val SOURCE_RASTER = "raster"
val SOURCE_UNKNOW = "unknow"
val SOURCE_VECTOR = "vector"

const val GEO_JSON_FEATURE = "Feature"
Expand Down
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
}

}

0 comments on commit 8483e57

Please sign in to comment.