#!/usr/bin/env python

import grass.script as gscript


def main():

    

    gscript.run_command('g.proj', epsg='32633', flags='c')

    

    gscript.run_command('g.region', overwrite=True, res='20', w='478000', e='528300', n='4190000', s='4141900', flags='p')

    # OSM data from http://download.geofabrik.de/europe/italy/isole.html
    gscript.run_command('v.import', input='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/vulnerability/gis_osm_landuse_a_free_1.shp', output='landuse', overwrite=True)
    gscript.run_command('v.import', input='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/vulnerability/gis_osm_buildings_a_free_1.shp', output='buildings', overwrite=True)

    gscript.run_command('v.import', input='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/vulnerability/gis_osm_roads_free_1.shp', output='roads', overwrite=True)

    gscript.run_command('v.import', input='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/vulnerability/adminFinal.geojson', output='population', overwrite=True)

    

    #clip data to region

    gscript.run_command('v.in.region', overwrite=True, output='region')

    gscript.run_command('v.clip', overwrite=True, input='roads@PERMANENT', clip='region@PERMANENT', output='roads_region')

    gscript.run_command('v.clip', overwrite=True, input='buildings@PERMANENT', clip='region@PERMANENT', output='buildings_region')

    gscript.run_command('v.clip', overwrite=True, input='landuse@PERMANENT', clip='region@PERMANENT', output='landuse_region')

    gscript.run_command('v.clip', overwrite=True, input='population@PERMANENT', clip='region@PERMANENT', output='population_region')

    

    

    #roadpoints density

    #sort roads by type

    gscript.run_command('v.reclass', input='roads_region', output='roads_region_reclassify', column='fclass', overwrite=True)

    gscript.run_command('v.mkgrid', map='roadpoints_density', position='region', box='100,100', overwrite=True)

    #create one layer for each class and generate roadpoints with different distances

    gscript.run_command('v.extract', input='roads_region_reclassify', cats='3,9', output='roads_region_reclassify_1', overwrite=True)

    gscript.run_command('v.extract', input='roads_region_reclassify', cats='14', output='roads_region_reclassify_2', overwrite=True)

    gscript.run_command('v.extract', input='roads_region_reclassify', cats='10,11', output='roads_region_reclassify_3', overwrite=True)

    gscript.run_command('v.extract', input='roads_region_reclassify', cats='8', output='roads_region_reclassify_4', overwrite=True)

    gscript.run_command('v.extract', input='roads_region_reclassify', cats='4,5,21,22', output='roads_region_reclassify_5', overwrite=True)

    gscript.run_command('v.to.points', input='roads_region_reclassify_1@PERMANENT', type='line', output='roadpoints1', dmax=80, overwrite=True)

    gscript.run_command('v.to.points', input='roads_region_reclassify_2@PERMANENT', type='line', output='roadpoints2', dmax=65, overwrite=True)

    gscript.run_command('v.to.points', input='roads_region_reclassify_3@PERMANENT', type='line', output='roadpoints3', dmax=50, overwrite=True)

    gscript.run_command('v.to.points', input='roads_region_reclassify_4@PERMANENT', type='line', output='roadpoints4', dmax=24, overwrite=True)

    gscript.run_command('v.to.points', input='roads_region_reclassify_5@PERMANENT', type='line', output='roadpoints5', dmax=15, overwrite=True)

    #patch all roadpoints into one layer

    gscript.run_command('v.patch', input='roadpoints1@PERMANENT,roadpoints2@PERMANENT,roadpoints3@PERMANENT,roadpoints4@PERMANENT,roadpoints5@PERMANENT', output='roadpoints', overwrite=True)

    #count roadpoints per raster pixel and convert to raster

    gscript.run_command('v.vect.stats', points='roadpoints@PERMANENT', areas='roadpoints_density@PERMANENT', count_column='roadpoints_count')

    gscript.run_command('v.to.rast', input='roadpoints_density@PERMANENT', output='roadpoints_density_raster', use='attr', attribute_column='roadpoints_count', overwrite=True, type='area')

    #reclassify roadpoints density

    gscript.run_command('r.reclass', input='roadpoints_density_raster@PERMANENT', output='roadpoints_density_reclassified', rules='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/reclassification/reclassification_roadpoints_density.txt', overwrite=True)

    gscript.run_command('r.resample', input='roadpoints_density_reclassified@PERMANENT', output='roadpoints_density_reclassified', overwrite=True)

    

    #population density

    gscript.run_command('v.mkgrid', map='population_density', position='region', box='100,100', overwrite=True)

    #convert to raster

    gscript.run_command('v.to.rast', input='population_region@PERMANENT', output='population_density_raster', use='attr', attribute_column='Population_Density', overwrite=True, type='area')

    #reclassify population density

    gscript.run_command('r.reclass', input='population_density_raster@PERMANENT', output='population_density_reclassified', rules='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/reclassification/reclassification_population_density.txt', overwrite=True)

    gscript.run_command('r.resample', input='population_density_reclassified@PERMANENT', output='population_density_reclassified', overwrite=True)

    

    #create grid for calculation

    gscript.run_command('v.mkgrid', map='building_density', position='region', box='100,100', overwrite=True)

    #count buildings per grid section

    gscript.run_command('v.vect.stats', points='buildings@PERMANENT', type='centroid', areas='building_density@PERMANENT', count_column='building_count')

    #convert grid to raster with building density as raster value

    gscript.run_command('v.to.rast', input='building_density@PERMANENT', output='building_density_raster', use='attr', attribute_column='building_count', overwrite=True, type='area')

    #reclassify building density

    gscript.run_command('r.reclass', input='building_density_raster@PERMANENT', output='building_density_reclassified', rules='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/reclassification/reclassification_building_density.txt', overwrite=True)

    gscript.run_command('r.resample', input='building_density_reclassified@PERMANENT', output='building_density_reclassified', overwrite=True)

    

    #landuse

    gscript.run_command('v.reclass', input='landuse_region', output='landuse_region_reclassify', column='fclass', overwrite=True)

    #convert to raster

    gscript.run_command('v.to.rast', input='landuse_region_reclassify@PERMANENT', output='landuse_region_reclassify_raster', use='attr', attribute_column='cat', overwrite=True, type='area')

    #1 allotments, 2 cemetry, 3 commercial, 4 farm, 5 forest, 6 grass, 7 heath, 8 industrial, 9 meadow, 10 military, 11 nature_reserve, 12 orchard, 13 park, 14 quarry, 15 recreation_ground, 16 residential, 17 retail, 18 scrub, 19 vineyard

    #reclassify landuse

    gscript.run_command('r.reclass', input='landuse_region_reclassify_raster@PERMANENT', output='landuse_region_reclassify_raster2', rules='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/reclassification/reclassification_landuse.txt', overwrite=True)

    gscript.run_command('r.resample', input='landuse_region_reclassify_raster2@PERMANENT', output='landuse_region_reclassify_raster2', overwrite=True)

    gscript.run_command('r.null', map='landuse_region_reclassify_raster2@PERMANENT', null='1')



    #calculate vulnerability

    gscript.run_command('r.mapcalc', expression='vulnerability = building_density_reclassified@PERMANENT*0.35 + roadpoints_density_reclassified@PERMANENT*0.2 + landuse_region_reclassify_raster2@PERMANENT*0.2 + population_density_reclassified@PERMANENT*0.2', overwrite=True)

    

    #export as pictures

    gscript.run_command('r.out.gdal', input='vulnerability', output='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/results/vulnerability.tif', overwrite=True )

    gscript.run_command('r.out.gdal', input='building_density_reclassified', output='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/results/building_density_reclassified.tif', overwrite=True)

    gscript.run_command('r.out.gdal', input='population_density_reclassified', output='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/results/population_density_reclassified.tif', overwrite=True)

    gscript.run_command('r.out.gdal', input='roadpoints_density_reclassified', output='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/results/roadpoints_density_reclassified.tif', overwrite=True)

    gscript.run_command('r.out.gdal', input='landuse_region_reclassify_raster2', output='D:/Dokumente_D/Uni/5_Semester/GIS_Proseminar/project/fossgis_project/etna/results/landuse_region_reclassify_raster2.tif', overwrite=True)


if __name__ == '__main__':
    main()
