1

I tried a code to get attribute values from internet. Line by line from console the code worked well. As soon I was running it as Python file, QGIS crashed without comments. Trying commenting lines out - the reason was in line that begins with

req=...

Is it usual, that code works well in console, but not as code from redactor?

import urllib.request

url1 = 'https://geoportaal.maaamet.ee/url/xgis-xy.php?'
for f in er_f.getFeatures():
    pt=f.geometry().centroid().asPoint()
    y=pt.x()
    x=pt.y()
    url2='x='+str(x)+'&y='+str(y)+'&d=1'
    url=url1+url2
    print(url)
    req = urllib.request.Request(url)
    response = urllib.request.urlopen(req)
    tunnus = response.read()
    print(tunnus)
1

The ssl certificates on the server seem to work not as expected for the quite outdated urllib requests module:

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)>

I've tried your code with a working example of coordinates as no one knows the feature layer you're using and used the requests module for python3.

The result works as expected even as a standalone python file:

import requests
url1 = 'https://geoportaal.maaamet.ee/url/xgis-xy.php?'
test = [{"x": 6515844.06, "y":547300.83},{"x": 6515814.06, "y":547779.83}]

for f in test:
    y=f["y"]
    x=f["x"]
    url2='x='+str(x)+'&y='+str(y)+'&d=1'
    url=url1+url2
    print(url)
    req = requests.get(url)
    response = req.text
    print(response)

enter image description here

|improve this answer|||||
  • 1
    Thank you. The new requests libray works well. – Marje Talvis Jan 14 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.