#--------------------------------------------------------------------- # # QGIS Rest - A QGIS plugin to allow for interacting with REST servers # # Copyright (C) 2008 Aaron Racicot # # EMAIL: aaronr (at) z-pulley.com # WEB : www.reprojected.com # # Based on the original "Rest Client" Copyright (c) 2007 Christopher Schmidt # http://www.restclient.org/ # # Vector format code based on "FeatureServer" Copyright (c) 2007 MetaCarta # http://www.featureserver.org/ # #--------------------------------------------------------------------- # # licensed under the terms of GNU GPL 2 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # #--------------------------------------------------------------------- from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgisrest_gui import Ui_Dialog import resources_rc import httplib, urlparse import tempfile import os from vectorformats.Formats.OGR import OGR from vectorformats.Formats.KML import KML #from vectorformats.Formats.HTML import HTML from vectorformats.Formats.GeoRSS import GeoRSS from vectorformats.Formats.GeoJSON import GeoJSON #from vectorformats.Formats.WFS import WFS DEFAULT_URL = "http://featureserver.org/demo/featureserver.cgi/scribble?maxfeatures=1" class QGISRestGui(QDialog, Ui_Dialog): def __init__(self, parent, fl): QDialog.__init__(self, parent, fl) self.setupUi(self) self.send_data = {'data': '', 'headers':'User-Agent: QGISRest-1'} #Set the initial data and headers for the client self.urlLine.clear() self.urlLine.insert(DEFAULT_URL) self.localContentText.clear() self.localContentText.append(self.send_data['data']) self.localHeadersText.clear() self.localHeadersText.append(self.send_data['headers']) QObject.connect(self.sendURL, SIGNAL("clicked()"), self.sendURL_clicked) def sendURL_clicked(self): content_types = { 'application/vnd.google-earth.kml+xml': 'KML', 'application/json': 'JSON', 'text/javascript': 'JSON', 'application/rss+xml': 'GeoRSS', 'text/html': 'HTML', 'osm': 'OSM', 'gml': 'WFS', 'wfs': 'WFS', 'kml': 'KML', 'json': 'JSON', 'georss': 'GeoRSS', 'atom': 'GeoRSS', 'html': 'HTML', 'geojson':'GeoJSON', 'text/plain':'GeoJSON', 'application/atom+xml':'GeoRSS', 'text/xml':'WFS' } """Loads the data from the server, using the current settings""" print "Checking the clicked" method = str(self.methodComboBox.currentText().toUpper()) url = str(self.urlLine.text()) self.send_data['data'] = str(self.localContentText.toPlainText()) self.send_data['headers'] = str(self.localHeadersText.toPlainText()) data = self.send_data['data'] headers = self.send_data['headers'] headers = headers.split("\n") headers_dict = {} for header in headers: parts = header.split(":") if len(parts) > 1: headers_dict[parts[0]] = parts[1].strip() urlparts = urlparse.urlparse(url) conn = httplib.HTTPConnection(urlparts[1]) url = "%s?%s" % (urlparts[2], urlparts[4]) if method == "POST" or method == "PUT": conn.request(method, url, data, headers_dict) else: conn.request(method, url, None, headers_dict) response = conn.getresponse() self.remoteContentBox.setTitle(QApplication.translate("Dialog", "%s %s" % (response.status, response.reason), None, QApplication.UnicodeUTF8)) self.data = response.read() self.headers = response.getheaders() self.remoteContentText.clear() self.remoteContentText.append(self.data) self.remoteHeadersText.clear() headers_string = [] contentType = '' for header in self.headers: headers_string.append("%s: %s" % (header[0], header[1])) if header[0] == "content-type": contentType = header[1] self.remoteHeadersText.append("\n".join(headers_string)) format = "" found = False if contentType.lower() in content_types: format = content_types[contentType.lower()] found = True # Create a temp file to load data into qgis #tempf,tempfpath = tempfile.mkstemp('.shp','restclient') #print tempf,tempfpath #os.close(tempf) tempd = tempfile.mkdtemp() o = OGR(driver="ESRI Shapefile", dsname=tempd, save_on_encode=True) x = "" if format == "KML": x = KML() if format == "JSON": x = GeoJSON() if format == "GeoJSON": x = GeoJSON() if format == "GeoRSS": x = GeoRSS() if format == "HTML": #x = HTML() x = "" print "HTML Not implemented yet" if format == "OSM": x = "" print "OSM Not implemented yet" if format == "WFS": #x = WFS() x = "" print "GML Not implemented yet" #os.write(tempf,o.encode(x.decode(self.data))) if x != "": o.encode(x.decode(self.data)) print "%s/%s" % (tempd,o.layername) vlayer = QgsVectorLayer("%s/%s.shp" % (tempd,o.layername), "QGISRest", "ogr") if vlayer.isValid(): print "Layer loaded." QgsMapLayerRegistry.instance().addMapLayer(vlayer) else: print "Failed to load layer!" def on_btnCancel_clicked(self): self.close()