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

Update ShapeSource methods to make it usable with any cluster #1499

Merged
merged 4 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
```
Please add unreleased changes in the following style:
PR Title ([#123](link to my pr))

Update ShapeSource methods to make it usable with any cluster ([#1499](https://github.com/react-native-mapbox-gl/maps/pull/1499))
tr3v3r marked this conversation as resolved.
Show resolved Hide resolved
```

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,55 @@ public void querySourceFeatures(String callbackID,
mManager.handleEvent(event);
}

public void getClusterExpansionZoom(String callbackID, int clusterId) {
public void getClusterExpansionZoom(String callbackID, String featureJSON) {
if (mSource == null) {
WritableMap payload = new WritableNativeMap();
payload.putString("error", "source is not yet loaded");
AndroidCallbackEvent event = new AndroidCallbackEvent(this, callbackID, payload);
mManager.handleEvent(event);
return;
}
Feature feature = Feature.fromJson(featureJSON);

int zoom = mSource.getClusterExpansionZoom(feature);

if (zoom == -1) {
WritableMap payload = new WritableNativeMap();
mfazekas marked this conversation as resolved.
Show resolved Hide resolved
payload.putString("error", "Could not get zoom for cluster:" + featureJSON);
AndroidCallbackEvent event = new AndroidCallbackEvent(this, callbackID, payload);
mManager.handleEvent(event);
return;
}

WritableMap payload = new WritableNativeMap();
payload.putInt("data", zoom);

AndroidCallbackEvent event = new AndroidCallbackEvent(this, callbackID, payload);
mManager.handleEvent(event);
}

public void getClusterLeaves(String callbackID, String featureJSON, int number, int offset) {
Feature clusterFeature = Feature.fromJson(featureJSON);
tr3v3r marked this conversation as resolved.
Show resolved Hide resolved
FeatureCollection leaves = mSource.getClusterLeaves(clusterFeature, number, offset);
WritableMap payload = new WritableNativeMap();
payload.putString("data", leaves.toJson());

AndroidCallbackEvent event = new AndroidCallbackEvent(this, callbackID, payload);
mManager.handleEvent(event);
}

public void getClusterChildren(String callbackID, String featureJSON) {
Feature clusterFeature = Feature.fromJson(featureJSON);
FeatureCollection leaves = mSource.getClusterChildren(clusterFeature);
WritableMap payload = new WritableNativeMap();
payload.putString("data", leaves.toJson());

AndroidCallbackEvent event = new AndroidCallbackEvent(this, callbackID, payload);
mManager.handleEvent(event);
}

// Deprecated. Will be removed in 9+ ver.
public void getClusterExpansionZoomById(String callbackID, int clusterId) {
if (mSource == null) {
WritableMap payload = new WritableNativeMap();
payload.putString("error", "source is not yet loaded");
Expand Down Expand Up @@ -210,7 +258,8 @@ public void getClusterExpansionZoom(String callbackID, int clusterId) {
mManager.handleEvent(event);
}

public void getClusterLeaves(String callbackID, int clusterId, int number, int offset) {
// Deprecated. Will be removed in 9+ ver.
public void getClusterLeavesById(String callbackID, int clusterId, int number, int offset) {
Feature clusterFeature = mSource.querySourceFeatures(Expression.eq(Expression.get("cluster_id"), clusterId)).get(0);
FeatureCollection leaves = mSource.getClusterLeaves(clusterFeature, number, offset);
WritableMap payload = new WritableNativeMap();
Expand All @@ -220,7 +269,8 @@ public void getClusterLeaves(String callbackID, int clusterId, int number, int o
mManager.handleEvent(event);
}

public void getClusterChildren(String callbackID, int clusterId) {
// Deprecated. Will be removed in 9+ ver.
public void getClusterChildrenById(String callbackID, int clusterId) {
Feature clusterFeature = mSource.querySourceFeatures(Expression.eq(Expression.get("cluster_id"), clusterId)).get(0);
FeatureCollection leaves = mSource.getClusterChildren(clusterFeature);
WritableMap payload = new WritableNativeMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public Map<String, String> customEvents() {
public static final int METHOD_GET_CLUSTER_LEAVES = 105;
public static final int METHOD_GET_CLUSTER_CHILDREN = 106;

// Deprecated. Will be removed in 9+ ver.
public static final int METHOD_GET_CLUSTER_EXPANSION_ZOOM_BY_ID = 107;
public static final int METHOD_GET_CLUSTER_LEAVES_BY_ID = 108;
public static final int METHOD_GET_CLUSTER_CHILDREN_BY_ID = 109;

@Nullable
@Override
public Map<String, Integer> getCommandsMap() {
Expand All @@ -165,6 +170,12 @@ public Map<String, Integer> getCommandsMap() {
.put("getClusterExpansionZoom", METHOD_GET_CLUSTER_EXPANSION_ZOOM)
.put("getClusterLeaves", METHOD_GET_CLUSTER_LEAVES)
.put("getClusterChildren", METHOD_GET_CLUSTER_CHILDREN)

// Deprecated. Will be removed in 9+ ver.
.put("getClusterExpansionZoomById", METHOD_GET_CLUSTER_EXPANSION_ZOOM_BY_ID)
.put("getClusterLeavesById", METHOD_GET_CLUSTER_LEAVES_BY_ID)
.put("getClusterChildrenById", METHOD_GET_CLUSTER_CHILDREN_BY_ID)

.build();
}

Expand All @@ -178,18 +189,35 @@ public void receiveCommand(RCTMGLShapeSource source, int commandID, @Nullable Re
);
break;
case METHOD_GET_CLUSTER_EXPANSION_ZOOM:
source.getClusterExpansionZoom(args.getString(0), args.getInt(1));
source.getClusterExpansionZoom(args.getString(0), args.getString(1));
break;
case METHOD_GET_CLUSTER_LEAVES:
source.getClusterLeaves(
args.getString(0),
args.getInt(1),
args.getString(1),
args.getInt(2),
args.getInt((3))
);
break;
case METHOD_GET_CLUSTER_CHILDREN:
source.getClusterChildren(
args.getString(0),
args.getString(1)
);
break;
case METHOD_GET_CLUSTER_EXPANSION_ZOOM_BY_ID:
source.getClusterExpansionZoomById(args.getString(0), args.getInt(1));
break;
case METHOD_GET_CLUSTER_LEAVES_BY_ID:
source.getClusterLeavesById(
args.getString(0),
args.getInt(1),
args.getInt(2),
args.getInt((3))
);
break;
case METHOD_GET_CLUSTER_CHILDREN_BY_ID:
source.getClusterChildrenById(
args.getString(0),
args.getInt(1)
);
Expand Down
12 changes: 6 additions & 6 deletions docs/ShapeSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ shapeSource.features()
```


#### getClusterExpansionZoom(clusterId)
#### getClusterExpansionZoom(feature)

Returns the zoom needed to expand the cluster.

##### arguments
| Name | Type | Required | Description |
| ---- | :--: | :------: | :----------: |
| `clusterId` | `number` | `Yes` | The id of the cluster to expand. |
| `feature` | `Feature` | `Yes` | The feature cluster to expand. |



Expand All @@ -53,14 +53,14 @@ const zoom = await shapeSource.getClusterExpansionZoom(clusterId);
```


#### getClusterLeaves(clusterId, limit, offset)
#### getClusterLeaves(feature, limit, offset)

Returns the FeatureCollection from the cluster.

##### arguments
| Name | Type | Required | Description |
| ---- | :--: | :------: | :----------: |
| `clusterId` | `number` | `Yes` | The id of the cluster to expand. |
| `feature` | `Feature` | `Yes` | The feature cluster to expand. |
| `limit` | `number` | `Yes` | The number of points to return. |
| `offset` | `number` | `Yes` | The amount of points to skip (for pagination). |

Expand All @@ -71,14 +71,14 @@ const collection = await shapeSource.getClusterLeaves(clusterId, limit, offset);
```


#### getClusterChildren(clusterId)
#### getClusterChildren(feature)

Returns the FeatureCollection from the cluster (on the next zoom level).

##### arguments
| Name | Type | Required | Description |
| ---- | :--: | :------: | :----------: |
| `clusterId` | `number` | `Yes` | The id of the cluster to expand. |
| `feature` | `Feature` | `Yes` | The feature cluster to expand. |



Expand Down
24 changes: 12 additions & 12 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3575,16 +3575,16 @@
},
{
"name": "getClusterExpansionZoom",
"docblock": "Returns the zoom needed to expand the cluster.\n\n@example\nconst zoom = await shapeSource.getClusterExpansionZoom(clusterId);\n\n@param {number} clusterId - The id of the cluster to expand.\n@return {number}",
"docblock": "Returns the zoom needed to expand the cluster.\n\n@example\nconst zoom = await shapeSource.getClusterExpansionZoom(clusterId);\n\n@param {Feature} feature - The feature cluster to expand.\n@return {number}",
"modifiers": [
"async"
],
"params": [
{
"name": "clusterId",
"description": "The id of the cluster to expand.",
"name": "feature",
"description": "The feature cluster to expand.",
"type": {
"name": "number"
"name": "Feature"
},
"optional": false
}
Expand All @@ -3602,16 +3602,16 @@
},
{
"name": "getClusterLeaves",
"docblock": "Returns the FeatureCollection from the cluster.\n\n@example\nconst collection = await shapeSource.getClusterLeaves(clusterId, limit, offset);\n\n@param {number} clusterId - The id of the cluster to expand.\n@param {number} limit - The number of points to return.\n@param {number} offset - The amount of points to skip (for pagination).\n@return {FeatureCollection}",
"docblock": "Returns the FeatureCollection from the cluster.\n\n@example\nconst collection = await shapeSource.getClusterLeaves(clusterId, limit, offset);\n\n@param {Feature} feature - The feature cluster to expand.\n@param {number} limit - The number of points to return.\n@param {number} offset - The amount of points to skip (for pagination).\n@return {FeatureCollection}",
"modifiers": [
"async"
],
"params": [
{
"name": "clusterId",
"description": "The id of the cluster to expand.",
"name": "feature",
"description": "The feature cluster to expand.",
"type": {
"name": "number"
"name": "Feature"
},
"optional": false
},
Expand Down Expand Up @@ -3645,16 +3645,16 @@
},
{
"name": "getClusterChildren",
"docblock": "Returns the FeatureCollection from the cluster (on the next zoom level).\n\n@example\nconst collection = await shapeSource.getClusterChildren(clusterId);\n\n@param {number} clusterId - The id of the cluster to expand.\n@return {FeatureCollection}",
"docblock": "Returns the FeatureCollection from the cluster (on the next zoom level).\n\n@example\nconst collection = await shapeSource.getClusterChildren(clusterId);\n\n@param {Feature} feature - The feature cluster to expand.\n@return {FeatureCollection}",
"modifiers": [
"async"
],
"params": [
{
"name": "clusterId",
"description": "The id of the cluster to expand.",
"name": "feature",
"description": "The feature cluster to expand.",
"type": {
"name": "number"
"name": "Feature"
},
"optional": false
}
Expand Down
12 changes: 7 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,21 @@ declare namespace MapboxGL {
*/
class VectorSource extends Component<VectorSourceProps> { }
class ShapeSource extends Component<ShapeSourceProps> {
getClusterExpansionZoom(clusterId: number): Promise<number>;
features(filter?: Expression): Promise<FeatureCollection<Geometry, Properties>>

getClusterExpansionZoom(feature: Feature<Geometry, Properties> | number): Promise<number>
/**
* Returns all the leaves of a cluster with pagination support.
* @param clusterId the ID of the cluster
* @param cluster feature cluster
* @param limit the number of leaves to return
* @param offset the amount of points to skip (for pagination)
*/
getClusterLeaves: (clusterId: number, limit: number, offset: number ) => object
getClusterLeaves: (feature: Feature<Geometry, Properties> | number, limit: number, offset: number ) => Promise<FeatureCollection<Geometry, Properties>>
/**
* Returns the children of a cluster (on the next zoom level).
* @param clusterId the ID of the cluster
* @param cluster feature cluster
*/
getClusterChildren: (clusterId: number) => object
getClusterChildren: (feature: Feature<Geometry, Properties> | number) => Promise<FeatureCollection<Geometry, Properties>>
}
class RasterSource extends Component<RasterSourceProps> { }

Expand Down
17 changes: 14 additions & 3 deletions ios/RCTMGL/RCTMGLShapeSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,22 @@
@property (nonatomic, assign) BOOL hasPressListener;

- (nonnull NSArray<id <MGLFeature>> *)featuresMatchingPredicate:(nullable NSPredicate *)predicate;
- (nonnull NSArray<id <MGLFeature>> *)getClusterLeaves:(nonnull NSNumber *)clusterId
- (nonnull NSArray<id <MGLFeature>> *)getClusterLeaves:(nonnull NSString *)featureJSON
number:(NSUInteger)number
offset:(NSUInteger)offset;
- (nonnull NSArray<id <MGLFeature>> *)getClusterChildren:(nonnull NSNumber *)clusterId;
- (nonnull NSArray<id <MGLFeature>> *)getClusterChildren:(nonnull NSString *)featureJSON;

- (double)getClusterExpansionZoom:(nonnull NSNumber *)clusterId;
- (double)getClusterExpansionZoom:(nonnull NSString *)featureJSON;

// Deprecated. Will be removed in 9+ ver.
- (nonnull NSArray<id <MGLFeature>> *)getClusterLeavesById:(nonnull NSNumber *)clusterId
number:(NSUInteger)number
offset:(NSUInteger)offset;

// Deprecated. Will be removed in 9+ ver.
- (nonnull NSArray<id <MGLFeature>> *)getClusterChildrenById:(nonnull NSNumber *)clusterId;

// Deprecated. Will be removed in 9+ ver.
- (double)getClusterExpansionZoomById:(nonnull NSNumber *)clusterId;

@end
41 changes: 38 additions & 3 deletions ios/RCTMGL/RCTMGLShapeSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,40 @@ - (nullable MGLSource*)makeSource
return [shapeSource featuresMatchingPredicate:predicate];
}

- (double)getClusterExpansionZoom:(nonnull NSNumber *)clusterId
- (double)getClusterExpansionZoom:(nonnull NSString *)featureJSON
{
MGLShapeSource *shapeSource = (MGLShapeSource *)self.source;

MGLPointFeature *feature = (MGLPointFeature*)[RCTMGLUtils shapeFromGeoJSON:featureJSON];

return [shapeSource zoomLevelForExpandingCluster:(MGLPointFeatureCluster *)feature];
}

- (nonnull NSArray<id <MGLFeature>> *)getClusterLeaves:(nonnull NSString *)featureJSON
number:(NSUInteger)number
offset:(NSUInteger)offset
{
MGLShapeSource *shapeSource = (MGLShapeSource *)self.source;

MGLPointFeature *feature = (MGLPointFeature*)[RCTMGLUtils shapeFromGeoJSON:featureJSON];

MGLPointFeatureCluster * cluster = (MGLPointFeatureCluster *)feature;
return [shapeSource leavesOfCluster:cluster offset:offset limit:number];
}

- (nonnull NSArray<id <MGLFeature>> *)getClusterChildren:(nonnull NSString *)featureJSON
{
MGLShapeSource *shapeSource = (MGLShapeSource *)self.source;

MGLPointFeature *feature = (MGLPointFeature*)[RCTMGLUtils shapeFromGeoJSON:featureJSON];

MGLPointFeatureCluster * cluster = (MGLPointFeatureCluster *)feature;
return [shapeSource childrenOfCluster:cluster];
}


// Deprecated. Will be removed in 9+ ver.
- (double)getClusterExpansionZoomById:(nonnull NSNumber *)clusterId
{
MGLShapeSource *shapeSource = (MGLShapeSource *)self.source;
NSArray<id<MGLFeature>> *features = [shapeSource featuresMatchingPredicate: [NSPredicate predicateWithFormat:@"%K = %i", @"cluster_id", clusterId.intValue]];
Expand All @@ -118,7 +151,8 @@ - (double)getClusterExpansionZoom:(nonnull NSNumber *)clusterId
return [shapeSource zoomLevelForExpandingCluster:(MGLPointFeatureCluster *)features[0]];
}

- (nonnull NSArray<id <MGLFeature>> *)getClusterLeaves:(nonnull NSNumber *)clusterId
// Deprecated. Will be removed in 9+ ver.
- (nonnull NSArray<id <MGLFeature>> *)getClusterLeavesById:(nonnull NSNumber *)clusterId
number:(NSUInteger)number
offset:(NSUInteger)offset
{
Expand All @@ -131,7 +165,8 @@ - (double)getClusterExpansionZoom:(nonnull NSNumber *)clusterId
return [shapeSource leavesOfCluster:cluster offset:offset limit:number];
}

- (nonnull NSArray<id <MGLFeature>> *)getClusterChildren:(nonnull NSNumber *)clusterId
// Deprecated. Will be removed in 9+ ver.
- (nonnull NSArray<id <MGLFeature>> *)getClusterChildrenById:(nonnull NSNumber *)clusterId
{
MGLShapeSource *shapeSource = (MGLShapeSource *)self.source;

Expand Down
Loading