Skip to content

Commit

Permalink
Fix #15 again, filter by current extent
Browse files Browse the repository at this point in the history
  • Loading branch information
elesdoar committed May 25, 2015
1 parent 9779ac7 commit c8cf243
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 6 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<a name="0.1.3"></a>
### 0.1.3 (2015-05-24)

#### New Features

* Filter tables by current extent.
* Add more info for table list.
* Use threads for download data.

#### Fixes.

* Fix error at multiusers accounts #15, again.

<a name="0.1.2"></a>
### 0.1.2 (2015-05-07)

Expand Down
4 changes: 2 additions & 2 deletions CartoDBPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def run(self):
self.iface.mainWindow().statusBar().showMessage("Processed {} %".format(0))
for i, table in enumerate(selectedItems):
widget = dlg.getItemWidget(table)
worker = CartoDBLayerWorker(self.iface, widget.tableName, widget.tableOwner, dlg)
worker = CartoDBLayerWorker(self.iface, widget.tableName, widget.tableOwner, dlg, filterByExtent=dlg.filterByExtent())
worker.finished.connect(self.addLayer)
self.worker = worker
worker.load()
Expand Down Expand Up @@ -171,7 +171,7 @@ def addSQL(self):
sql = dlg.getQuery()
progressMessageBar, progress = self.addLoadingMsg(1)
QgsMessageLog.logMessage('SQL: ' + sql, 'CartoDB Plugin', QgsMessageLog.INFO)
layer = CartoDBLayer(self.iface, 'SQLQuery', dlg.currentUser, dlg.currentApiKey, sql)
layer = CartoDBLayer(self.iface, 'SQLQuery', dlg.currentUser, dlg.currentApiKey, sql=sql)
QgsMapLayerRegistry.instance().addMapLayer(layer)
self.layers.append(layer)
progress.setValue(1)
Expand Down
3 changes: 3 additions & 0 deletions dialogs/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def getTablesListSelectedItems(self):
def getItemWidget(self, item):
return self.ui.tablesList.itemWidget(item)

def filterByExtent(self):
return self.ui.extentCH.isChecked()

@pyqtSlot()
def connect(self):
# Get tables from CartoDB.
Expand Down
11 changes: 9 additions & 2 deletions layers/CartoDBLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CartoDBLayer(QgsVectorLayer):
LAYER_TNAME_PROPERTY = 'tableName'
LAYER_SQL_PROPERTY = 'cartoSQL'

def __init__(self, iface, tableName, user, apiKey, owner=None, sql=None, geoJSON=None):
def __init__(self, iface, tableName, user, apiKey, owner=None, sql=None, geoJSON=None, filterByExtent=False):
# SQLite available?
self.iface = iface
self.user = user
Expand All @@ -63,6 +63,9 @@ def __init__(self, iface, tableName, user, apiKey, owner=None, sql=None, geoJSON

if sql is None:
sql = 'SELECT * FROM ' + ((owner + '.') if owner is not None else '') + self.cartoTable
if filterByExtent:
extent = self.iface.mapCanvas().extent()
sql = sql + " WHERE ST_Intersects(ST_GeometryFromText('{}', 4326), the_geom)".format(extent.asWktPolygon())
else:
self.forceReadOnly = True

Expand Down Expand Up @@ -359,13 +362,14 @@ class CartoDBLayerWorker(QObject):
finished = pyqtSignal(CartoDBLayer)
error = pyqtSignal(Exception, basestring)

def __init__(self, iface, tableName, owner=None, dlg=None, sql=None):
def __init__(self, iface, tableName, owner=None, dlg=None, sql=None, filterByExtent=False):
QObject.__init__(self)
self.iface = iface
self.owner = owner
self.tableName = tableName
self.dlg = dlg
self.sql = sql
self.filterByExtent = filterByExtent

def load(self):
worker = CartoDBPluginWorker(self, 'loadLayer')
Expand All @@ -390,6 +394,9 @@ def _loadData(self, geoJSON):
def loadLayer(self):
if self.sql is None:
sql = 'SELECT * FROM ' + ((self.owner + '.') if self.owner is not None else '') + self.tableName
if self.filterByExtent:
extent = self.iface.mapCanvas().extent()
sql = sql + " WHERE ST_Intersects(ST_GeometryFromText('{}', 4326), the_geom)".format(extent.asWktPolygon())
else:
sql = self.sql

Expand Down
2 changes: 2 additions & 0 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ author=Kudos Ltda. and contributors
[email protected]
changelog=0.1.3
- Use threads for download data.
- Filter tables by current extent.
- Add more info for table list.
0.1.2
- Filter tables by name.
- Indicate READ_ONLY tables for multiuser accounts.
Expand Down
1 change: 1 addition & 0 deletions toolbars/CartoDBToolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def setupUi(self):
self.nameLB = QLabel(self)
self.nameLB.setFocusPolicy(Qt.ClickFocus)
self.nameLB.setTextFormat(Qt.RichText)
self.nameLB.setText("<html><head/><body><p><span style=\" text-decoration: underline; color:#00557f;\">Add Connection</span></p></body></html>")

self.setCursor(QCursor(Qt.PointingHandCursor))
self.connectLayout.addWidget(self.avatarLB)
Expand Down
2 changes: 1 addition & 1 deletion ui/UI_CartoDBPlugin.ui
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<item>
<layout class="QHBoxLayout" name="optionsLayout">
<item>
<widget class="QCheckBox" name="extentCB">
<widget class="QCheckBox" name="extentCH">
<property name="text">
<string>Filter by current extent</string>
</property>
Expand Down
2 changes: 1 addition & 1 deletion widgets/ListItemWidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def setSize(self, size):
def setRows(self, rows):
self.rows = rows
if rows is not None:
self.ui.rowsTX.setText(str(rows))
self.ui.rowsTX.setText("{:,} rows".format(rows))

def setTextColor(self, color):
self.ui.tableNameTX.setStyleSheet('color: ' + color)
Expand Down

0 comments on commit c8cf243

Please sign in to comment.