Furhter work on MapStyle
Accidentally left untracked by mistake earlier
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
package eu.konggdev.strikemaps.map.layer;
|
||||||
|
|
||||||
|
import org.maplibre.android.style.layers.Layer;
|
||||||
|
import org.maplibre.android.style.sources.GeoJsonSource;
|
||||||
|
|
||||||
|
//FIXME: Get rid of MapLibre reliance
|
||||||
|
public class MapLayer {
|
||||||
|
public GeoJsonSource source;
|
||||||
|
public Layer layer;
|
||||||
|
public MapLayer(GeoJsonSource source, Layer layer) {
|
||||||
|
this.source = source;
|
||||||
|
this.layer = layer;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +1,27 @@
|
|||||||
package eu.konggdev.strikemaps.map.style;
|
package eu.konggdev.strikemaps.map.style;
|
||||||
|
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import eu.konggdev.strikemaps.app.AppController;
|
import eu.konggdev.strikemaps.app.AppController;
|
||||||
import eu.konggdev.strikemaps.helper.FileHelper;
|
import eu.konggdev.strikemaps.helper.FileHelper;
|
||||||
import eu.konggdev.strikemaps.map.layer.MapLayer;
|
|
||||||
import eu.konggdev.strikemaps.map.source.MapSource;
|
import eu.konggdev.strikemaps.map.source.MapSource;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
public class MapStyle {
|
public class MapStyle {
|
||||||
public int version;
|
//Only local data
|
||||||
public String name;
|
public String name;
|
||||||
public String icon;
|
public Bitmap icon;
|
||||||
public String description;
|
|
||||||
|
|
||||||
|
public JsonNode metadata; // everything except layers + sources
|
||||||
public Map<String, MapSource> sources;
|
public Map<String, MapSource> sources;
|
||||||
public List<MapLayer> layers;
|
public JsonNode layerDefinitions; // the "layers" array
|
||||||
|
|
||||||
public JsonNode raw;
|
|
||||||
|
|
||||||
//FIXME
|
//FIXME
|
||||||
public static MapStyle fromMapLibreJsonFile(String filename, AppController app) {
|
public static MapStyle fromMapLibreJsonFile(String filename, AppController app) {
|
||||||
@@ -30,34 +32,38 @@ public class MapStyle {
|
|||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
JsonNode root = mapper.readTree(styleContents);
|
JsonNode root = mapper.readTree(styleContents);
|
||||||
MapStyle style = new MapStyle();
|
|
||||||
|
|
||||||
style.version = root.path("version").asInt();
|
MapStyle style = new MapStyle();
|
||||||
style.name = root.path("name").asText();
|
style.name = root.path("name").asText();
|
||||||
style.icon = root.path("icon").asText();
|
style.icon = getIcon(root.path("icon").asText(), app);
|
||||||
style.description = root.path("description").asText();
|
|
||||||
|
|
||||||
style.sources = mapper.convertValue(
|
style.sources = mapper.convertValue(
|
||||||
root.path("sources"),
|
root.path("sources"),
|
||||||
new TypeReference<Map<String, MapSource>>() {}
|
new TypeReference<Map<String, MapSource>>() {}
|
||||||
);
|
);
|
||||||
|
|
||||||
style.layers = new ArrayList<>();
|
style.layerDefinitions = root.path("layers");
|
||||||
for (JsonNode layerNode : root.path("layers")) {
|
|
||||||
|
|
||||||
MapLayer layer = mapper.treeToValue(layerNode, MapLayer.class);
|
ObjectNode metadata = root.deepCopy();
|
||||||
|
metadata.remove("layers");
|
||||||
layer.raw = layerNode; // IMPORTANT
|
metadata.remove("sources");
|
||||||
|
style.metadata = metadata;
|
||||||
style.layers.add(layer);
|
|
||||||
}
|
|
||||||
|
|
||||||
style.raw = root; // full backup
|
|
||||||
|
|
||||||
return style;
|
return style;
|
||||||
} catch ( Exception e ) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Bitmap getIcon(String iconLocator, AppController app) {
|
||||||
|
switch(iconLocator.split("//")[0]) {
|
||||||
|
//TODO: https
|
||||||
|
case "assets:":
|
||||||
|
return BitmapFactory.decodeStream(FileHelper.openAssetStream("bundled/icon/" + iconLocator.split("//")[1], app));
|
||||||
|
default:
|
||||||
|
app.logcat("Unimplemented icon locator space: " + iconLocator);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user