def mkDiv(b, z, qptDivision, project_name, root_out, zl_layer, zl_field, di_layer, di_field, di_name): root_code = os.path.dirname(os.path.realpath(__file__)) Lz = zl_layer #QgsProject.instance().mapLayer(mapL['zones']) debug(b.attributes()) #angle = b.attribute('ang') angle = b.attribute(2) #esto visulamente es correcto ##pero me fastidia la lectira posterior porque no dejo rastro de esta manipulacion ##para el escaneo posterio deberia de modificar el orden de los bounds en el qr y en el pie de pagina if angle > 45: angle = angle - 90 elif angle < -45: angle = angle + 90 # sql = '"name" = \''+b.attribute('name')+'\'' # sql += ' AND "x" = '+str(b.attribute('x')) # sql += ' AND "y" = '+str(b.attribute('y')) #filter('divisions', sql) sql = '"name" = \'' + b.attribute(1) + '\'' sql += ' AND "x" = ' + str(b.attribute(3)) sql += ' AND "y" = ' + str(b.attribute(4)) QgsProject.instance().mapLayer(DVL).setSubsetString(sql) sql2 = '"name" = \'' + b.attribute(1) + '\'' #sql2 = '"name" = \'' + b.attribute('name') + '\'' #filter('divisions2', sql2) QgsProject.instance().mapLayer(DVL2).setSubsetString(sql) template_file = open(qptDivision, 'r') template_content = template_file.read() template_file.close() document = QDomDocument() document.setContent(template_content) p = QgsProject().instance() composition = QgsLayout(p) composition.loadFromTemplate(document, QgsReadWriteContext()) ##mapa principal map_item = composition.itemById('mapa') map_item.setKeepLayerStyles(True) map_item.setKeepLayerSet(True) map_item.setFollowVisibilityPreset(True) map_item.setFollowVisibilityPresetName('_szona_big') map_item.setMapRotation(-angle) ######## cc = b.geometry().centroid().asPoint() coordsDesc = str(b.geometry().asWkt())[10:][:-2].split(', ') b.geometry().rotate(-angle, cc) rBound = b.geometry().boundingBox() map_item.zoomToExtent(rBound) ##mapa 2 map_item2 = composition.itemById('minimapa') map_item2.setKeepLayerStyles(True) map_item2.setKeepLayerSet(True) map_item2.setFollowVisibilityPreset(True) map_item2.setFollowVisibilityPresetName('_szona_small') #cuidado que aqu´i se lia con valor integer o string zz = zl_layer.getFeatures( QgsFeatureRequest(QgsExpression('"name" = \'' + b.attribute(1) + '\''))) zz = [x for x in zz] z = zz[0].geometry() map_item2.zoomToExtent(z.boundingBox()) ##barrio y distrito distrito = composition.itemById('distrito') distrito.setText('districte : ' + unicode(zz[0].attribute('d_name'))) barrio = composition.itemById('barrio') fields = [e.name() for e in zz[0].fields().toList()] if 'b_name' in fields: barrio.setText('barri : ' + zz[0].attribute('b_name')) else: barrio.setText('zona : ' + zz[0].attribute('name').replace('_', '.')) ##anotacion coordenadas coordsDesc = [c.split(' ') for c in coordsDesc] coords = composition.itemById('coordenadas') coords.setText(' aX: '+str(round(float(coordsDesc[0][0]),0))+ \ ' ay: '+str(round(float(coordsDesc[0][1]),0))+ \ ' bX: '+str(round(float(coordsDesc[1][0]),0))+ \ ' by: '+str(round(float(coordsDesc[1][1]),0))+ \ ' cX: '+str(round(float(coordsDesc[2][0]),0))+ \ ' cy: '+str(round(float(coordsDesc[2][1]),0))+ \ ' dX: '+str(round(float(coordsDesc[3][0]),0))+ \ ' dy: '+str(round(float(coordsDesc[3][1]),0)) ) ##zona zo = composition.itemById('zona') zo.setText(str(b.attribute(1).replace('_', '.'))) ##title ti = composition.itemById('project') ti.setText(project_name) ##cbarres #cb_name = 'cb_'+str(b.attribute('name').replace('_', '.'))+'_'+str(b.attribute('x'))+'_'+str(b.attribute('y')) cb_text = '*' + str(b.attribute(1).replace('_', '.')) + ',' + str( b.attribute(3)) + ',' + str(b.attribute(4)) + '*' #makeBarcode(cb_text, cb_name) cbarres = composition.itemById('cbarres') cbarres.setText(cb_text) #qrcode name = str(b.attribute(1).replace('_', '.')) + '_' + str( b.attribute(3)) + '_' + str(b.attribute(4)) qr = composition.itemById('qrcode') data = name+' '+ \ str(round(float(coordsDesc[0][0]),3))+' '+ \ str(round(float(coordsDesc[0][1]),3))+', '+ \ str(round(float(coordsDesc[1][0]),3))+' '+ \ str(round(float(coordsDesc[1][1]),3))+', '+ \ str(round(float(coordsDesc[2][0]),3))+' '+ \ str(round(float(coordsDesc[2][1]),3))+', '+ \ str(round(float(coordsDesc[3][0]),3))+' '+ \ str(round(float(coordsDesc[3][1]),3)) qrpath = makeQR(data, name) qr.setPicturePath(qrpath) #pagina pagina = composition.itemById('pagina') pagina.setText(name) #norte corners = composition.itemById('corners') corners.setPicturePath(root_code + '/templates/corners_a4.svg') logo = composition.itemById('logo') logo.setPicturePath(root_code + '/templates/logo.svg') norte = composition.itemById('norte') norte.setPicturePath(root_code + '/templates/norte.svg') norte.setRotation(angle) composition.refresh() currentDistrict = unicode(zz[0].attribute(zl_field)) image_name = str(b.attribute(1).replace('_', '-')) + '_' + str( b.id()) + '_' + currentDistrict printRaster(composition, image_name, root_out) return
def mkForm(b, qptForm, form_file, project_name, root_out, zl_layer, zl_field, di_layer, di_field, di_name): root_code = os.path.dirname(os.path.realpath(__file__)) name = str(b.attribute(1).replace('_', '.')) + '_' + str( b.attribute(3)) + '_' + str(b.attribute(4)) template_file = open(qptForm, 'r') template_content = template_file.read() template_file.close() document = QDomDocument() document.setContent(template_content) composition = QgsLayout(QgsProject().instance()) composition.loadFromTemplate(document, QgsReadWriteContext()) corners = composition.itemById('corners') corners.setPicturePath(root_code + '/templates/corners_a4.svg') logo = composition.itemById('logo') logo.setPicturePath(root_code + '/templates/logo.svg') ##title ti = composition.itemById('project') ti.setText(project_name) ##zona zo = composition.itemById('zona') zo.setText(str(b.attribute(1).replace('_', '.'))) ##pagina zo = composition.itemById('pagina') zo.setText(name) #form qr = composition.itemById('form') qr.setPicturePath(form_file) #qrcode qr = composition.itemById('qrcode') data = name qrpath = makeQR(data, name) qr.setPicturePath(qrpath) ##cbarres cbarres = composition.itemById('cbarres') cbarres.setText('*' + str(b.attribute(1).replace('_', '.')) + ',' + str(b.attribute(3)) + ',' + str(b.attribute(4)) + '*') composition.refresh() zz = zl_layer.getFeatures( QgsFeatureRequest(QgsExpression('"name" = \'' + b.attribute(1) + '\''))) zz = [x for x in zz] z = zz[0].geometry() currentDistrict = unicode(zz[0].attribute(zl_field)) image_name = str(b.attribute(1).replace('_', '-')) + '_' + str( b.id()) + '_form' + '_' + currentDistrict printRaster(composition, image_name, root_out) return
def mkChapter(b, qptChapter, project_name, root_out, zl_layer, zl_field, di_layer, di_field, di_name): """ b es un bound """ root_code = os.path.dirname(os.path.realpath(__file__)) Lz = zl_layer #QgsProject.instance().mapLayer(mapL['zones']) file_qptChapter = open(qptChapter, 'r') template_content_chapter = file_qptChapter.read() file_qptChapter.close() ### #filter('zones', '"name" = \''+b.attribute('name')+'\'' )#cuidado si el valor es integer o string #filter('zones2', '"name" = \''+b.attribute('name')+'\'' )#cuidado si el valor es integer o string #filter('divisions', '"name" = \''+b.attribute('name')+'\'' ) #filter('divisions2', '"name" = \'' + b.attribute('name') + '\'') # QgsProject.instance().mapLayer(ZVL).setSubsetString( '"name" = \''+b.attribute('name')+'\'' ) # QgsProject.instance().mapLayer(DVL).setSubsetString('"name" = \''+b.attribute('name')+'\'' ) # QgsProject.instance().mapLayer(DVL2).setSubsetString('"name" = \''+b.attribute('name')+'\'' ) # QgsProject.instance().mapLayer(ZVL).setSubsetString( '"%s" = \'%s\'' %(zl_field, b.attribute(zl_field)) ) # QgsProject.instance().mapLayer(DVL).setSubsetString('"%s" = \'%s\'' %(di_field, b.attribute(zl_field)) ) # QgsProject.instance().mapLayer(DVL2).setSubsetString('"%s" = \'%s\'' %(di_field, b.attribute(zl_field)) ) ### seleccionamos la zona #zz = Lz.getFeatures(QgsFeatureRequest(QgsExpression('"name" = \''+b.attribute('name')+'\''))) #cuidado si el valor es integer o string print('zz', '"%s" = \'%s\'' % (zl_field, b.attribute(zl_field))) zz = Lz.getFeatures( QgsFeatureRequest( QgsExpression('"%s" = \'%s\'' % (zl_field, b.attribute(zl_field)))) ) #cuidado si el valor es integer o string zz = [x for x in zz] # escogemos el distrito correspondiente a la zona #filter('districtes', '"name" = \''+unicode(zz[0].attribute('d_name'))+'\'' ) di_layer.setSubsetString('"' + di_field + '" = \'' + unicode(zz[0].attribute(zl_field)) + '\'') global currentDistrict #currentDistrict = unicode(zz[0].attribute('d_id')) currentDistrict = unicode(zz[0].attribute(zl_field)) #coje la geometria del distriro #distri = QgsProject.instance().mapLayer(mapL['districtes']) gd = [d for d in di_layer.getFeatures()][0] #canvas = QgsMapCanvas() document = QDomDocument() document.setContent(template_content_chapter) p = QgsProject().instance() composition = QgsLayout(p) composition.loadFromTemplate(document, QgsReadWriteContext()) ## bounds = (b.geometry()).boundingBox() map_item = composition.itemById('mapa') map_item.zoomToExtent(bounds) map_item.setKeepLayerStyles(True) map_item.setKeepLayerSet(True) map_item.setFollowVisibilityPreset(True) map_item.setFollowVisibilityPresetName('_zona_big') map_item_distri = composition.itemById('mapDistrito') map_item_distri.zoomToExtent(gd.geometry().boundingBox()) map_item_distri.setKeepLayerStyles(True) map_item_distri.setKeepLayerSet(True) map_item_distri.setFollowVisibilityPreset(True) map_item_distri.setFollowVisibilityPresetName('_zona_small') ##title ti = composition.itemById('project') ti.setText(project_name) ##zona zo = composition.itemById('zona') zo.setText(str(b.attribute('id'))) corners = composition.itemById('corners') corners.setPicturePath(root_code + '/templates/corners_a4.svg') logo = composition.itemById('logo') logo.setPicturePath(root_code + '/templates/logo.svg') #norte = composition.itemById('norte') #norte.setPicturePath(root_code+'templates/norte.svg') #norte.setRotation(angle) ##barrio y distrito distrito = composition.itemById('distrito') distrito.setText('districte : ' + unicode(zz[0].attribute('d_name'))) barrio = composition.itemById('barrio') fields = [e.name() for e in zz[0].fields().toList()] if 'b_name' in fields: barrio.setText('barri : ' + zz[0].attribute('b_name')) #legacy else: barrio.setText('zona : ' + zz[0].attribute('name').replace('_', '.')) composition.refresh() image_name = str(b.attribute('name').replace('_', '-')) + '_' + currentDistrict printRaster(composition, image_name, root_out) # filter('divisions', '' ) # filter('divisions2', '' ) # filter('districtes', '' ) QgsProject.instance().mapLayer(DVL).setSubsetString('') QgsProject.instance().mapLayer(DVL2).setSubsetString('') return