Skip to content

bump version #87

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

Closed
Closed
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: 1 addition & 1 deletion src/guitares/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
@author: ormondt
"""

__version__ = "0.0.10"
__version__ = "0.0.11"
76 changes: 49 additions & 27 deletions src/guitares/pyqt5/mapbox/image_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,50 @@ def make_overlay(self):
return None, None

def update(self):
if hasattr(self.data, "map_overlay"):
if hasattr(self.data, "map_overlay"):
xlim, ylim = self.make_overlay()
if xlim is None:
return
bounds = [[xlim[0], xlim[1]], [ylim[0], ylim[1]]]
bounds_string = "[[" + str(bounds[0][0]) + "," + str(bounds[0][1]) + "],[" + str(bounds[1][0]) + "," + str(bounds[1][1]) + "]]"
overlay_file = "./overlays/" + self.file_name
js_string = "import('/js/image_layer.js').then(module => {module.updateLayer('" + overlay_file + "','" + self.map_id + "'," + bounds_string + ","")});"
bounds_string = f"[[{bounds[0][0]},{bounds[0][1]}],[{bounds[1][0]},{bounds[1][1]}]]"
overlay_file = f"./overlays/{self.file_name}"
js_string = f"import('/js/image_layer.js').then(module => {{module.updateLayer('{overlay_file}', '{self.map_id}', {bounds_string}, '')}});"
self.mapbox.view.page().runJavaScript(js_string)
js_string = "import('/js/image_layer.js').then(module => {module.setOpacity('" + self.map_id + "', 1.0)});"
js_string = f"import('/js/image_layer.js').then(module => {{module.setOpacity('{self.map_id}', 1.0)}});"
self.mapbox.view.page().runJavaScript(js_string)


def set_data(self,
data,
image_file=None,
xlim=None,
ylim=None):

def set_data(self, data, image_file=None, xlim=None, ylim=None):
fname = os.path.join(self.mapbox.server_path, "overlays", self.file_name)

self.data = data

js_string = f"import('/js/main.js').then(module => {{module.removeLayer('{self.map_id}')}});"
self.mapbox.view.page().runJavaScript(js_string)

try:
xlim, ylim = self.make_overlay()
if xlim is None:
return
except Exception as e:
print(f"Something went wrong with map overlay: {self.map_id}, {e}")
return

bounds = [[xlim[0], xlim[1]], [ylim[0], ylim[1]]]
bounds_string = f"[[{bounds[0][0]},{bounds[0][1]}],[{bounds[1][0]},{bounds[1][1]}]]"
overlay_file = f"./overlays/{self.file_name}"
js_string = f"import('/js/image_layer.js').then(module => {{module.addLayer('{overlay_file}', '{self.map_id}', {bounds_string}, '')}});"
self.mapbox.view.page().runJavaScript(js_string)

# def set_data(self,
# data,
# image_file=None,
# xlim=None,
# ylim=None):

# fname = os.path.join(self.mapbox.server_path, "overlays", self.file_name)

# self.data = data

# id_string = self.id

# dataset = rasterio.open(image_file)
Expand Down Expand Up @@ -117,21 +139,21 @@ def set_data(self,

# shutil.copy(image_file, os.path.join(self.mapbox.server_path, "overlays", self.file_name))

js_string = "import('/js/main.js').then(module => {module.removeLayer('" + self.map_id + "')});"
self.mapbox.view.page().runJavaScript(js_string)

try:
xlim, ylim = self.make_overlay()
if xlim is None:
return
except:
print("Something went wrong with map overlay : " + self.map_id)
return
bounds = [[xlim[0], xlim[1]], [ylim[0], ylim[1]]]
bounds_string = "[[" + str(bounds[0][0]) + "," + str(bounds[0][1]) + "],[" + str(bounds[1][0]) + "," + str(bounds[1][1]) + "]]"
overlay_file = "./overlays/" + self.file_name
js_string = "import('/js/image_layer.js').then(module => {module.addLayer('" + overlay_file + "','" + self.map_id + "'," + bounds_string + ","")});"
self.mapbox.view.page().runJavaScript(js_string)
# js_string = "import('/js/main.js').then(module => {module.removeLayer('" + self.map_id + "')});"
# self.mapbox.view.page().runJavaScript(js_string)

# try:
# xlim, ylim = self.make_overlay()
# if xlim is None:
# return
# except:
# print("Something went wrong with map overlay : " + self.map_id)
# return
# bounds = [[xlim[0], xlim[1]], [ylim[0], ylim[1]]]
# bounds_string = "[[" + str(bounds[0][0]) + "," + str(bounds[0][1]) + "],[" + str(bounds[1][0]) + "," + str(bounds[1][1]) + "]]"
# overlay_file = "./overlays/" + self.file_name
# js_string = "import('/js/image_layer.js').then(module => {module.addLayer('" + overlay_file + "','" + self.map_id + "'," + bounds_string + ","")});"
# self.mapbox.view.page().runJavaScript(js_string)


# overlay_file = "./overlays/" + "main.background_topography.png"
Expand Down
49 changes: 48 additions & 1 deletion src/guitares/pyqt5/mapbox/server/js/image_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ function getMap(side) {

export function addLayer(fileName, id, bounds, colorbar, side) {
var mp = getMap(side);

// Always remove the layer first to avoid an error
if (mp.getLayer(id)) {
mp.removeLayer(id);
}

if (mp.getSource(id)) {
mp.removeSource(id);
}

mp.addSource(id, {
'type': 'image',
'url': fileName,
Expand Down Expand Up @@ -35,7 +45,43 @@ export function addLayer(fileName, id, bounds, colorbar, side) {

export function updateLayer(fileName, id, bounds, colorbar, side) {
var mp = getMap(side);
mp.getSource(id).updateImage({

// If the layer does not exist, add it
if (!mp.getLayer(id)) {
// console.log("Layer " + id + " does not exist, adding it instead of updating it");
mp.addLayer(fileName, id, bounds, colorbar, side);
return;
}

// If the layer is not visible, do not update it
if (mp.getLayoutProperty(id, 'visibility') !== 'visible') {
// console.log("Layer " + id + " is not visible, so it won't be updated");
return;
}

// If the source does not exist, add it
if (!mp.getSource(id)) {
mp.addSource(id, {
'type': 'image',
'url': fileName,
'coordinates': [
[bounds[0][0], bounds[1][1]],
[bounds[0][1], bounds[1][1]],
[bounds[0][1], bounds[1][0]],
[bounds[0][0], bounds[1][0]]
]
});
}

var source = mp.getSource(id);
// If the source does not have updateImage method, do not update it
if (typeof source.updateImage !== 'function') {
// console.log("Source does not have updateImage method");
return;
}

// Update the image
source.updateImage({
'url': fileName,
'coordinates': [
[bounds[0][0], bounds[1][1]],
Expand All @@ -44,6 +90,7 @@ export function updateLayer(fileName, id, bounds, colorbar, side) {
[bounds[0][0], bounds[1][0]]
]
});

if (colorbar) {
setLegend(mp, id, colorbar);
}
Expand Down
151 changes: 90 additions & 61 deletions src/guitares/pyqt5/mapbox/server/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,51 +96,55 @@ map.on('style.load', () => {
});

const layers = map.getStyle().layers;
const labelLayerId = layers.find(
(layer) => layer.type === 'symbol' && layer.layout['text-field']
).id;

// The 'building' layer in the Mapbox Streets
// vector tileset contains building height data
// from OpenStreetMap.
// console.log('Adding 3d buildings')
map.addLayer(
{
'id': 'add-3d-buildings',
'source': 'composite',
'source-layer': 'building',
'filter': ['==', 'extrude', 'true'],
'type': 'fill-extrusion',
'minzoom': 15,
'paint': {
'fill-extrusion-color': '#aaa',

// Use an 'interpolate' expression to
// add a smooth transition effect to
// the buildings as the user zooms in.
'fill-extrusion-height': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'height']
],
'fill-extrusion-base': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'min_height']
],
'fill-extrusion-opacity': 0.6
}
},
labelLayerId
const labelLayer = layers.find(
(layer) => layer.type === 'symbol' && layer.layout['text-field']
);

if (!labelLayer) {
console.log('No label layer with id found with text-field property');
} else {
const labelLayerId = labelLayer.id;
// The 'building' layer in the Mapbox Streets
// vector tileset contains building height data
// from OpenStreetMap.
// console.log('Adding 3d buildings')
map.addLayer({
'id': 'add-3d-buildings',
'source': 'composite',
'source-layer': 'building',
'filter': ['==', 'extrude', 'true'],
'type': 'fill-extrusion',
'minzoom': 15,
'paint': {
'fill-extrusion-color': '#aaa',
// Use an 'interpolate' expression to
// add a smooth transition effect to
// the buildings as the user zooms in.
'fill-extrusion-height': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'height']
],
'fill-extrusion-base': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'min_height']
],
'fill-extrusion-opacity': 0.6
}
}, labelLayerId);
}

// Add additional layers

});

map.on('moveend', () => {
Expand Down Expand Up @@ -278,8 +282,6 @@ export function hideLegend(id) {
}
}



export function getExtent() {
// Called after moving map ended
// Get new map extents
Expand Down Expand Up @@ -351,22 +353,49 @@ export function setProjection(projection) {
map.setProjection(projection);
}

export function setLayerStyle(style) {
map.setStyle('mapbox://styles/mapbox/' + style);
map.once('idle', () => { addDummyLayer(); layerStyleSet(); });
var legends = document.getElementsByClassName("overlay_legend")
if (legends) {
for (const legend of legends) {
legend.remove();
}
// styleID should be in the form "satellite-v9"
export function setLayerStyle(styleID) {
fetch(`https://api.mapbox.com/styles/v1/mapbox/${styleID}?access_token=${mapboxgl.accessToken}`)
.then(response => response.json())
.then(newStyle => {
const currentStyle = map.getStyle();
// ensure any sources from the current style are copied across to the new style
newStyle.sources = Object.assign(
{},
currentStyle.sources,
newStyle.sources
);

// find the index of where to insert our layers to retain in the new style
let labelIndex = newStyle.layers.findIndex((el) => {
return el.id == 'waterway-label';
});

// default to on top
if (labelIndex === -1) {
labelIndex = newStyle.layers.length;
}
const appLayers = currentStyle.layers.filter((el) => {
// app layers are the layers to retain, and these are any layers which have a different source set
return (
el.source &&
el.source != 'mapbox://mapbox.satellite' &&
el.source != 'mapbox' &&
el.source != 'composite'
);
});
newStyle.layers = [
...newStyle.layers.slice(0, labelIndex),
...appLayers,
...newStyle.layers.slice(labelIndex, -1),
];
map.setStyle(newStyle);
layerStyleSet();
})
.catch(error => {
console.error(`Error fetching style: ${error.message}`);
});
}
var legends = document.getElementsByClassName("choropleth_legend")
if (legends) {
for (const legend of legends) {
legend.remove();
}
}
}

export function setTerrain(trueOrFalse, exaggeration) {
if (trueOrFalse) {
Expand Down
Loading