def make_kml(in_file, out_file, location_column, columns, column_types): """Create and save a KML file from the given CSV data and desired columns.""" data = ureport_csv.load(in_file, location_col=location_column, target_cols=columns) layers = [] for column, type in zip(columns, column_types): question = data[0].questions[column] print question column_data = [] for d in data: point = geo.Point(d.point.lat, d.point.lon) point.value = d.answers[column] if type == 'yesno': point = boolean_data.yes_no_to_boolean(point) elif type == 'numeric': point.value = float(point.value) column_data.append(point) if type == 'yesno': layers.extend(yesno_layers(question, column_data)) elif type == 'numeric': layers.append(numeric_layer(question, column_data)) else: raise RuntimeError('Unknown column type [%s]' % type) files = [layer.file for layer in layers if hasattr(layer, 'file')] kml.write_kmz(out_file, kml.kml('UReport', layers), files)
def write(self, filename, version): date_time = datetime.datetime.now().timetuple()[:6] zf = zipfile.ZipFile(filename, 'w') document = kml.Document() document.add(*self.roots) document.add(*self.elements) string_io = StringIO() kml.kml(version, document).pretty_write(string_io) zi = zipfile.ZipInfo('doc.kml') zi.compress_type = zipfile.ZIP_DEFLATED zi.date_time = date_time zi.external_attr = 0644 << 16 zf.writestr(zi, string_io.getvalue()) string_io.close() for key, value in self.files.items(): zi = zipfile.ZipInfo(key) zi.compress_type = zipfile.ZIP_DEFLATED zi.date_time = date_time zi.external_attr = 0644 << 16 zf.writestr(zi, value) zf.close()
def process(self): self.setStyles() self.processLayer() Kml = kml(self.layer.name()) types = ["string" for x in self.exports] Kml.addSchema(self.layer.name(), self.exports, types) for item in self.styles: styId, kwargs = item Kml.addStyle(styId, **kwargs) msg = QMessageBox() if not self.styles: #self.error.emit("La simbologia deve essere categorizzata e almeno UNA categoria deve essere accesa.") #self.finished.emit(False) msg.setText("La simbologia deve essere categorizzata e almeno UNA categoria deve essere accesa. Il layer %s non verra' esportato" % (self.layer.name())) msg.setIcon(QMessageBox.Critical) msg.setWindowTitle("Layer non esportabile!") msg.setStandardButtons(QMessageBox.Ok) retval = msg.exec_() Utils.logMessage("La simbologia deve essere categorizzata e almeno UNA categoria deve essere accesa.") return 0 style = self.styles[0][0] for i in range(len(self.data)): self.updateProgress() folder = self.featFolder[i] name = self.labels[i] coords = self.coords[i] if self.styleField is not None: style = self.featStyles[i] fields = {} fields[self.layer.name()] = zip(self.exports, self.data[i]) Kml.addPlacemark(folder, name, style, coords, fields) self.counter += 1 tmpKml = os.path.join(self.tmpDir, "doc.kml") fstream = open(tmpKml, "w") fstream.writelines(Kml.generatekml()) fstream.close() z = zipfile.ZipFile(self.outFile, "w") z.write(tmpKml, arcname="doc.kml") for styDict in [x[1] for x in self.styles]: if "iconfile" in styDict.keys(): filename = os.path.join(self.tmpDir, styDict["iconfile"]) z.write(filename, arcname=os.path.basename(filename)) z.close() self.cleanup() self.updateProgress()
def testKml(self): house = kml.Placemark('house', '', kml.Point(5, 9)) work = kml.Placemark('work', '', kml.Point(1, 2)) kml_data = kml.kml('title', [house, work]) self.assertEqual("""<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Folder> <name>title</name> <visibility>1</visibility> %s %s </Folder> </kml>""" % (str(house), str(work)), kml_data)
def process(self): self.setStyles() self.processLayer() Kml = kml(self.layer.name()) types = ["string" for x in self.exports] Kml.addSchema(self.layer.name(), self.exports, types) for item in self.styles: styId, kwargs = item Kml.addStyle(styId, **kwargs) style = self.styles[0][0] for i in range(len(self.data)): self.updateProgress() folder = self.featFolder[i] name = self.labels[i] coords = self.coords[i] if self.styleField is not None: style = self.featStyles[i] fields = {} fields[self.layer.name()] = zip(self.exports, self.data[i]) Kml.addPlacemark(folder, name, style, coords, fields) self.counter += 1 tmpKml = os.path.join(self.tmpDir, "doc.kml") fstream = open(tmpKml, "w") fstream.writelines(Kml.generatekml()) fstream.close() z = zipfile.ZipFile(self.outFile, "w") z.write(tmpKml, arcname="doc.kml") for styDict in [x[1] for x in self.styles]: if "iconfile" in styDict.keys(): filename = os.path.join(self.tmpDir, styDict["iconfile"]) z.write(filename, arcname=os.path.basename(filename)) z.close() self.cleanup() self.updateProgress()
def do_kml(self): from kml import kml return kml()