Skip to content

Commit

Permalink
Added resources
Browse files Browse the repository at this point in the history
  • Loading branch information
babayoshihiko committed May 13, 2024
1 parent fbf59a4 commit 8c3793b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
26 changes: 23 additions & 3 deletions jpDataDownloader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
import urllib.request
from qgis.core import QgsMessageLog, Qgis
from qgis.PyQt.QtWidgets import QApplication, QWidget, QProgressBar
import sys
import urllib.request
import zipfile

class Downloader(QWidget):

Expand All @@ -26,5 +28,23 @@ def Handle_Progress(self, blocknum, blocksize, totalsize):
# method to download any file using urllib
def Download(self, url, filename, progressBar):
self._progressBar = progressBar
urllib.request.urlretrieve(url, filename, self.Handle_Progress)
self._progressBar.setValue(100)
self._progressBar.setValue(0)
try:
response = urllib.request.urlopen(url)
if response.getcode() == 200:
content_type = response.headers.get('Content-Type')
if content_type == 'application/zip':
urllib.request.urlretrieve(url, filename, self.Handle_Progress)
self._progressBar.setValue(100)
try:
with zipfile.ZipFile(filename, 'r') as zip_ref:
# Check if the zipfile is readable
zip_ref.testzip()
return True
except zipfile.BadZipFile:
QgsMessageLog.logMessage('The downloaded zipfile is corrupt.', 'jpdata', level=QgsMessageLog.WARNING)
return False # Zipfile is corrupt
except urllib.error.URLError as e:
QgsMessageLog.logMessage(str(e) + ' (' + url + ')', 'jpdata', level=Qgis.Warning)
return False # Zipfile is corrupt

21 changes: 14 additions & 7 deletions jpDataUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
***************************************************************************/
"""

from qgis.core import QgsMessageLog, Qgis
import os, csv


Expand Down Expand Up @@ -237,14 +238,20 @@ def getTilesFromCsv():
rows = list(csvreader)
return rows

def findShpFile(folderPath, row, code_pref):
def findShpFile(folderPath, row, code_pref, code_muni = '', name_muni = ''):
shpFile = None
if os.path.exists(folderPath + '/' + row['shp'].replace(u'code_pref', code_pref)):
shpFile = folderPath + '/' + row['shp'].replace(u'code_pref', code_pref)
elif os.path.exists(folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '/' + row['shp'].replace(u'code_pref', code_pref)):
shpFile = folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '/' + row['shp'].replace(u'code_pref', code_pref)
elif os.path.exists(folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '\\' + row['shp'].replace(u'code_pref', code_pref)):
shpFile = folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '\\' + row['shp'].replace(u'code_pref', code_pref)
shpFileTarget = row['shp'].replace('code_pref', code_pref)
shpFileTarget = shpFileTarget.replace('code_muni', code_muni)
shpFileTarget = shpFileTarget.replace('name_muni', name_muni)

if os.path.exists(folderPath + '/' + shpFileTarget):
shpFile = folderPath + '/' + shpFileTarget
elif os.path.exists(folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '/' + shpFileTarget):
shpFile = folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '/' + shpFileTarget
elif os.path.exists(folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '\\' + shpFileTarget):
shpFile = folderPath + '/' + row['altdir'].replace(u'code_pref', code_pref) + '\\' + shpFileTarget
else:
QgsMessageLog.logMessage('Cannot find the file ' + shpFileTarget, 'jpdata', level=QgsMessageLog.WARNING)

return shpFile

4 changes: 2 additions & 2 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
name=jpdata
qgisMinimumVersion=3.0
description=Download and load various data of Japan
version=0.1
version=0.3
author=Yoshihiko Baba
[email protected]

Expand All @@ -26,7 +26,7 @@ hasProcessingProvider=no
# Tags are comma separated with spaces allowed
tags=python, Japan

homepage=https://www.uclmail.net/users/babayoshihiko/
homepage=https://github.com/babayoshihiko/jpdata
category=Plugins
icon=icon.png
# experimental flag
Expand Down
2 changes: 1 addition & 1 deletion zip_plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cp -R ../jpdata ./jpdata
rm -f jpdata/.gitignore
rm -rf jpdata/__pycache__
rm -rf jpdata/.git
rm -f jpdata/resources_rc.py
#rm -f jpdata/resources.py
rm -f jpdata/i18n.sh
rm -f jpdata/zip_plugin.sh
rm -f jpdata/i18n/jpdata_ja.ts
Expand Down

0 comments on commit 8c3793b

Please sign in to comment.