3

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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 11, 2021 at 15:40
5
  • Working fine for me with your code in 3.10. It just takes about one second to load. Commented 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!". Commented 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? Commented 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. Commented Mar 12, 2021 at 15:36
  • So I have tried my second sample on my home computer (same version of QGIS), and it works. Commented Mar 19, 2021 at 15:16

1 Answer 1

2

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)
answered Mar 12, 2021 at 21:31
6
  • 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. Commented 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 changed Commented 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. Commented 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 related Commented Mar 19, 2021 at 15:38
  • On all computers, I am on Windows 10. Commented Mar 19, 2021 at 15:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.