I am working on importing weather data from an online source into QGIS 3.10 using Python. When I download the data and use the 'vsizip,' it works perfectly fine and loads the file into my project (below).
zip_uri = '/vsizip/C:/Users/camerony/Downloads/day1otlk-shp.zip'
shp = QgsVectorLayer(zip_uri, 'SPC Day 1 Categorical Outlook', 'ogr')
if not shp.isValid():
print ("Layer failed to load!")
else:
QgsProject.instance().addMapLayer(shp)
However, when I try to use the 'vsizip' and 'vsicurl,' it does not work, and it cannot load the file.
zip_uri = '/vsizip//vsicurl/https://www.spc.noaa.gov/products/outlook/day1otlk-shp.zip'
shp = QgsVectorLayer(zip_uri, 'SPC Day 1 Categorical Outlook', 'ogr')
if not shp.isValid():
print ("Layer failed to load!")
else:
QgsProject.instance().addMapLayer(shp)
How would I be able to load this zipped folder and associated shapefiles directly from a website?
-
Working fine for me with your code in 3.10. It just takes about one second to load.MrXsquared– MrXsquared2021年03月11日 15:54:28 +00:00Commented Mar 11, 2021 at 15:54
-
The first code works for me, but when I run the second code, it returns my print statement "Layer failed to load!".Cameron Y– Cameron Y2021年03月11日 16:16:49 +00:00Commented Mar 11, 2021 at 16:16
-
Thats weird, because your second one works fine for me... Are you using a proxy or something like that?MrXsquared– MrXsquared2021年03月11日 16:23:12 +00:00Commented Mar 11, 2021 at 16:23
-
I am not. I have yet to try the code on my home computer and see if it works there, though.Cameron Y– Cameron Y2021年03月12日 15:36:37 +00:00Commented Mar 12, 2021 at 15:36
-
So I have tried my second sample on my home computer (same version of QGIS), and it works.Cameron Y– Cameron Y2021年03月19日 15:16:28 +00:00Commented Mar 19, 2021 at 15:16
1 Answer 1
I've tried your second sample without issue and it opens behind the scene day1otlk_cat.shp
As your remote zip file contained more than one shp, I've tried using full path to shapefiles to be explicit and it works too.
First sample is using explicit shp name to avoid "automagically" behaviour (your issue could be related)
zipbase = '/vsizip//vsicurl/https://www.spc.noaa.gov/products/outlook/day1otlk-shp.zip'
shp_name = 'day1otlk_cat.shp'
vl = QgsVectorLayer(f"{zipbase}/{shp_name}", 'SPC Day 1 Categorical Outlook', 'ogr')
if not vl.isValid():
print ("Layer failed to load!")
else:
QgsProject.instance().addMapLayer(vl)
The second sample list layers from your remote zip, loop to create multiples layers and add them with an unique operation
from gdal import ogr
zipbase = '/vsizip//vsicurl/https://www.spc.noaa.gov/products/outlook/day1otlk-shp.zip'
shapefiles = [layer.GetName() for layer in ogr.Open(zipbase)]
layers = []
for shp_name in shapefiles:
shp_path = f"{zipbase}/{shp_name}.shp"
vl = QgsVectorLayer(shp_path, shp_name, 'ogr')
layers.append(vl)
QgsProject.instance().addMapLayers(layers)
-
I tried both of these methods. The first one works on my home computer but does not work on either of my work computers. It returns the "Layer failed to load!" statement. The second method returns an error for me saying that the NoneType is not iterable, referring to the zipped folder.Cameron Y– Cameron Y2021年03月19日 15:04:18 +00:00Commented Mar 19, 2021 at 15:04
-
Corrected 1st one. Better? Both samples tested on QGIS 3.10 but with a recent GDAL version e.g v3.1.4. Did you look at your GDAL version within QGIS e.g menu "Help" > "About". Your issue could be related how vsizip/vsizip support has changedThomasG77– ThomasG772021年03月19日 15:23:25 +00:00Commented Mar 19, 2021 at 15:23
-
The first one is not working for me here at work. Could it possibly be some IT issue that would be blocking it form working? For the second method, the GDAL is v3.1.4.Cameron Y– Cameron Y2021年03月19日 15:36:29 +00:00Commented Mar 19, 2021 at 15:36
-
What is your OS? On Ubuntu, on home network so sure no network restriction. Asking to recommend a tool to check network calls to troubleshot if relatedThomasG77– ThomasG772021年03月19日 15:38:39 +00:00Commented Mar 19, 2021 at 15:38
-
On all computers, I am on Windows 10.Cameron Y– Cameron Y2021年03月19日 15:50:30 +00:00Commented Mar 19, 2021 at 15:50