Skip to content

Commit

Permalink
Fix #40 error when the layer name contains non-ascii characters
Browse files Browse the repository at this point in the history
  • Loading branch information
elesdoar committed Mar 14, 2016
1 parent 07c9d76 commit 5fb2b4f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<a name="0.2.4"></a>
### 0.2.4 (2016-03-14)

#### Refactor

* Change 'Add CartoDB Layer' button to layer toolbar on QGIS >= 2.12. (Left side on QGIS).
* Change 'Add SQL CartoDB Layer' button to layer toolbar on QGIS >= 2.12. (Left side on QGIS).

#### Fixes

* Fix #40 error when the layer name contains non-ascii characters

<a name="0.2.3"></a>
### 0.2.3 (2015-10-04)

Expand Down
12 changes: 10 additions & 2 deletions dialogs/Upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from QgisCartoDB.layers import CartoDBLayer
from QgisCartoDB.dialogs.Basic import CartoDBPluginUserDialog
from QgisCartoDB.ui.Upload import Ui_Upload
from QgisCartoDB.utils import getSize, checkTempDir, zipLayer, checkCartoDBId
from QgisCartoDB.utils import getSize, checkTempDir, zipLayer, checkCartoDBId, stripAccents
from QgisCartoDB.widgets import CartoDBLayerListItem

import math
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(self, iface, toolbar, parent=None):
self.ui.uploadBar.hide()
self.ui.uploadingLB.hide()
for id_ly, ly in layers.iteritems():
qDebug('Layer id {}'.format(id_ly))
qDebug('Layer id {}'.format(stripAccents(id_ly)))
if ly.type() == QgsMapLayer.VectorLayer and not isinstance(ly, CartoDBLayer):
item = QListWidgetItem(self.ui.layersList)
widget = CartoDBLayerListItem(ly.name(), ly, getSize(ly), ly.dataProvider().featureCount())
Expand All @@ -87,6 +87,14 @@ def uploadZip(self, zip_path, widget, convertLayer=None, convert=False):
def completeUpload(data):
"""On complete upload"""
timer = QTimer(self)
qDebug('data: {}'.format(str(data)))

if 'error' in data and data['error'] is not None:
self.ui.bar.clearWidgets()
self.ui.bar.pushMessage(QApplication.translate('CartoDBPlugin', 'Error uploading layer: {}').format(data['error']),
level=QgsMessageBar.CRITICAL, duration=5)
widget.setStatus('Error', 0)
return

self.ui.uploadBar.hide()
self.ui.uploadingLB.hide()
Expand Down
4 changes: 4 additions & 0 deletions i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<source>Upload Complete</source>
<translation>Upload Complete</translation>
</message>
<message>
<source>Error uploading layer: {}</source>
<translation>Error uploading layer: {}</translation>
</message>
<message>
<source>Using {} of {}</source>
<translation>Using {} of {}</translation>
Expand Down
4 changes: 4 additions & 0 deletions i18n/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<source>Upload Complete</source>
<translation>Capas Cargadas</translation>
</message>
<message>
<source>Error uploading layer: {}</source>
<translation>Error subiendo capa: {}</translation>
</message>
<message>
<source>Using {} of {}</source>
<translation>Usando {} de {}</translation>
Expand Down
6 changes: 5 additions & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ about=
- simplejson
- certifi
email=michaelsalgado@gkudos.com
changelog=0.2.3
changelog=0.2.4
- Change 'Add CartoDB Layer' button to layer toolbar. (Left side on QGIS).
- Change 'Add SQL CartoDB Layer' button to layer toolbar. (Left side on QGIS).
- Fix error when the layer name contains non-ascii characters
0.2.3
- Fix error updating recently uploaded data.
0.2.2
- Change privacy for maps, (public checkbox).
Expand Down
22 changes: 13 additions & 9 deletions utils/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import random
import tempfile
import zipfile
import unicodedata

def stripAccents(text):
"""Strips accent to text"""
return ''.join(c for c in unicodedata.normalize('NFD', text) if unicodedata.category(c) != 'Mn')

def randomColor(mix=(255, 255, 255)):
"""Generate Random Color"""
Expand Down Expand Up @@ -78,34 +82,34 @@ def zipLayer(layer):
file_path = file_path.replace('/vsizip/', '')
if layer.storageType() in ['ESRI Shapefile', 'GPX', 'GeoJSON', 'LIBKML']:
return file_path

_file = QFile(file_path)
file_info = QFileInfo(_file)

dirname = file_info.dir().absolutePath()
filename = file_info.completeBaseName()
filename = stripAccents(file_info.completeBaseName())
layername = stripAccents(layer.name())

tempdir = checkTempDir()

zip_path = os.path.join(tempdir, layer.name() + '.zip')
zip_path = os.path.join(tempdir, layername + '.zip')
zip_file = zipfile.ZipFile(zip_path, 'w')


if layer.storageType() == 'ESRI Shapefile':
for suffix in ['.shp', '.dbf', '.prj', '.shx']:
if os.path.exists(os.path.join(dirname, filename + suffix)):
zip_file.write(os.path.join(dirname, filename + suffix), layer.name() + suffix, zipfile.ZIP_DEFLATED)
zip_file.write(os.path.join(dirname, filename + suffix), layername + suffix, zipfile.ZIP_DEFLATED)
elif layer.storageType() == 'GeoJSON':
zip_file.write(file_path, layer.name() + '.geojson', zipfile.ZIP_DEFLATED)
zip_file.write(file_path, layername + '.geojson', zipfile.ZIP_DEFLATED)
elif layer.storageType() == 'GPX':
zip_file.write(file_path, layer.name() + '.gpx', zipfile.ZIP_DEFLATED)
zip_file.write(file_path, layername + '.gpx', zipfile.ZIP_DEFLATED)
elif layer.storageType() == 'LIBKML':
zip_file.write(file_path, layer.name() + '.kml', zipfile.ZIP_DEFLATED)
zip_file.write(file_path, layername + '.kml', zipfile.ZIP_DEFLATED)
else:
geo_json_name = os.path.join(tempfile.tempdir, layer.name())
geo_json_name = os.path.join(tempfile.tempdir, layername)
error = QgsVectorFileWriter.writeAsVectorFormat(layer, geo_json_name, "utf-8", None, "GeoJSON")
if error == QgsVectorFileWriter.NoError:
zip_file.write(geo_json_name + '.geojson', layer.name() + '.geojson', zipfile.ZIP_DEFLATED)
zip_file.write(geo_json_name + '.geojson', layername + '.geojson', zipfile.ZIP_DEFLATED)
zip_file.close()
return zip_path

Expand Down

0 comments on commit 5fb2b4f

Please sign in to comment.