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

Fix: Update of generate.dart and its template files. Fixed Offset, Translate and expressions arrays on iOS. #481

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import java.util.List;
import java.util.Map;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;


import static org.maplibre.maplibregl.Convert.toMap;

class LayerPropertyConverter {
Expand Down Expand Up @@ -44,7 +44,16 @@ static PropertyValue[] interpretSymbolLayerProperties(Object o) {
properties.add(PropertyFactory.iconHaloBlur(expression));
break;
case "icon-translate":
properties.add(PropertyFactory.iconTranslate(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.iconTranslate(floatArray));
} else {
properties.add(PropertyFactory.iconTranslate(expression));
}
} else {
properties.add(PropertyFactory.iconTranslate(expression));
}
break;
case "icon-translate-anchor":
properties.add(PropertyFactory.iconTranslateAnchor(expression));
Expand All @@ -65,7 +74,16 @@ static PropertyValue[] interpretSymbolLayerProperties(Object o) {
properties.add(PropertyFactory.textHaloBlur(expression));
break;
case "text-translate":
properties.add(PropertyFactory.textTranslate(expression));
if (jsonElement.isJsonArray()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put all this logic into convertJsonToFloatArray to avoid repetition?..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't remember why I did this, it was a while ago. Maybe because in some cases the conversion was handled differently, but currently it seems logical to put everything in convertJsonToFloatArray.

final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.textTranslate(floatArray));
} else {
properties.add(PropertyFactory.textTranslate(expression));
}
} else {
properties.add(PropertyFactory.textTranslate(expression));
}
break;
case "text-translate-anchor":
properties.add(PropertyFactory.textTranslateAnchor(expression));
Expand Down Expand Up @@ -104,12 +122,21 @@ static PropertyValue[] interpretSymbolLayerProperties(Object o) {
properties.add(PropertyFactory.iconTextFit(expression));
break;
case "icon-text-fit-padding":
properties.add(PropertyFactory.iconTextFitPadding(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.iconTextFitPadding(floatArray));
} else {
properties.add(PropertyFactory.iconTextFitPadding(expression));
}
} else {
properties.add(PropertyFactory.iconTextFitPadding(expression));
}
break;
case "icon-image":
if(jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()){
if (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()) {
properties.add(PropertyFactory.iconImage(jsonElement.getAsString()));
}else{
} else {
properties.add(PropertyFactory.iconImage(expression));
}
break;
Expand All @@ -123,7 +150,16 @@ static PropertyValue[] interpretSymbolLayerProperties(Object o) {
properties.add(PropertyFactory.iconKeepUpright(expression));
break;
case "icon-offset":
properties.add(PropertyFactory.iconOffset(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.iconOffset(floatArray));
} else {
properties.add(PropertyFactory.iconOffset(expression));
}
} else {
properties.add(PropertyFactory.iconOffset(expression));
}
break;
case "icon-anchor":
properties.add(PropertyFactory.iconAnchor(expression));
Expand Down Expand Up @@ -186,7 +222,16 @@ static PropertyValue[] interpretSymbolLayerProperties(Object o) {
properties.add(PropertyFactory.textTransform(expression));
break;
case "text-offset":
properties.add(PropertyFactory.textOffset(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.textOffset(floatArray));
} else {
properties.add(PropertyFactory.textOffset(expression));
}
} else {
properties.add(PropertyFactory.textOffset(expression));
}
break;
case "text-allow-overlap":
properties.add(PropertyFactory.textAllowOverlap(expression));
Expand Down Expand Up @@ -230,7 +275,16 @@ static PropertyValue[] interpretCircleLayerProperties(Object o) {
properties.add(PropertyFactory.circleOpacity(expression));
break;
case "circle-translate":
properties.add(PropertyFactory.circleTranslate(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.circleTranslate(floatArray));
} else {
properties.add(PropertyFactory.circleTranslate(expression));
}
} else {
properties.add(PropertyFactory.circleTranslate(expression));
}
break;
case "circle-translate-anchor":
properties.add(PropertyFactory.circleTranslateAnchor(expression));
Expand Down Expand Up @@ -280,7 +334,16 @@ static PropertyValue[] interpretLineLayerProperties(Object o) {
properties.add(PropertyFactory.lineColor(expression));
break;
case "line-translate":
properties.add(PropertyFactory.lineTranslate(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.lineTranslate(floatArray));
} else {
properties.add(PropertyFactory.lineTranslate(expression));
}
} else {
properties.add(PropertyFactory.lineTranslate(expression));
}
break;
case "line-translate-anchor":
properties.add(PropertyFactory.lineTranslateAnchor(expression));
Expand All @@ -298,7 +361,16 @@ static PropertyValue[] interpretLineLayerProperties(Object o) {
properties.add(PropertyFactory.lineBlur(expression));
break;
case "line-dasharray":
properties.add(PropertyFactory.lineDasharray(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.lineDasharray(floatArray));
} else {
properties.add(PropertyFactory.lineDasharray(expression));
}
} else {
properties.add(PropertyFactory.lineDasharray(expression));
}
break;
case "line-pattern":
properties.add(PropertyFactory.linePattern(expression));
Expand Down Expand Up @@ -354,7 +426,16 @@ static PropertyValue[] interpretFillLayerProperties(Object o) {
properties.add(PropertyFactory.fillOutlineColor(expression));
break;
case "fill-translate":
properties.add(PropertyFactory.fillTranslate(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.fillTranslate(floatArray));
} else {
properties.add(PropertyFactory.fillTranslate(expression));
}
} else {
properties.add(PropertyFactory.fillTranslate(expression));
}
break;
case "fill-translate-anchor":
properties.add(PropertyFactory.fillTranslateAnchor(expression));
Expand Down Expand Up @@ -392,7 +473,16 @@ static PropertyValue[] interpretFillExtrusionLayerProperties(Object o) {
properties.add(PropertyFactory.fillExtrusionColor(expression));
break;
case "fill-extrusion-translate":
properties.add(PropertyFactory.fillExtrusionTranslate(expression));
if (jsonElement.isJsonArray()) {
final Float[] floatArray = convertJsonToFloatArray(jsonElement);
if (floatArray != null) {
properties.add(PropertyFactory.fillExtrusionTranslate(floatArray));
} else {
properties.add(PropertyFactory.fillExtrusionTranslate(expression));
}
} else {
properties.add(PropertyFactory.fillExtrusionTranslate(expression));
}
break;
case "fill-extrusion-translate-anchor":
properties.add(PropertyFactory.fillExtrusionTranslateAnchor(expression));
Expand Down Expand Up @@ -537,4 +627,21 @@ static PropertyValue[] interpretHeatmapLayerProperties(Object o) {
return properties.toArray(new PropertyValue[properties.size()]);
}

private static boolean isNumber(JsonElement element) {
return element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber();
}

private static Float[] convertJsonToFloatArray(JsonElement jsonElement) {
final JsonArray jsonArray = jsonElement.getAsJsonArray();
Float[] floatArray = new Float[jsonArray.size()];

for (int i = 0; i < jsonArray.size(); i++) {
if (jsonArray.get(i).isJsonPrimitive() && jsonArray.get(i).getAsJsonPrimitive().isNumber()) {
floatArray[i] = jsonArray.get(i).getAsFloat();
} else {
return null;
}
}
return floatArray;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ class LayerPropertyConverter {

private class func interpretExpression(propertyName: String, expression: String) -> NSExpression? {
let isColor = propertyName.contains("color");
let isOffset = propertyName.contains("offset");
let isTranslate = propertyName.contains("translate");

do {
let json = try JSONSerialization.jsonObject(with: expression.data(using: .utf8)!, options: .fragmentsAllowed)
Expand All @@ -364,20 +366,35 @@ class LayerPropertyConverter {
return NSExpression(forConstantValue: UIColor(hexString: color))
}
}
// this is required because NSExpression.init(mglJSONObject: json) fails to create
// a proper Expression if the data of a literal is an array

if let offset = json as? [Any]{
// checks on the value of property that are literal expressions
if offset.count == 2 && offset.first is String && offset.first as? String == "literal" {
if let vector = offset.last as? [Any]{
if(vector.count == 2) {
if let x = vector.first as? Double, let y = vector.last as? Double {
return NSExpression(forConstantValue: NSValue(cgVector: CGVector(dx: x, dy: y)))
if isOffset || isTranslate {
// this is required because NSExpression.init(mglJSONObject: json) fails to create
// a proper Expression if the data of a literal is an array destined for a CGVector
if let x = vector.first as? Double, let y = vector.last as? Double {
return NSExpression(forConstantValue: NSValue(cgVector: CGVector(dx: x, dy: y)))
}
}

return NSExpression.init(mglJSONObject: json)
}
}
// checks on the value of properties that are arrays
} else if offset.count == 2, let x = offset.first as? Double, let y = offset.last as? Double {
if isOffset || isTranslate {
// this is required because NSExpression.init(mglJSONObject: json) fails to create
// a proper Expression if the data of an array is destined for a CGVector
return NSExpression(forConstantValue: NSValue(cgVector: CGVector(dx: x, dy: y)))
}
// this is required because NSExpression.init(mglJSONObject: json) fails to create
// a proper Expression if the data is an array of double
return NSExpression(forConstantValue: [NSNumber(value: x), NSNumber(value: y)])
}
}

return NSExpression.init(mglJSONObject: json)
} catch {
}
Expand Down
22 changes: 16 additions & 6 deletions scripts/lib/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import 'package:recase/recase.dart';
import 'conversions.dart';

main() async {
final styleJson =
jsonDecode(await File('scripts/input/style.json').readAsString());
final currentPath = Directory.current.path;
final styleFilePath = '$currentPath/input/style.json';
final styleJson = jsonDecode(await File(styleFilePath).readAsString());

final layerTypes = [
"symbol",
Expand All @@ -20,6 +21,7 @@ main() async {
"hillshade",
"heatmap",
];

final sourceTypes = [
"vector",
"raster",
Expand Down Expand Up @@ -58,8 +60,8 @@ main() async {
}.map((p) => {"property": p}).toList();

const templates = [
"maplibre_gl/android/src/main/java/com/mapbox/mapboxgl/LayerPropertyConverter.java",
"maplibre_gl/ios/Classes/LayerPropertyConverter.swift",
"maplibre_gl/android/src/main/java/org/maplibre/maplibregl/LayerPropertyConverter.java",
"maplibre_gl/ios/maplibre_gl/Sources/maplibre_gl/LayerPropertyConverter.swift",
"maplibre_gl/lib/src/layer_expressions.dart",
"maplibre_gl/lib/src/layer_properties.dart",
"maplibre_gl_web/lib/src/layer_tools.dart",
Expand All @@ -75,13 +77,16 @@ Future<void> render(
Map<String, List> renderContext,
String path,
) async {
final currentParentPath = Directory.current.parent.path;

final pathItems = path.split("/");
final filename = pathItems.removeLast();
final outputPath = pathItems.join("/");
final outputPath = '$currentParentPath/${pathItems.join("/")}';

print("Rendering $filename");
final templateFile =
await File('./scripts/templates/$filename.template').readAsString();
await File('$currentParentPath/scripts/templates/$filename.template')
.readAsString();

final template = Template(templateFile);
final outputFile = File('$outputPath/$filename');
Expand All @@ -98,9 +103,14 @@ List<Map<String, dynamic>> buildStyleProperties(

Map<String, dynamic> buildStyleProperty(
String key, Map<String, dynamic> value) {
final typeDart = dartTypeMappingTable[value["type"]];
final nestedTypeDart = dartTypeMappingTable[value["value"]] ??
dartTypeMappingTable[value["value"]?["type"]];
final camelCase = ReCase(key).camelCase;

return <String, dynamic>{
'value': key,
'isFloatArrayProperty': typeDart == "List" && nestedTypeDart == "double",
'isVisibilityProperty': key == "visibility",
'requiresLiteral': key == "icon-image",
'isIosAsCamelCase': renamedIosProperties.containsKey(camelCase),
Expand Down
Loading
Loading