Skip to content

Fix before-id bug#114

Merged
giswqs merged 2 commits intomainfrom
before-id
Nov 8, 2025
Merged

Fix before-id bug#114
giswqs merged 2 commits intomainfrom
before-id

Conversation

@giswqs
Copy link
Copy Markdown
Member

@giswqs giswqs commented Nov 8, 2025

No description provided.

Copilot AI review requested due to automatic review settings November 8, 2025 03:27
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Nov 8, 2025

@github-actions github-actions Bot temporarily deployed to pull request November 8, 2025 03:28 Inactive
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for layer ordering in MapLibre by implementing the before_id parameter, allowing layers to be inserted at specific positions in the layer stack rather than always on top.

  • Adds Python methods to query style layers and find the first symbol layer
  • Modifies add_layer() to store and pass before_id parameter to JavaScript
  • Updates JavaScript layer restoration and addition logic to respect before_id positioning

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
anymap/maplibre.py Added style introspection methods and modified add_layer() to store before_id in metadata and pass it to JavaScript
anymap/static/maplibre_widget.js Updated layer restoration to read beforeId from metadata and modified addLayer case to handle before_id parameter with fallback logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread anymap/maplibre.py Outdated
Dict: The style of the map.
"""
if isinstance(self._style, str):
response = requests.get(self.style, timeout=10)
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

Inconsistent variable reference: the code checks isinstance(self._style, str) but then calls requests.get(self.style, ...). Should use self._style in the requests.get call to match the isinstance check.

Suggested change
response = requests.get(self.style, timeout=10)
response = requests.get(self._style, timeout=10)

Copilot uses AI. Check for mistakes.
Comment thread anymap/maplibre.py
Comment on lines +267 to +272
def get_style_layers(self, return_ids=False, sorted=True) -> List[str]:
"""
Get the names of the basemap layers.

Returns:
List[str]: The names of the basemap layers.
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

The return type annotation List[str] is incorrect. The method can return either List[str] (when return_ids=True), List[Dict] (when return_ids=False), or an empty list. The return type should be Union[List[str], List[Dict]] to accurately reflect the method's behavior.

Suggested change
def get_style_layers(self, return_ids=False, sorted=True) -> List[str]:
"""
Get the names of the basemap layers.
Returns:
List[str]: The names of the basemap layers.
def get_style_layers(self, return_ids=False, sorted=True) -> Union[List[str], List[Dict]]:
"""
Get the basemap layers or their IDs.
Returns:
Union[List[str], List[Dict]]: The list of basemap layer IDs (if return_ids=True), or the list of basemap layer dictionaries (if return_ids=False).

Copilot uses AI. Check for mistakes.
Comment thread anymap/maplibre.py
Comment on lines +269 to +272
Get the names of the basemap layers.

Returns:
List[str]: The names of the basemap layers.
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

The docstring description is inaccurate. The method returns layer IDs (when return_ids=True) or full layer objects (when return_ids=False), not just 'names'. The description should clarify this and document the parameters return_ids and sorted.

Suggested change
Get the names of the basemap layers.
Returns:
List[str]: The names of the basemap layers.
Get the style layers from the map's style.
Args:
return_ids (bool): If True, return a list of layer IDs (str). If False, return a list of full layer objects (dict).
sorted (bool): If True and return_ids is True, the list of IDs will be sorted alphabetically.
Returns:
List[str] or List[dict]: A list of layer IDs (if return_ids=True) or a list of full layer objects (if return_ids=False).

Copilot uses AI. Check for mistakes.
Comment on lines 5665 to +5707
const currentLayers = model.get("_layers") || {};
currentLayers[actualLayerId] = layerConfig;
model.set("_layers", currentLayers);
model.save_changes();
attachFeaturePopup(actualLayerId);
} catch (error) {
console.error('[addLayer] Error adding layer:', error);
// Try adding without beforeId as fallback
if (beforeId) {
try {
map.addLayer(layerConfig);
const currentLayers = model.get("_layers") || {};
currentLayers[actualLayerId] = layerConfig;
model.set("_layers", currentLayers);
model.save_changes();
attachFeaturePopup(actualLayerId);
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

Duplicate code: lines 5703-5707 duplicate the logic from lines 5692-5696. Consider extracting this persistence and popup attachment logic into a helper function to reduce duplication.

Suggested change
const currentLayers = model.get("_layers") || {};
currentLayers[actualLayerId] = layerConfig;
model.set("_layers", currentLayers);
model.save_changes();
attachFeaturePopup(actualLayerId);
} catch (error) {
console.error('[addLayer] Error adding layer:', error);
// Try adding without beforeId as fallback
if (beforeId) {
try {
map.addLayer(layerConfig);
const currentLayers = model.get("_layers") || {};
currentLayers[actualLayerId] = layerConfig;
model.set("_layers", currentLayers);
model.save_changes();
attachFeaturePopup(actualLayerId);
persistLayerAndAttachPopup(actualLayerId, layerConfig);
} catch (error) {
console.error('[addLayer] Error adding layer:', error);
// Try adding without beforeId as fallback
if (beforeId) {
try {
map.addLayer(layerConfig);
persistLayerAndAttachPopup(actualLayerId, layerConfig);

Copilot uses AI. Check for mistakes.
Comment thread anymap/maplibre.py
if add_sidebar:
self._ipython_display_ = self._patched_display

def get_style(self) -> Dict:
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

This assignment to 'get_style' is unnecessary as it is redefined before this value is used.

Copilot uses AI. Check for mistakes.
Comment thread anymap/maplibre.py
style = {}
return style

def get_style_layers(self, return_ids=False, sorted=True) -> List[str]:
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

This assignment to 'get_style_layers' is unnecessary as it is redefined before this value is used.

Copilot uses AI. Check for mistakes.
@github-actions github-actions Bot temporarily deployed to pull request November 8, 2025 03:35 Inactive
@giswqs giswqs merged commit b2f056b into main Nov 8, 2025
8 checks passed
@giswqs giswqs deleted the before-id branch November 8, 2025 03:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants