def write_building_reports(bld_ids=[], reporttemplate=None): from mole.qgisinteraction import legend from mole.project import config from mole import oeq_global import os database = legend.nodeByName(config.data_layer_name) if not database: return None database = database[0] if not reporttemplate: reporttemplate = config.building_report_tpl tpl_path = os.path.join(oeq_global.OeQ_project_path(), config.report_template_dir, reporttemplate) rep_path = os.path.join(oeq_global.OeQ_project_path(), config.building_report_dir) th = open(tpl_path, 'r') template = th.read() th.close() building_list = database.layer().selectedFeatures() if not building_list: building_list = database.layer().getFeatures() output = [] for building in building_list: if (not bld_ids) | (building[config.building_id_key] in bld_ids): tpl = template for field in [i.name() for i in database.layer().fields()]: tpl.replace('!!!' + field + '!!!', str(building[field])) rh = open( os.path.join( rep_path, "BLD_" + building[config.building_id_key] + '.html'), 'w') rh.write(tpl) rh.close() output.append(tpl) return output
def write_temporary_vector_layer_to_disk(vlayer, style=None, replace_in_legend=True): import os from qgis.utils import iface from mole import oeq_global if oeq_global.OeQ_project_name() == '': iface.actionSaveProjectAs().trigger() layer_name = vlayer.name() layer_crs = vlayer.crs() path = os.path.join(oeq_global.OeQ_project_path(), layer_name + '.shp') error = QgsVectorFileWriter.writeAsVectorFormat(vlayer, path, "System", layer_crs, 'ESRI Shapefile') if error == QgsVectorFileWriter.NoError: if replace_in_legend: QgsMapLayerRegistry.instance().removeMapLayer(vlayer.id()) rewritten_layer = iface.addVectorLayer(path, layer_name, "ogr") #oeq_global.OeQ_wait_for_renderer(60000) if not rewritten_layer.isValid(): oeq_global.OeQ_push_warning(title='Write Error!', message='path') return vlayer if style != None: add_style_to_layer(style, rewritten_layer) rewritten_layer.startEditing() time.sleep(0.2) rewritten_layer.commitChanges() return rewritten_layer else: oeq_global.OeQ_push_warning(title='Write Error!', message='path') return vlayer
def createCentroidLayer(polygon_layer_name, centroid_layer_name, forced=False): ''' Create Polygon centroids that are reliably inside :param polygon_layer_name: Name of the polygon layer :type: string :param centroid_layer_name: Name of the new centroid layer :type: string :param forced: Force regeneration of centroids :type: boolean :return: Centroid Layer or None :rtype: QgsVectorLayer ''' import os from mole import oeq_global from mole.project import config from mole.qgisinteraction import plugin_interaction,legend,layer_interaction # check if centroid layer exists if legend.nodeExists(centroid_layer_name): #force rebuild? if forced: #remove centroid layer legend.nodeRemove(centroid_layer_name,True) else: return legend.nodeByName(centroid_layer_name)[0].layer() # init plugin communication rci = plugin_interaction.RealCentroidInteraction(config.real_centroid_plugin_name) output = os.path.join(oeq_global.OeQ_project_path(), centroid_layer_name + '.shp') centroid_layer = rci.create_centroids(polygon_layer_name, output) if centroid_layer.isValid(): layer_interaction.add_layer_to_registry(centroid_layer) polygon_layer = legend.nodeByName(polygon_layer_name) #if not failed if bool(polygon_layer): polygon_layer = polygon_layer[0].layer() rci.calculate_accuracy(polygon_layer, centroid_layer) layer_interaction.add_style_to_layer(config.valid_centroids_style, centroid_layer) legend.nodeByName(centroid_layer.name())[0].setExpanded(False) #source_section = self.progress_items_model.section_views[1] #section_model = source_section.model() #project_item = section_model.findItems("Load building coordinates")[0] #project_item.setCheckState(2) #time.sleep(0.1) #self.handle_load_raster_maps() return centroid_layer return None
def write_project_definition_report(self, template): from mole.qgisinteraction import legend from mole.project import config from mole import oeq_global database = legend.nodeByName(config.data_layer_name) if not database: return None database = database[0] tpl_path = os.path.join(oeq_global.OeQ_project_path(), config.report_template_dir, config.building_report_tpl) th = open(tpl_path, 'r') template = th.read() th.close() building_list = database.layer().selectedFeatures() if not building_list: building_list = database.layer().getFeatures() output = [] for building in building_list: tpl = template for field in [i.name() for i in database.layer().fields()]: tpl.replace('!!!' + field + '!!!', str(building[field])) output.append() return output
def write_project_definitions_report(): from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform from mole.qgisinteraction import legend from mole.project import config from mole import oeq_global import os import webbrowser from cgi import escape from urllib import pathname2url tpl_path = os.path.join(oeq_global.OeQ_plugin_path(), config.report_template_dir, config.project_definitions_tpl) rep_path = os.path.join(oeq_global.OeQ_project_path(), config.report_dir, config.project_definitions_tpl) th = open(tpl_path, 'r') template = str(th.read()) th.close() extend_node = legend.nodeByName(config.investigation_shape_layer_name) if not extend_node: return None extent = extend_node[0].layer().extent() #crsSrc=QgsCoordinateReferenceSystem(int(config.default_extent_crs.split(':')[-1]), QgsCoordinateReferenceSystem.EpsgCrsId) #crsDest=QgsCoordinateReferenceSystem(int(self.source_crs.split(':')[-1]), QgsCoordinateReferenceSystem.EpsgCrsId) ##transform extent #coord_transformer = QgsCoordinateTransform(crsSrc, crsDest) #extent = coord_transformer.transform(extent) p_info = oeq_global.OeQ_project_info project_info = { 'PRJ_NAME': p_info['project_name'], 'PRJ_DESCR': p_info['description'], 'IAC_CTY': p_info['location_city'], 'IAC_COD': p_info['location_postal'], 'IAC_LON1': p_info['location_lon'], 'IAC_LAT1': p_info['location_lat'], 'IA_LONMIN': str(extent.xMinimum()), 'IA_LONMAX': str(extent.xMaximum()), 'IA_LATMIN': str(extent.yMinimum()), 'IA_LATMAX': str(extent.xMaximum()), 'IA_CRS': p_info['location_crs'], 'PRJ_YOC': p_info['average_construction_year'], 'PRJ_PDENS': p_info['population_density'], 'PRJ_HDD': p_info['heating_degree_days'] } for field in project_info.keys(): print field print escape(project_info[field]) template = template.replace('!!!' + field + '!!!', escape(project_info[field])) if not os.path.exists( os.path.join(oeq_global.OeQ_project_path(), config.report_dir)): os.makedirs( os.path.join(oeq_global.OeQ_project_path(), config.report_dir)) print rep_path rh = open(rep_path, 'w') print rep_path template = template.encode('utf8', 'replace') rh.write('"""' + template + '"""') rh.close() #print escape(rep_path) print rep_path webbrowser.open('file://' + os.path.realpath(rep_path), new=2) return template