Skip to content

Commit

Permalink
Layer stacking order
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchadwin committed Apr 15, 2015
1 parent a4645a5 commit 82005b0
Showing 1 changed file with 33 additions and 54 deletions.
87 changes: 33 additions & 54 deletions qgis2leaf_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
os.makedirs(miscStore)
#lets create a css file for own css:
with open(cssStore + 'own_style.css', 'w') as f_css:

text = """
body {
padding: 0;
margin: 0;
}"""
if full == 1:
text += """
Expand All @@ -103,24 +101,12 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
width: 100%;
padding: 0;
margin: 0;
}"""
elif full == 0:
text += """
html, body, #map {
height: """+str(height)+"""px;
width: """+str(width)+"""px;
}"""
if opacity_raster == True and full == 1:
text += """
Expand Down Expand Up @@ -159,7 +145,6 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
f_css.close()

#the index file has an easy beginning. we will store it right away:
canvas = qgis.utils.iface.mapCanvas()
outputIndex = outputProjectFileName + os.sep + 'index.html'
with open(outputIndex, 'w') as f_html:
base = """<!DOCTYPE html>
Expand Down Expand Up @@ -253,6 +238,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
in_raster = str(i.dataProvider().dataSourceUri())
prov_raster = tempfile.gettempdir() + os.sep + 'exp_' + safeLayerName + '_prov.tif'
out_raster = dataStore + os.sep + 'exp_' + safeLayerName + '.png'

crsSrc = i.crs()
crsDest = QgsCoordinateReferenceSystem(4326)
xform = QgsCoordinateTransform(crsSrc, crsDest)
Expand All @@ -261,7 +247,6 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
processing.runalg("gdalogr:warpreproject",in_raster,i.crs().authid(),"EPSG:4326","",0,1,0,-1,75,6,1,False,0,False,"",prov_raster)
print extentRepNew
processing.runalg("gdalogr:translate",prov_raster,100,True,"",0,"",extentRepNew,False,0,0,75,6,1,False,0,False,"",out_raster)

