#!/usr/bin/python3.5

import urllib.request

url="https://wxs.ign.fr/iqqdkoa235g0mf1gr3si0162/isochrone/isochrone.xml?location=935800,6850112&srs=epsg:2154&method=Time&graph=Voiture&graphName=Voiture&exclusions=&time=1200&holes=false&smoothing=false"
request = urllib.request.Request(url)
valid="False"
try:
    response = urllib.request.urlopen(request)
    htmlBytes = response.read()
    htmlStr = htmlBytes.decode("utf8")
    print ("something cool")
    valid="True"
except:
    print("something wrong")
if valid =="True" :
    tab_iso=htmlStr
    index1=tab_iso.find("wktGeometry")
    index2=tab_iso.rfind("wktGeometry")
    index3=len(tab_iso)
    iso=tab_iso[index1+12:index2-2]
    mLayer = QgsVectorLayer('Polygon?crs=epsg:4326', "test", "memory")
    mProvider = mLayer.dataProvider()
    geomWkt= iso
    geomPoly = QgsGeometry.fromWkt(geomWkt)
    feat = QgsFeature()
    feat.setGeometry(geomPoly)
    mProvider.addFeatures( [feat] )
    mLayer.commitChanges()
    mLayer.updateExtents()
    if mLayer.isValid():
        QgsProject.instance().addMapLayer(mLayer)