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

Main granice patryk #21

Merged
merged 8 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
26 changes: 15 additions & 11 deletions Searcher/searchAddress.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json
import os
from http.client import IncompleteRead
from json import JSONDecodeError
from typing import Union, Tuple
from urllib.error import HTTPError, URLError
from urllib.parse import quote
from urllib.request import urlopen

from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsGeometry, QgsFeature, QgsField, QgsFields, \
QgsProject, QgsVectorLayer, QgsMessageLog, Qgis
QgsVectorLayer, QgsMessageLog, Qgis
from qgis.utils import iface

from ..utils import tr, add_map_layer_to_group, search_group_name
from ..utils import tr, add_map_layer_to_group, search_group_name, project


class SearchAddress:
Expand All @@ -35,7 +37,7 @@ def __init__(self):
QgsField("y", QVariant.Double, "double", 10, 4),
]

def fetch_address(self, address):
def fetch_address(self, address: str) -> None:
self.address = address
uug = 'https://services.gugik.gov.pl/uug?request=GetAddress&address='
url = uug + quote(self.address)
Expand All @@ -54,7 +56,7 @@ def fetch_address(self, address):
return
self.res.decode()

def get_layer(self):
def get_layer(self) -> Union[bool, Tuple[str]]:
req_type = self.jres['type']
if req_type in ['city', 'address']:
org = 'MultiPoint?crs=epsg:2180&index=yes'
Expand All @@ -73,8 +75,9 @@ def get_layer(self):
return False
return org, obj_type, qml

def get_layer_data(self, org, obj_type, qml):
lyr = QgsProject.instance().mapLayersByName(obj_type)
def get_layer_data(self, org: str, obj_type: str,
qml: str) -> QgsVectorLayer:
lyr = project.mapLayersByName(obj_type)
if lyr:
return lyr[0]

Expand All @@ -94,10 +97,11 @@ def get_layer_data(self, org, obj_type, qml):
lyr.loadNamedStyle(os.path.join(direc, qml))
return lyr

def process_results(self):
def process_results(self) -> Union[
Tuple[bool, str], Tuple[bool, QgsFeature]]:
try:
self.jres = json.loads(self.res)
except Exception:
except JSONDecodeError:
return False, tr('Cannot parse results.')

if 'found objects' in self.jres:
Expand Down Expand Up @@ -145,14 +149,14 @@ def process_results(self):

return True, feats

def zoom_to_feature(self, layer):
layer = QgsProject.instance().mapLayersByName(layer)[0]
def zoom_to_feature(self, layer: str) -> None:
layer = project.mapLayersByName(layer)[0]
iface.mapCanvas().zoomScale(500)
layer.selectByIds([len(layer)])
iface.mapCanvas().zoomToSelected(layer)
layer.removeSelection()

def add_feats(self, feats):
def add_feats(self, feats: QgsFeature) -> Union[bool, None]:
if isinstance(feats, str):
pass
else:
Expand Down
38 changes: 18 additions & 20 deletions Searcher/searchParcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,52 @@
import re
import socket
from http.client import IncompleteRead
from typing import List
from urllib.error import HTTPError, URLError
from urllib.request import urlopen

from qgis.core import QgsGeometry, QgsFeature, \
QgsProject, QgsVectorLayer, Qgis
QgsVectorLayer, Qgis
from qgis.utils import iface

from ..utils import tr, CustomMessageBox, search_group_name, \
add_map_layer_to_group
add_map_layer_to_group, project


class FetchULDK:
def __init__(self, params=None):
self.params = params
self.responce = []

def fetch_list(self, area, teryt):
def fetch_list(self, area: str, teryt: str) -> bool:
map(str, teryt)
if area not in {'powiat', 'gmina', 'obreb'}:
self.responce = []
return False
self.params = f'obiekt={area}&wynik=nazwa%2Cteryt&teryt={teryt}&'
return self.fetch()

def fetch_voivodeships(self):
def fetch_voivodeships(self) -> bool:
self.params = 'obiekt=wojewodztwo&wynik=nazwa%2Cteryt&teryt=&'
return self.fetch()

def fetch_parcel(self, teryt):
def fetch_parcel(self, teryt: str) -> bool:
self.responce = []
if not isinstance(teryt, str):
return False
self.params = f'request=GetParcelById&id={teryt}&result=' + \
'geom_wkt,teryt,voivodeship,county,region,commune,parcel'
return self.fetch()

def fetch_in_point(self, coords):
# TODO: Dodac pobieranie działki w pkt po kliknieciu
pass

def fetch(self):
def fetch(self) -> bool:
if '- gmina' in self.params or '- miasto' in self.params:
flag = self.params.find('-')
self.params = self.params[0:flag]
url = f'https://uldk.gugik.gov.pl/?{self.params}'
self.responce = []
try:
with urlopen(url, timeout=19) as r:
content = r.read()
with urlopen(url, timeout=19) as url_handler:
content = url_handler.read()
except IncompleteRead:
CustomMessageBox(None,
f"{tr('Error!')} {tr('Service returned incomplete responce.')}").button_ok()
Expand All @@ -75,10 +72,11 @@ def fetch(self):
f"{tr('Service did not find any matches, wrong plot number.')}").button_ok()
return False

self.responce = self.natural_sort([x for x in res[1:] if x != ''])
self.responce = self.natural_sort(
[ter for ter in res[1:] if ter != ''])
return True

def natural_sort(self, list):
def natural_sort(self, list: List[str]) -> List[str]:
convert = lambda text: int(text) if text.isdigit() else text.lower()
alpha_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(list, key=alpha_key)
Expand All @@ -97,8 +95,8 @@ def __init__(self):
self.lyr_name, 'memory'
)

def get_layer(self):
lyr = QgsProject.instance().mapLayersByName(self.lyr_name)
def get_layer(self) -> None:
lyr = project.mapLayersByName(self.lyr_name)
if len(lyr) > 0:
self.lyr = lyr[0]
return
Expand All @@ -108,7 +106,7 @@ def get_layer(self):
self.lyr.loadNamedStyle(
os.path.join(direc, 'layer_style', 'dzialki.qml'))

def parse_responce(self, resp):
def parse_responce(self, resp: List[str]) -> None:
feats = []
for row in resp:
ft = self._create_feature(row)
Expand All @@ -129,14 +127,14 @@ def parse_responce(self, resp):
Qgis.Warning
)

def zoom_to_feature(self, layer):
layer = QgsProject.instance().mapLayersByName(layer)[0]
def zoom_to_feature(self, layer: str) -> None:
layer = project.mapLayersByName(layer)[0]
iface.mapCanvas().zoomScale(500)
layer.selectByIds([len(layer)])
iface.mapCanvas().zoomToSelected(layer)
layer.removeSelection()

def _create_feature(self, row):
def _create_feature(self, row: str) -> QgsFeature:
if row[:4].upper() == 'SRID':
row = row[row.index(';') + 1:]
feat = QgsFeature()
Expand Down
Loading