#now determine the canvas bounding box
#####now with viewcontrol
if extent == 'canvas extent':
Expand Down Expand Up @@ -302,7 +287,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
middle = """
<script>
"""
#print '>> ' + crsProj4
print '>> ' + crsProj4
if matchCRS == True and crsAuthId != 'EPSG:4326':
print '>> ' + crsProj4
middle += """
Expand Down Expand Up @@ -432,19 +417,15 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
},
onEachFeature: function (feature, layer) {""" + popFuncs + """
}"""
new_obj, cluster_num = buildPointWFS(layerName, i.source(), "", stylestr, cluster_set, cluster_num)
new_obj, cluster_num = buildPointWFS(layerName, i.source(), "", stylestr, cluster_set, cluster_num, visible)
else:
new_obj = """
var exp_""" + safeLayerName + """JSON = new L.geoJson(exp_""" + safeLayerName + """,{
onEachFeature: pop_""" + safeLayerName + "," + pointToLayer_str + """
}
});"""
#add points to the cluster group
new_obj += """
feature_group.addLayer(exp_""" + safeLayerName + """JSON);"""
if cluster_set == False:
new_obj += restackLayers(layerName)
else:
if cluster_set:
new_obj += """
var cluster_group"""+ safeLayerName + """JSON= new L.MarkerClusterGroup({showCoverageOnHover: false});"""
new_obj += """
Expand All @@ -468,7 +449,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
},
onEachFeature: function (feature, layer) {"""+popFuncs+"""
}"""
new_obj = buildNonPointWFS(layerName, i.source(), "", stylestr, popFuncs)
new_obj = buildNonPointWFS(layerName, i.source(), "", stylestr, popFuncs, visible)
else:
new_obj = """
function doStyle""" + safeLayerName + """(feature) {""" + lineStyle_str + """
Expand Down Expand Up @@ -502,7 +483,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
},
onEachFeature: function (feature, layer){"""+popFuncs+"""
}"""
new_obj = buildNonPointWFS(layerName, i.source(), "", stylestr, popFuncs)
new_obj = buildNonPointWFS(layerName, i.source(), "", stylestr, popFuncs, visible)
else:
new_obj = """
function doStyle""" + safeLayerName + """(feature) {""" + polyStyle_str + """
Expand Down Expand Up @@ -548,7 +529,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
},
onEachFeature: function (feature, layer) {"""+popFuncs+"""
}"""
new_obj, cluster_num = buildPointWFS(layerName, i.source(), categoryStr, stylestr, cluster_set, cluster_num)
new_obj, cluster_num = buildPointWFS(layerName, i.source(), categoryStr, stylestr, cluster_set, cluster_num, visible)
else:
new_obj = categoryStr + """
var exp_""" + safeLayerName + """JSON = new L.geoJson(exp_""" + safeLayerName + """,{
Expand All @@ -564,9 +545,6 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
var cluster_group"""+ safeLayerName + """JSON= new L.MarkerClusterGroup({showCoverageOnHover: false});
cluster_group"""+ safeLayerName + """JSON.addLayer(exp_""" + safeLayerName + """JSON);"""
cluster_num += 1
elif cluster_set == False:
new_obj += """
feature_group.addLayer(exp_""" + safeLayerName + """JSON);"""
elif rendererDump[0:11] == 'CATEGORIZED' and i.geometryType() == 1:
categories = renderer.categories()
valueAttr = renderer.classAttribute()
Expand Down Expand Up @@ -605,7 +583,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
onEachFeature: function (feature, layer) {"""+popFuncs+"""
}"""
if i.providerType() == 'WFS' and encode2JSON == False:
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs)
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs, visible)
else:
new_obj = buildNonPointJSON(categoryStr, safeLayerName)
elif rendererDump[0:11] == 'CATEGORIZED' and i.geometryType() == 2:
Expand Down Expand Up @@ -648,7 +626,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
style:doStyle""" + layerName + """,
onEachFeature : function (feature, layer) {"""+popFuncs+"""
}"""
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs)
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs, visible)
else:
new_obj = buildNonPointJSON(categoryStr, safeLayerName)
elif rendererDump[0:9] == 'GRADUATED' and i.geometryType() == 0 and icon_prov != True:
Expand Down Expand Up @@ -676,7 +654,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
},
onEachFeature: function (feature, layer) {"""+popFuncs+"""
}"""
new_obj, cluster_num = buildPointWFS(layerName, i.source(), categoryStr, stylestr, cluster_set, cluster_num)
new_obj, cluster_num = buildPointWFS(layerName, i.source(), categoryStr, stylestr, cluster_set, cluster_num, visible)
else:
new_obj = categoryStr + """
var exp_""" + safeLayerName + """JSON = new L.geoJson(exp_""" + safeLayerName + """,{
Expand All @@ -691,9 +669,6 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
var cluster_group"""+ safeLayerName + """JSON= new L.MarkerClusterGroup({showCoverageOnHover: false});
cluster_group"""+ safeLayerName + """JSON.addLayer(exp_""" + safeLayerName + """JSON);"""
cluster_num += 1
elif cluster_set == False:
new_obj += """
feature_group.addLayer(exp_""" + safeLayerName + """JSON);"""
elif rendererDump[0:9] == 'GRADUATED' and i.geometryType() == 1:
valueAttr = renderer.classAttribute()
categoryStr = """
Expand All @@ -716,7 +691,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
style:doStyle""" + layerName + """,
onEachFeature: function (feature, layer) {"""+popFuncs+"""
}"""
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs)
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs, visible)
else:
new_obj = buildNonPointJSON(categoryStr, safeLayerName)
elif rendererDump[0:9] == 'GRADUATED' and i.geometryType() == 2:
Expand All @@ -742,7 +717,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
style: doStyle""" + layerName + """,
onEachFeature: function (feature, layer) {"""+popFuncs+"""
}"""
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs)
new_obj = buildNonPointWFS(layerName, i.source(), categoryStr, stylestr, popFuncs, visible)
else:
new_obj = buildNonPointJSON(categoryStr, safeLayerName)
elif icon_prov == True and i.geometryType() == 0:
Expand All @@ -766,15 +741,11 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
var cluster_group"""+ safeLayerName + """JSON= new L.MarkerClusterGroup({showCoverageOnHover: false});
cluster_group"""+ safeLayerName + """JSON.addLayer(exp_""" + safeLayerName + """JSON);"""
cluster_num += 1
elif cluster_set == False:
new_obj += """
feature_group.addLayer(exp_""" + safeLayerName + """JSON);"""
else:
new_obj = """
var exp_""" + safeLayerName + """JSON = new L.geoJson(exp_""" + safeLayerName + """,{
onEachFeature: pop_""" + safeLayerName + """,
});
feature_group.addLayer(exp_""" + safeLayerName + """JSON);"""
});"""

# store everything in the file
if i.providerType() != 'WFS' or encode2JSON == True:
Expand All @@ -784,7 +755,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
if visible == 'show all' and cluster_set == False:
f5.write("""
//add comment sign to hide this layer on the map in the initial view.
exp_""" + safeLayerName + """JSON.addTo(map);""")
feature_group.addLayer(exp_""" + safeLayerName + """JSON);""")
if visible == 'show all' and cluster_set == True:
if i.geometryType() == 0:
f5.write("""
Expand All @@ -793,11 +764,11 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
if i.geometryType() != 0:
f5.write("""
//add comment sign to hide this layer on the map in the initial view.
exp_""" + safeLayerName + """JSON.addTo(map);""")
feature_group.addLayer(exp_""" + safeLayerName + """JSON);""")
if visible == 'show none' and cluster_set == False:
f5.write("""
//delete comment sign to show this layer on the map in the initial view.
//exp_""" + safeLayerName + """JSON.addTo(map);""")
//feature_group.addLayer(exp_""" + safeLayerName + """JSON);""")
if visible == 'show none' and cluster_set == True:
if i.geometryType() == 0:
f5.write("""
Expand All @@ -806,7 +777,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
if i.geometryType() != 0:
f5.write("""
//delete comment sign to show this layer on the map in the initial view.
//exp_""" + safeLayerName + """JSON.addTo(map);""")
//feature_group.addLayer(exp_""" + safeLayerName + """JSON);""")
f5.close()
elif i.type() == 1:
if i.dataProvider().name() == "wms":
Expand All @@ -827,7 +798,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
print d
#print i.source()
else:
out_raster_name = 'data/' + 'exp_' + safeLayerName + '.png'
out_raster_name = 'data/' + 'exp_' + safeLayerName + '.jpg'
pt2 = i.extent()
crsSrc = i.crs() # WGS 84
crsDest = QgsCoordinateReferenceSystem(4326) # WGS 84 / UTM zone 33N
Expand All @@ -847,7 +818,11 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
f5_raster.write(new_obj)
f5_raster.close()

with open(outputIndex, 'a') as f5fgroup:
f5fgroup.write("""
feature_group.addTo(map);""")
f5fgroup.close()

#let's add a Title and a subtitle
if webmap_head != "":
Expand Down Expand Up @@ -1043,7 +1018,7 @@ def qgis2leaf_exec(outputProjectFileName, basemapName, basemapMeta, basemapAddre
f12.close()
webbrowser.open(outputIndex)

def buildPointWFS(layerName, layerSource, categoryStr, stylestr, cluster_set, cluster_num):
def buildPointWFS(layerName, layerSource, categoryStr, stylestr, cluster_set, cluster_num, visible):
new_obj="""
var """+layerName+"""URL='"""+layerSource+"""&outputFormat=text%2Fjavascript&format_options=callback%3Aget"""+layerName+"""Json';
"""+layerName+"""URL="""+layerName+"""URL.replace(/SRSNAME\=EPSG\:\d+/, 'SRSNAME=EPSG:4326');""" + categoryStr + """
Expand All @@ -1064,7 +1039,9 @@ def buildPointWFS(layerName, layerSource, categoryStr, stylestr, cluster_set, cl
onEachFeature: function (feature, layer) {
exp_"""+layerName+"""JSON.addData(feature)
}
});
});"""
if visible == 'show all':
new_obj+="""
for (index = 0; index < layerOrder.length; index++) {
feature_group.removeLayer(layerOrder[index]);feature_group.addLayer(layerOrder[index]);
}"""
Expand All @@ -1083,11 +1060,10 @@ def buildNonPointJSON(categoryStr, safeLayerName):
var exp_""" + safeLayerName + """JSON = new L.geoJson(exp_""" + safeLayerName + """,{
onEachFeature: pop_""" + safeLayerName + """,
style: doStyle""" + safeLayerName + """
});
feature_group.addLayer(exp_""" + safeLayerName + """JSON);"""
});"""
return new_obj

def buildNonPointWFS(layerName, layerSource, categoryStr, stylestr, popFuncs):
def buildNonPointWFS(layerName, layerSource, categoryStr, stylestr, popFuncs, visible):
new_obj="""
var """+layerName+"""URL='"""+layerSource+"""&outputFormat=text%2Fjavascript&format_options=callback%3Aget"""+layerName+"""Json';
"""+layerName+"""URL="""+layerName+"""URL.replace(/SRSNAME\=EPSG\:\d+/, 'SRSNAME=EPSG:4326');""" + categoryStr + """
Expand All @@ -1104,10 +1080,13 @@ def buildNonPointWFS(layerName, layerSource, categoryStr, stylestr, popFuncs):
onEachFeature: function (feature, layer) {
exp_"""+layerName+"""JSON.addData(feature)
}
});
});"""
if visible == 'show all':
new_obj+="""
for (index = 0; index < layerOrder.length; index++) {
feature_group.removeLayer(layerOrder[index]);feature_group.addLayer(layerOrder[index]);
}
}"""
new_obj+="""
}
});"""
return new_obj
Expand Down

0 comments on commit 82005b0

Please sign in to comment.