Skip to content

Commit

Permalink
restyle using black
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCaha committed Aug 8, 2023
1 parent 0fb74bc commit 542a6be
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 513 deletions.
19 changes: 9 additions & 10 deletions qmapshaper/classes/class_qmapshaper_command_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@


class QMapshaperCommandBuilder:

@staticmethod
def prepare_console_commands(input_data_path: str,
output_data_path: str,
command: str,
arguments: List[str],
clean_before: bool = False,
clean_after: bool = False) -> List[str]:

def prepare_console_commands(
input_data_path: str,
output_data_path: str,
command: str,
arguments: List[str],
clean_before: bool = False,
clean_after: bool = False,
) -> List[str]:
commands = []

commands.append(input_data_path)
Expand All @@ -38,9 +38,8 @@ def prepare_console_commands(input_data_path: str,

@staticmethod
def prepare_console_output_data(output_data_path: str) -> List[str]:

command = ["-o"]
command.append('format={}'.format(QMapshaperFile.get_format(output_data_path)))
command.append("format={}".format(QMapshaperFile.get_format(output_data_path)))
command.append(output_data_path)

return command
Expand Down
77 changes: 33 additions & 44 deletions qmapshaper/classes/class_qmapshaper_data_preparer.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
from typing import List, Union

from qgis.core import (QgsVectorLayer, QgsVectorLayerUtils, QgsMemoryProviderUtils,
QgsVectorFileWriter, QgsCoordinateTransformContext, QgsVectorLayerJoinInfo,
QgsField)
from qgis.PyQt.QtCore import QVariant

from processing.algs.gdal.GdalUtils import GdalUtils
from qgis.core import (
QgsCoordinateTransformContext,
QgsField,
QgsMemoryProviderUtils,
QgsVectorFileWriter,
QgsVectorLayer,
QgsVectorLayerJoinInfo,
QgsVectorLayerUtils,
)
from qgis.PyQt.QtCore import QVariant

from ..text_constants import TextConstants
from .class_qmapshaper_file import QMapshaperFile, QMapshaperGeojsonFile


class QMapshaperDataPreparer:

@staticmethod
def copy_to_memory_layer(layer: QgsVectorLayer) -> QgsVectorLayer:

memory_layer = QgsMemoryProviderUtils.createMemoryLayer(layer.name(), layer.fields(),
layer.wkbType(), layer.crs())
memory_layer = QgsMemoryProviderUtils.createMemoryLayer(
layer.name(), layer.fields(), layer.wkbType(), layer.crs()
)

memory_layer.startEditing()

Expand All @@ -30,9 +34,7 @@ def copy_to_memory_layer(layer: QgsVectorLayer) -> QgsVectorLayer:
return memory_layer

@staticmethod
def write_layer_with_minimal_attributes(layer: QgsVectorLayer, file: str,
col_index: Union[int, List[int]]) -> None:

def write_layer_with_minimal_attributes(layer: QgsVectorLayer, file: str, col_index: Union[int, List[int]]) -> None:
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = QMapshaperFile.driver_name()

Expand All @@ -41,30 +43,24 @@ def write_layer_with_minimal_attributes(layer: QgsVectorLayer, file: str,
else:
options.attributes = col_index

QgsVectorFileWriter.writeAsVectorFormatV3(layer=layer,
fileName=file,
transformContext=QgsCoordinateTransformContext(),
options=options)
QgsVectorFileWriter.writeAsVectorFormatV3(
layer=layer, fileName=file, transformContext=QgsCoordinateTransformContext(), options=options
)

@staticmethod
def write_layer_with_as_geojson(layer: QgsVectorLayer,
file: str,
decimal_precision: int = None) -> None:

def write_layer_with_as_geojson(layer: QgsVectorLayer, file: str, decimal_precision: int = None) -> None:
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = QMapshaperGeojsonFile.driver_name()

if decimal_precision:
options.layerOptions = ["COORDINATE_PRECISION={}".format(decimal_precision)]

QgsVectorFileWriter.writeAsVectorFormatV3(layer=layer,
fileName=file,
transformContext=QgsCoordinateTransformContext(),
options=options)
QgsVectorFileWriter.writeAsVectorFormatV3(
layer=layer, fileName=file, transformContext=QgsCoordinateTransformContext(), options=options
)

@staticmethod
def write_output_file(layer: QgsVectorLayer, file: str, layer_name: str) -> None:

fields = layer.fields()

fields_indexes = [x for x in range(0, fields.count())]
Expand All @@ -80,17 +76,14 @@ def write_output_file(layer: QgsVectorLayer, file: str, layer_name: str) -> None
options.attributes = fields_indexes
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile

QgsVectorFileWriter.writeAsVectorFormatV3(layer=layer,
fileName=file,
transformContext=QgsCoordinateTransformContext(),
options=options)
QgsVectorFileWriter.writeAsVectorFormatV3(
layer=layer, fileName=file, transformContext=QgsCoordinateTransformContext(), options=options
)

@staticmethod
def join_fields_back(layer_to_join_to: QgsVectorLayer,
layer_to_join_from: QgsVectorLayer,
prefix: str = "",
join_fids_back=True) -> None:

def join_fields_back(
layer_to_join_to: QgsVectorLayer, layer_to_join_from: QgsVectorLayer, prefix: str = "", join_fids_back=True
) -> None:
join = QgsVectorLayerJoinInfo()
join.setTargetFieldName(TextConstants.JOIN_FIELD_NAME)
join.setJoinLayer(layer_to_join_from)
Expand All @@ -105,32 +98,28 @@ def join_fields_back(layer_to_join_to: QgsVectorLayer,

@staticmethod
def add_mapshaper_id_field(layer: QgsVectorLayer) -> int:

layer.startEditing()

field_index = layer.addExpressionField(
"$id", QgsField(TextConstants.JOIN_FIELD_NAME, QVariant.Int))
field_index = layer.addExpressionField("$id", QgsField(TextConstants.JOIN_FIELD_NAME, QVariant.Int))

layer.commitChanges()

return field_index

@staticmethod
def add_mapshaper_generalization_field(layer: QgsVectorLayer,
ids: List[int],
selected_generalize: bool = True) -> int:

def add_mapshaper_generalization_field(
layer: QgsVectorLayer, ids: List[int], selected_generalize: bool = True
) -> int:
ids_string = ",".join([str(x) for x in ids])

expr = f'array_contains(array({ids_string}), $id)'
expr = f"array_contains(array({ids_string}), $id)"

if not selected_generalize:
expr = "NOT " + expr

layer.startEditing()

field_index = layer.addExpressionField(
expr, QgsField(TextConstants.GENERALIZATION_FIELD_NAME, QVariant.Bool))
field_index = layer.addExpressionField(expr, QgsField(TextConstants.GENERALIZATION_FIELD_NAME, QVariant.Bool))

layer.commitChanges()

Expand Down
7 changes: 0 additions & 7 deletions qmapshaper/classes/class_qmapshaper_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@


class QMapshaperFile:

@staticmethod
def get_format(file_name: str) -> str:

file_types = [QMapshaperFile, QMapshaperGeojsonFile, QMapshaperTopoJsonFile]

for file_type in file_types:
Expand All @@ -18,7 +16,6 @@ def get_format(file_name: str) -> str:

@staticmethod
def random_temp_filename() -> str:

filename = "{}.{}".format(uuid4(), QMapshaperFile.extension())

return QgsProcessingUtils.generateTempFilename(filename)
Expand All @@ -37,10 +34,8 @@ def extension() -> str:


class QMapshaperGeojsonFile:

@staticmethod
def random_temp_filename() -> str:

filename = "{}.{}".format(uuid4(), QMapshaperGeojsonFile.extension())

return QgsProcessingUtils.generateTempFilename(filename)
Expand All @@ -59,10 +54,8 @@ def extension() -> str:


class QMapshaperTopoJsonFile:

@staticmethod
def random_temp_filename() -> str:

filename = "{}.{}".format(uuid4(), QMapshaperTopoJsonFile.extension())

return QgsProcessingUtils.generateTempFilename(filename)
Expand Down
22 changes: 7 additions & 15 deletions qmapshaper/classes/class_qmapshaper_paths.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
from pathlib import Path
import platform
from pathlib import Path

from processing.core.ProcessingConfig import ProcessingConfig

from ..text_constants import TextConstants
from ..utils import log


class QMapshaperPaths:

@staticmethod
def mapshaper_executable_path() -> str:

mapshaper_folder = QMapshaperPaths.mapshaper_folder()

if mapshaper_folder:
path = QMapshaperPaths.subfolder_bin(
mapshaper_folder) / QMapshaperPaths.mapshaper_command_name()
path = QMapshaperPaths.subfolder_bin(mapshaper_folder) / QMapshaperPaths.mapshaper_command_name()
return path.as_posix()

return ""

@staticmethod
def subfolder_bin(folder) -> Path:

command = "mapshaper"

exec_path = Path(folder) / command
Expand All @@ -39,18 +34,19 @@ def subfolder_bin(folder) -> Path:

@staticmethod
def mapshaper_folder() -> str:

folder = ProcessingConfig.getSetting(TextConstants.MAPSHAPER_FOLDER)

if not folder:
folder = QMapshaperPaths.guess_mapshaper_folder()

return folder if folder else ''
return folder if folder else ""

@staticmethod
def guess_mapshaper_folder() -> str:

from .class_qmapshaper_runner import MapshaperProcessChecker, NpmPackageLocationCheckerProcess
from .class_qmapshaper_runner import (
MapshaperProcessChecker,
NpmPackageLocationCheckerProcess,
)

# globally available
mp = MapshaperProcessChecker("")
Expand All @@ -69,7 +65,6 @@ def guess_mapshaper_folder() -> str:

@staticmethod
def mapshaper_command_name() -> str:

tool = ProcessingConfig.getSetting(TextConstants.MAPSHAPER_TOOL_NAME)

if not tool:
Expand All @@ -86,13 +81,10 @@ def guess_mapshaper_command_name() -> str:

@staticmethod
def mapshaper_command_call() -> str:

mapshaper_bin_folder = QMapshaperPaths.mapshaper_executable_path()

if mapshaper_bin_folder:

return mapshaper_bin_folder

else:

return QMapshaperPaths.mapshaper_command_name()
Loading

0 comments on commit 542a6be

Please sign in to comment.