Skip to content

Commit

Permalink
Merge pull request #38 from benoitblanc/handle_zip_shp
Browse files Browse the repository at this point in the history
Handle zip format
  • Loading branch information
manisandro authored Mar 4, 2022
2 parents d97cd45 + 8e591c1 commit 71bee66
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
19 changes: 18 additions & 1 deletion plugins/themes/controllers/files_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from zipfile import ZipFile

from flask import flash, redirect, render_template, url_for
from werkzeug.utils import secure_filename
Expand Down Expand Up @@ -125,7 +126,17 @@ def upload_layer(self):
filename = secure_filename(f.filename)
try:
f.save(os.path.join(self.resources_path, filename))
flash("File '{}' successfully uploaded".format(filename),
is_zip_file = os.path.splitext(filename)[1] == '.zip'
if (is_zip_file):
self.app.logger.info(f"Extracting files from file {filename}...")
extensions = ('shp', 'shx', 'dbf', 'prj', 'cpg', 'geojson', 'kml', 'gpkg')
with ZipFile(os.path.join(self.resources_path, filename), 'r') as zip:
for file in zip.namelist():
if file.endswith(extensions):
self.app.logger.info(f"Extracting {file} from {filename}")
zip.extract(file, os.path.join(self.resources_path))
os.remove(os.path.join(self.resources_path, filename))
flash("File '{}' successfully uploaded{}".format(filename, " and extracted" if is_zip_file else ""),
'success')
return redirect(url_for('files'))
except IOError as e:
Expand All @@ -152,6 +163,12 @@ def upload_layer(self):
def delete_layer(self, layername):
"""Delete layer file."""
try:
is_shp_file = os.path.splitext(layername)[1] == '.shp'
if is_shp_file:
# Also remove all extensions that could be with SHP layer
extensions = ['.shx', '.dbf', '.prj', '.cpg']
name = os.path.splitext(layername)[0]
[os.remove(os.path.join(self.resources_path, name + ext)) for ext in extensions if os.path.exists(os.path.join(self.resources_path, name + ext))]
os.remove(os.path.join(self.resources_path, layername))
return redirect(url_for('files'))
except IOError as e:
Expand Down
8 changes: 4 additions & 4 deletions plugins/themes/forms/file_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
class LayerForm(FlaskForm):
"""Main form for Geospatial layer GUI"""

upload = FileField('Geospatial Layer', validators=[
upload = FileField('Geospatial Layer (.geojson, .kml, .gpkg, .shp, .zip)', validators=[
FileRequired(),
FileAllowed(['geojson', 'kml', 'gpkg', 'shp', 'dbf', 'shx', 'cpg', 'prj'],
'Please only use geospatial files (geojson, kml, gpkg, shp) !')
FileAllowed(['geojson', 'kml', 'gpkg', 'shp', 'dbf', 'shx', 'cpg', 'prj', 'zip'],
'Please only use geospatial files (geojson, kml, gpkg, shp, zip) !')
])
submit = SubmitField("Upload")

class ProjectForm(FlaskForm):
"""Main form for QGS Project GUI"""

upload = FileField('QGIS Project', validators=[
upload = FileField('QGIS Project (.qgs)', validators=[
FileRequired(),
FileAllowed(['qgs'], 'Please only use QGS projects !')
])
Expand Down

0 comments on commit 71bee66

Please sign in to comment.