Example #1
0
 def wash(self, address):
     url = self.wash_uri(address)
     if not url:
         return None
     request = QNetworkRequest(QUrl(url))
     reply = QgsNetworkAccessManager.blockingGet(request)
     return json.loads(str(reply.content().data(), encoding="utf-8"))
Example #2
0
 def getRemoteTiles(self):
     query = QUrlQuery()
     url = QUrl(f"{self.url}/search")
     query.addQueryItem("q", self.search_str)
     query.addQueryItem("f", "pjson")
     url.setQuery(query.query())
     response = QgsNetworkAccessManager.blockingGet(
         QNetworkRequest(QUrl(url)))
     if response.error() != QNetworkReply.NoError:
         raise Exception(response.errorString())
     responsejson = json.loads(response.content().data())
     LOG.debug("response from data repository: %s" % responsejson)
     tiles = []
     for result in responsejson["results"]:
         itemid = result["id"]
         timestamp = self.dataTimestamp(itemid)
         if timestamp is None:
             status = self.NOT_INSTALLED
         elif timestamp < result["modified"]:
             status = self.UPDATABLE
         else:
             status = self.UP_TO_DATE
         tile = dict(result)
         tile["status"] = status
         tiles.append(tile)
     return tiles
Example #3
0
 def address_from_id(self, id):
     url = self.address_uri(id)
     # QgsMessageLog.logMessage(f"URL for id [{id}]: {url}",'Geokoder', Qgis.Info)
     if not url:
         return None
     request = QNetworkRequest(QUrl(url))
     reply = QgsNetworkAccessManager.blockingGet(request)
     return json.loads(str(reply.content().data(), encoding="utf-8"))
Example #4
0
class Nominatim:
    URL = "https://nominatim.openstreetmap.org/reverse"

    def __init__(self):
        self.nam = QgsNetworkAccessManager()

        # has public access methods
        self._status_code = 0
        self._error_string = ""

        # completely private
        self._content = dict()

    def do_request(self, point: QgsPointXY) -> None:
        query = QUrlQuery()
        query.addQueryItem("lat", str(point.y()))
        query.addQueryItem("lon", str(point.x()))
        query.addQueryItem("format", "json")

        url = QUrl(self.URL)
        url.setQuery(query)

        request = QNetworkRequest(url)
        request.setHeader(QNetworkRequest.UserAgentHeader,
                          "*****@*****.**")

        response = self.nam.blockingGet(request)
        self._status_code = response.attribute(
            QNetworkRequest.HttpStatusCodeAttribute)
        self._content = json.loads(bytes(response.content()))
        if self._content.get("error"):
            self._error_string = self._content["error"]
            return

    def get_point(self) -> QgsPointXY:
        return QgsPointXY(float(self._content["lon"]),
                          float(self._content["lat"]))

    def get_bbox_points(self) -> Tuple[QgsPointXY, QgsPointXY]:
        res_bbox: List[float] = [
            float(coord) for coord in self._content["boundingbox"]
        ]
        min_y, max_y, min_x, max_x = res_bbox

        return QgsPointXY(min_x, min_y), QgsPointXY(max_x, max_y)

    def get_attributes(self) -> Tuple[str, str]:
        return self._content["display_name"], self._content["licence"]

    @property
    def status_code(self):
        return self._status_code

    @property
    def error_string(self):
        return self._error_string
Example #5
0
    def run(self):
        """Do the work."""
        nam = QgsNetworkAccessManager()

        # Don't raise any error during processing, relay error reporting to self.finished()
        try:
            for i, bbox in enumerate(self.grid):
                if self.isCanceled():
                    return False

                # Get the filepath and create the output directory (if necessary)
                x, y = (
                    bbox.boundingBox().xMinimum(),
                    bbox.boundingBox().yMinimum(),
                )
                hemisphere = "S" if y < 0 else "N"
                dir_name = "%s%02d" % (hemisphere, abs(y))
                directory = self.output_dir.joinpath(dir_name)
                directory.mkdir(exist_ok=True)
                tile_name = "%s%02d%s%03d.hgt" % (
                    hemisphere,
                    abs(y),
                    "W" if x < 0 else "E",
                    abs(x),
                )
                filepath = self.output_dir.joinpath(dir_name, tile_name)
                self.tiles.append(filepath)
                if filepath.is_file():
                    QgsMessageLog.logMessage(
                        message=f"Already downloaded tile {tile_name}",
                        level=Qgis.Info,
                    )
                    continue

                # get the new tile if it didn't exist
                url = QUrl(
                    f"http://s3.amazonaws.com/elevation-tiles-prod/skadi/{dir_name}/{tile_name}.gz"
                )
                request = QNetworkRequest(url)
                reply: QgsNetworkReplyContent = nam.blockingGet(request)
                # turn the reply into a file object and write the decompressed file to disk
                with gzip.GzipFile(
                    fileobj=BytesIO(reply.content()), mode="rb"
                ) as gz:
                    with open(filepath, "wb") as f:
                        f.write(gz.read())

                self.setProgress((i / len(self.grid)) * 100)

            return True
        except Exception as e:
            self.exception = e
            return False
