Skip to content
Open
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: 2 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ MLN_LAYER_PLUGIN_HEADERS = [
"src/mbgl/plugin/plugin_layer_impl.hpp",
"src/mbgl/plugin/plugin_layer_render.hpp",
"src/mbgl/plugin/plugin_layer_properties.hpp",
"src/mbgl/plugin/plugin_style_filter.hpp",
]

MLN_LAYER_PLUGIN_SOURCE = [
Expand All @@ -12,6 +13,7 @@ MLN_LAYER_PLUGIN_SOURCE = [
"src/mbgl/plugin/plugin_layer_impl.cpp",
"src/mbgl/plugin/plugin_layer_render.cpp",
"src/mbgl/plugin/plugin_layer_properties.cpp",
"src/mbgl/plugin/plugin_style_filter.cpp",
]

MLN_PUBLIC_GENERATED_STYLE_HEADERS = [
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace style {
class Light;
class Source;
class Layer;
class PluginStyleFilter;

class Style {
public:
Expand Down Expand Up @@ -71,6 +72,9 @@ class Style {
void addLayer(std::unique_ptr<Layer>, const std::optional<std::string>& beforeLayerID = std::nullopt);
std::unique_ptr<Layer> removeLayer(const std::string& layerID);

// Add style parsing filter
void addStyleFilter(std::shared_ptr<mbgl::style::PluginStyleFilter>);

// Private implementation
class Impl;
const std::unique_ptr<Impl> impl;
Expand Down
2 changes: 2 additions & 0 deletions platform/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ objc_library(
"//platform/darwin:app/CustomStyleLayerExample.m",
"//platform/darwin:app/PluginLayerExample.h",
"//platform/darwin:app/PluginLayerExample.mm",
"//platform/darwin:app/StyleFilterExample.h",
"//platform/darwin:app/StyleFilterExample.mm",
"//platform/darwin:app/PluginLayerExampleMetalRendering.h",
"//platform/darwin:app/PluginLayerExampleMetalRendering.mm",
"//platform/ios:ios_app_srcs",
Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ exports_files(
"app/PluginLayerTestStyle.json",
"app/PluginLayerExample.h",
"app/PluginLayerExample.mm",
"app/StyleFilterExample.h",
"app/StyleFilterExample.mm",
"app/PluginLayerExampleMetalRendering.h",
"app/PluginLayerExampleMetalRendering.mm",
"test/amsterdam.geojson",
Expand Down
16 changes: 16 additions & 0 deletions platform/darwin/app/StyleFilterExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// StyleFilterExample.h
// MapLibre
//
// Created by Malcolm Toon on 7/25/25.
//

#import "MLNStyleFilter.h"

NS_ASSUME_NONNULL_BEGIN

@interface StyleFilterExample : MLNStyleFilter

@end

NS_ASSUME_NONNULL_END
60 changes: 60 additions & 0 deletions platform/darwin/app/StyleFilterExample.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#import "StyleFilterExample.h"

@implementation StyleFilterExample

// This will filter the data passed in
-(NSData *)filterData:(NSData *)data {
// Don't call super

// This example will remove any layer that has "metal-rendering-layer" in the id

// Parse the JSON: Make the containers mutable
NSError *error = nil;
NSMutableDictionary *styleDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];

NSData *tempResult = data;
if (styleDictionary) {

// Get the layer array
NSMutableArray *layerArray = [styleDictionary objectForKey:@"layers"];

// Create an array to hold which objects to remove since we can't remove them in the loop
NSMutableArray *removedLayers = [NSMutableArray array];

// Loop the layers and look for any layers that have the search string in them
for (NSMutableDictionary *layer in layerArray) {
NSString *layerID = [layer objectForKey:@"id"];

// If we find the layers we're looking for, add them to the list of layers to remove
if ([layerID containsString:@"metal-rendering-layer"]) {
[removedLayers addObject:layer];
}
}

// Go through and remove any layers that were found
for (NSMutableDictionary *l in removedLayers) {
[layerArray removeObject:l];
}

// Re-create the JSON, this time the layers we filtered out won't be there
NSData *filteredStyleJSON = [NSJSONSerialization dataWithJSONObject:styleDictionary
options:0
error:&error];

// If the JSON write is successful, then set the output to the new json style
if (filteredStyleJSON) {
tempResult = filteredStyleJSON;
}

}

// Return the data
return tempResult;

}



@end
4 changes: 3 additions & 1 deletion platform/darwin/bazel/files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ MLN_DARWIN_OBJC_HEADERS = [
"src/NSValue+MLNAdditions.h",
"src/MLNPluginLayer.h",
"src/MLNPluginStyleLayer.h",
"src/MLNNetworkResponse.h",
"src/MLNStyleFilter.h",
"src/MLNStyleFilter_Private.h",
]

MLN_DARWIN_OBJCPP_HEADERS = [
Expand Down Expand Up @@ -226,6 +227,7 @@ MLN_DARWIN_PUBLIC_OBJCPP_SOURCE = [
"src/MLNPluginLayer.mm",
"src/MLNPluginStyleLayer.mm",
"src/MLNNetworkResponse.mm",
"src/MLNStyleFilter.mm",
]
MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE = [
"src/MLNCustomDrawableStyleLayer_Private.h",
Expand Down
12 changes: 12 additions & 0 deletions platform/darwin/src/MLNStyleFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MLNStyleFilter : NSObject

// This will filter the data passed in
-(NSData *)filterData:(NSData *)data;

@end

NS_ASSUME_NONNULL_END
23 changes: 23 additions & 0 deletions platform/darwin/src/MLNStyleFilter.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#import "MLNStyleFilter.h"
#include <mbgl/plugin/plugin_style_filter.hpp>

@interface MLNStyleFilter () {
std::shared_ptr<mbgl::style::PluginStyleFilter> _coreFilter;
}

@end

@implementation MLNStyleFilter

-(NSData *)filterData:(NSData *)data {
// Base class does nothing but return the same data passed in
return data;
}

// Private
-(void)setFilter:(std::shared_ptr<mbgl::style::PluginStyleFilter>)filter {
_coreFilter = filter;
}


@end
14 changes: 14 additions & 0 deletions platform/darwin/src/MLNStyleFilter_Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#ifndef MLNStyleFilter_Private_h
#define MLNStyleFilter_Private_h

#import "MLNStyleFilter.h"
#include <mbgl/plugin/plugin_style_filter.hpp>

@interface MLNStyleFilter(Private)

-(void)setFilter:(std::shared_ptr<mbgl::style::PluginStyleFilter>)filter;

@end

#endif /* MLNStyleFilter_Private_h */
2 changes: 2 additions & 0 deletions platform/ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "PluginLayerExample.h"
#import "PluginLayerExampleMetalRendering.h"
#import "MLNPluginStyleLayer.h"
#import "StyleFilterExample.h"

static const CLLocationCoordinate2D WorldTourDestinations[] = {
{ .latitude = 38.8999418, .longitude = -77.033996 },
Expand Down Expand Up @@ -281,6 +282,7 @@ -(void)addPluginLayers {

[self.mapView addPluginLayerType:[PluginLayerExample class]];
[self.mapView addPluginLayerType:[PluginLayerExampleMetalRendering class]];
[self.mapView addStyleFilter:[[StyleFilterExample alloc] init]];

}

Expand Down
6 changes: 6 additions & 0 deletions platform/ios/src/MLNMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
@class MLNScaleBar;
@class MLNShape;
@class MLNPluginLayer;
@class MLNStyleFilter;

@protocol MLNMapViewDelegate;
@protocol MLNAnnotation;
Expand Down Expand Up @@ -2322,6 +2323,11 @@ of north, the map will automatically snap to exact north.
*/
- (void)addPluginLayerType:(Class)pluginLayerClass;

/**
Adds a style filter to the map view
*/
-(void)addStyleFilter:(MLNStyleFilter *)styleFilter;

@end

NS_ASSUME_NONNULL_END
42 changes: 42 additions & 0 deletions platform/ios/src/MLNMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <mbgl/plugin/plugin_layer_factory.hpp>
#include <mbgl/plugin/plugin_layer.hpp>
#include <mbgl/plugin/plugin_layer_impl.hpp>
#include <mbgl/plugin/plugin_style_filter.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/mtl/mtl_fwd.hpp>
#include <mbgl/mtl/render_pass.hpp>
Expand Down Expand Up @@ -82,6 +83,8 @@
#import "MLNPluginLayer.h"
#import "MLNStyleLayerManager.h"
#include "MLNPluginStyleLayer_Private.h"
#include "MLNStyleFilter.h"
#include "MLNStyleFilter_Private.h"

#include <algorithm>
#include <cstdlib>
Expand Down Expand Up @@ -452,6 +455,9 @@ @interface MLNMapView () <UIGestureRecognizerDelegate,
// Plugin Layers
@property NSMutableArray *pluginLayers;

// Style Filters
@property NSMutableArray *styleFilters;

@end

@implementation MLNMapView
Expand Down Expand Up @@ -7851,6 +7857,42 @@ -(void)addPluginLayerType:(Class)pluginLayerClass {

}

/**
Adds a style filter to the map view
*/
-(void)addStyleFilter:(MLNStyleFilter *)styleFilter {

if (!self.styleFilters) {
self.styleFilters = [NSMutableArray array];
}
[self.styleFilters addObject:styleFilter];

auto coreStyleFilter = std::make_shared<mbgl::style::PluginStyleFilter>();
coreStyleFilter->_filterStyleFunction = [styleFilter](const std::string &filterData) -> const std::string {


std::string tempResult;

@autoreleasepool {
NSData *sourceData = [NSData dataWithBytesNoCopy:(void *)filterData.data()
length:filterData.size()
freeWhenDone:NO];
NSData *filteredData = [styleFilter filterData:sourceData];
tempResult = std::string((const char*)[filteredData bytes], [filteredData length]);

}
return tempResult;

};

// Set the ivar
[styleFilter setFilter:coreStyleFilter];

_mbglMap->getStyle().addStyleFilter(coreStyleFilter);

}


- (NSArray<NSString*>*)getActionJournalLogFiles
{
const auto& actionJournal = _mbglMap->getActionJournal();
Expand Down
14 changes: 14 additions & 0 deletions src/mbgl/plugin/plugin_style_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <mbgl/plugin/plugin_style_filter.hpp>

using namespace mbgl::style;

// This method
const std::string PluginStyleFilter::filterResponse(const std::string& res) {

if (_filterStyleFunction) {
auto tempResult = _filterStyleFunction(res);
return tempResult;
}
return res;

}
23 changes: 23 additions & 0 deletions src/mbgl/plugin/plugin_style_filter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <functional>


namespace mbgl {

namespace style {

class PluginStyleFilter {
public:

using OnFilterStyle = std::function<const std::string(const std::string&)>;

OnFilterStyle _filterStyleFunction;

// This method
const std::string filterResponse(const std::string&);
};


}
}
8 changes: 8 additions & 0 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,13 @@ std::unique_ptr<Layer> Style::removeLayer(const std::string& id) {
return impl->removeLayer(id);
}

// Add style parsing filter
void Style::addStyleFilter(std::shared_ptr<mbgl::style::PluginStyleFilter> filter) {
impl->mutated = true;
return impl->addStyleFilter(filter);

}


} // namespace style
} // namespace mbgl
Loading
Loading