Skip to content

Commit

Permalink
Attempt to patch layer style
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 3, 2024
1 parent b4f8095 commit 4ea3a16
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
22 changes: 22 additions & 0 deletions felt/core/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FeltApiClient:
USAGE_ENDPOINT = '/internal/reports'
RECENT_MAPS_ENDPOINT = '/maps/recent'
UPLOAD_V2_ENDPOINT = '/maps/{}/upload'
PATCH_STYLE_ENDPOINT = '/maps/{}/layers/{}/style'

def __init__(self):
# default headers to add to all requests
Expand Down Expand Up @@ -418,6 +419,27 @@ def finalize_layer_upload(self,
json_data.encode()
)

def patch_style(self,
map_id: str,
layer_id: str,
data: Dict) \
-> QNetworkReply:
"""
Patches a layer's style
"""
request = self._build_request(
self.PATCH_STYLE_ENDPOINT.format(map_id, layer_id),
{'Content-Type': 'application/json'}
)

json_data = json.dumps(data)
print(json_data)
return QgsNetworkAccessManager.instance().sendCustomRequest(
request,
b"PATCH",
json_data.encode()
)

def report_usage(self,
content: str,
usage_type: UsageType = UsageType.Info) -> QNetworkReply:
Expand Down
39 changes: 14 additions & 25 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
from .layer_style import LayerStyle
from .logger import Logger
from .map import Map
from .fsl_converter import (
FslConverter,
ConversionContext
)


@dataclass
Expand Down Expand Up @@ -207,31 +211,16 @@ def representative_layer_style(layer: QgsVectorLayer) -> LayerStyle:
if not layer.isSpatial() or not layer.renderer():
return LayerStyle()

if isinstance(layer.renderer(), QgsSingleSymbolRenderer):
return LayerExporter.symbol_to_layer_style(
layer.renderer().symbol()
)
if isinstance(layer.renderer(), QgsCategorizedSymbolRenderer) and \
layer.renderer().categories():
first_category = layer.renderer().categories()[0]
return LayerExporter.symbol_to_layer_style(
first_category.symbol()
)
if isinstance(layer.renderer(), QgsGraduatedSymbolRenderer) and \
layer.renderer().ranges():
first_range = layer.renderer().ranges()[0]
return LayerExporter.symbol_to_layer_style(
first_range.symbol()
)
if isinstance(layer.renderer(), QgsRuleBasedRenderer) and \
layer.renderer().rootRule().children():
for child in layer.renderer().rootRule().children():
if child.symbol():
return LayerExporter.symbol_to_layer_style(
child.symbol()
)

return LayerStyle()
context = ConversionContext()
fsl = FslConverter.vector_renderer_to_fsl(
layer.renderer(), context, layer.opacity()
)
if fsl:
fsl['version'] = '2.1'
# TODO -- labeling
return LayerStyle(
fsl=fsl
)

@staticmethod
def symbol_to_layer_style(symbol: QgsSymbol) -> LayerStyle:
Expand Down
6 changes: 5 additions & 1 deletion felt/core/layer_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
__revision__ = '$Format:%H$'

from dataclasses import dataclass
from typing import Optional
from typing import (
Optional,
Dict
)

from qgis.PyQt.QtGui import QColor

Expand All @@ -26,3 +29,4 @@ class LayerStyle:
"""
fill_color: Optional[QColor] = None
stroke_color: Optional[QColor] = None
fsl: Optional[Dict[str, object]] = None
22 changes: 21 additions & 1 deletion felt/core/map_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
QDate,
pyqtSignal,
QThread,
QSize
QSize,
QEventLoop
)
from qgis.PyQt.QtNetwork import (
QNetworkReply,
Expand Down Expand Up @@ -474,6 +475,25 @@ def _upload_progress(sent, total):
if self.isCanceled():
return False

layer_id = upload_details.get('data', {}).get('attributes', {}).get('first_layer_id')
if details.style.fsl is not None:
if not layer_id:
Logger.instance().log_error_json(
{
'type': Logger.S3_UPLOAD,
'error': 'Didn\'t get layer id to use for patching style'
}
)
else:
reply = API_CLIENT.patch_style(
map_id=self.associated_map.id,
layer_id=layer_id,
data=details.style.fsl
)
loop = QEventLoop()
reply.finished.connect(loop.exit)
loop.exec()

multi_step_feedback.step_finished()

return True
Expand Down

0 comments on commit 4ea3a16

Please sign in to comment.