def get_resource(
    name: str,
    dominode_base_url: str,
    network_manager: QgsNetworkAccessManager,
    feedback: typing.Optional[QgsFeedback] = None,
) -> typing.Optional[typing.Dict]:
    url_query = QUrlQuery()
    url_query.addQueryItem('name', name)
    url = QUrl(
        f'{dominode_base_url}/dominode-validation/api/dominode-resources/')
    url.setQuery(url_query)
    request = QNetworkRequest(url)
    request.setHeader(QNetworkRequest.ContentTypeHeader, 'application/json')
    reply = network_manager.blockingGet(request, '', True, feedback=feedback)
    status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    result = None
    if status_code == 200:
        raw_string_contents = bytes(reply.content()).decode('utf-8')
        contents = json.loads(raw_string_contents)
        exists = contents.get('count', 0) > 0
        if exists:
            result = contents['results'][0]
    return result
Example #7
0
    def result(self, result):
        # See if OK was pressed
        if result:
            project = QgsProject.instance()
            # First get all the values of the GUI items
            crs_input = self.dlg.crs_input.crs()
            crs_out = QgsCoordinateReferenceSystem(
                'EPSG:4326')  # we need this to be WGS84 for Nominatim
            lineedit_text = self.dlg.lineedit_xy.value()

            # Protect the free text field for coordinates from generic user failure
            try:
                lineedit_yx = [
                    float(coord.strip()) for coord in lineedit_text.split(',')
                ]
            except:
                QMessageBox.critical(
                    self.iface.mainWindow(), 'QuickAPI error',
                    "Did you really specify a coordinate in comma-separated Lat/Long?\nExiting..."
                )
                return

            # Create a Point and transform if necessary
            point = QgsPointXY(*reversed(lineedit_yx))
            if crs_input.authid() != 'EPSG:4326':
                xform = QgsCoordinateTransform(crs_input, crs_out, project)
                point_transform = xform.transform(point)
                point = point_transform

            # Set up the GET Request to Nominatim
            query = QUrlQuery()
            query.addQueryItem('lat', str(point.y()))
            query.addQueryItem('lon', str(point.x()))
            query.addQueryItem('format', 'json')

            url = QUrl('https://nominatim.openstreetmap.org/reverse')
            url.setQuery(query)

            request = QNetworkRequest(url)
            request.setHeader(QNetworkRequest.UserAgentHeader,
                              '*****@*****.**')

            nam = QgsNetworkAccessManager()
            response: QgsNetworkReplyContent = nam.blockingGet(request)

            # Only process if HTTP status code is 200
            status_code = response.attribute(
                QNetworkRequest.HttpStatusCodeAttribute)
            if status_code == 200:
                # Get the content of the response and process it
                response_json = json.loads(bytes(response.content()))
                if response_json.get('error'):
                    QMessageBox.critical(
                        self.iface.mainWindow(), "Quick API error",
                        "The request was not processed succesfully!\n\n"
                        "Message:\n"
                        "{}".format(response_json['error']))
                    return

                x = float(response_json['lon'])
                y = float(response_json['lat'])
                address = response_json['display_name']
                license = response_json['licence']

                # Create the output memory layer
                layer_out = QgsVectorLayer(
                    "Point?crs=EPSG:4326&field=address:string&field=license:string",
                    "Nominatim Reverse Geocoding", "memory")

                # Create the output feature (only one here)
                point_out = QgsPointXY(x, y)
                feature = QgsFeature()
                feature.setGeometry(QgsGeometry.fromPointXY(point_out))
                feature.setAttributes([address, license])

                # Add feature to layer and layer to map
                layer_out.dataProvider().addFeature(feature)
                layer_out.updateExtents()
                project.addMapLayer(layer_out)

                # build bbox for auto-zoom feature
                bbox = [float(coord) for coord in response_json['boundingbox']]
                min_y, max_y, min_x, max_x = bbox
                bbox_geom = QgsGeometry.fromRect(
                    QgsRectangle(min_x, min_y, max_x, max_y))

                # Transform bbox if map canvas has a different CRS
                if project.crs().authid() != 'EPSG:4326':
                    xform = QgsCoordinateTransform(crs_out, project.crs(),
                                                   project)
                    bbox_geom.transform(xform)

                self.iface.mapCanvas().zoomToFeatureExtent(
                    QgsRectangle.fromWkt(bbox_geom.asWkt()))