Ejemplo n.º 1
0
def save_wells_to_db(order_obj, page):
    psr_obj = models.PSR()
    if (int(arcpy.GetCount_management(config.wells_sja).getOutput(0)) == 0):
        arcpy.AddMessage('      - No well records are selected.')
        psr_obj.insert_map(order_obj.id, 'WELLS',
                           order_obj.number + '_US_WELLS.jpg', 1)
    else:
        in_rows = arcpy.SearchCursor(config.wells_sja)
        for in_row in in_rows:
            eris_id = str(int(in_row.ID))
            ds_oid = str(int(in_row.DS_OID))
            distance = str(float(in_row.Distance))
            direction = str(in_row.Direction)
            elevation = str(float(in_row.Elevation))
            site_elevation = str(
                float(in_row.Elevation) - float(in_row.Site_Z))
            map_key_loc = str(int(in_row.MapKeyLoc))
            map_key_no = str(int(in_row.MapKeyNo))
            psr_obj.insert_order_detail(order_obj.id, eris_id, ds_oid, '',
                                        distance, direction, elevation,
                                        site_elevation, map_key_loc,
                                        map_key_no)
        #note type 'SOIL' or 'GEOL' is used internally
        psr_obj.insert_map(order_obj.id, 'WELLS',
                           order_obj.number + '_US_WELLS.jpg', 1)
        if not config.if_multi_page:
            for i in range(1, page):
                psr_obj.insert_map(
                    order_obj.id, 'WELLS',
                    order_obj.number + '_US_WELLS' + str(i) + '.jpg', i + 1)
Ejemplo n.º 2
0
def generate_aspect_map(order_obj):
    arcpy.AddMessage('  -- Start generating aspect map ...')
    start = timeit.default_timer()
    ### create buffer map based on order geometry
    arcpy.Buffer_analysis(config.order_geometry_pcs_shp,
                          config.order_aspect_buffer,
                          config.buffer_dist_aspect)
    ### select dem itersected by order buffer
    master_dem_layer = arcpy.MakeFeatureLayer_management(
        config.master_lyr_dem, 'master_dem_layer')
    arcpy.SelectLayerByLocation_management(master_dem_layer, 'intersect',
                                           config.order_aspect_buffer)
    # arcpy.CopyFeatures_management(master_dem_layer, config.dem_selectedby_order)
    dem_rasters = []
    dem_raster = None
    if (int((arcpy.GetCount_management(master_dem_layer).getOutput(0))) == 0):
        arcpy.AddMessage('  -- NO records selected for US')
    else:
        dem_rows = arcpy.SearchCursor(master_dem_layer)
        for row in dem_rows:
            dem_raster_file = row.getValue("image_name")
            if (dem_raster_file != ''):
                dem_rasters.append(
                    os.path.join(config.img_dir_dem, dem_raster_file))
        del row
        del dem_rows
    if len(dem_rasters) == 0:
        master_dem_layer = arcpy.mapping.Layer(config.master_lyr_dem)
        arcpy.SelectLayerByLocation_management(master_dem_layer, 'intersect',
                                               config.order_aspect_buffer)
        if (int(
            (arcpy.GetCount_management(master_dem_layer).getOutput(0))) != 0):
            for row in dem_rows:
                dem_raster_file = row.getValue("image_name")
                if (dem_raster_file != ''):
                    dem_rasters.append(
                        os.path.join(config.img_dir_dem, dem_raster_file))
    if len(dem_rasters) >= 1:
        dem_raster = "img.img"
        if len(dem_rasters) == 1:
            ras = dem_rasters[0]
            arcpy.Clip_management(
                ras, "#", os.path.join(config.scratch_folder, dem_raster),
                config.order_aspect_buffer, "#", "NONE", "MAINTAIN_EXTENT")
        else:
            clipped_ras = ''
            i = 1
            for ras in dem_rasters:
                clip_name = "clip_ras_" + str(i) + ".img"
                arcpy.Clip_management(
                    ras, "#", os.path.join(config.scratch_folder, clip_name),
                    config.order_aspect_buffer, "#", "NONE", "MAINTAIN_EXTENT")
                clipped_ras = clipped_ras + os.path.join(
                    config.order_aspect_buffer, clip_name) + ";"
                i += 1

            arcpy.MosaicToNewRaster_management(clipped_ras[0:-1],
                                               config.scratch_folder,
                                               dem_raster,
                                               order_obj.spatial_ref_pcs,
                                               "32_BIT_FLOAT", "#", "1",
                                               "FIRST", "#")

        numpy_array = arcpy.RasterToNumPyArray(
            os.path.join(config.scratch_folder, dem_raster))
        x, y = gradient(numpy_array)
        slope = 57.29578 * arctan(sqrt(x * x + y * y))
        aspect = 57.29578 * arctan2(-x, y)

        for i in range(len(aspect)):
            for j in range(len(aspect[i])):
                if -180 <= aspect[i][j] <= -90:
                    aspect[i][j] = -90 - aspect[i][j]
                else:
                    aspect[i][j] = 270 - aspect[i][j]
                if slope[i][j] == 0:
                    aspect[i][j] = -1
        # gather some information on the original file
        cell_size_h = arcpy.Describe(
            os.path.join(config.scratch_folder, dem_raster)).meanCellHeight
        cell_size_w = arcpy.Describe(
            os.path.join(config.scratch_folder, dem_raster)).meanCellWidth
        extent = arcpy.Describe(os.path.join(config.scratch_folder,
                                             dem_raster)).Extent
        pnt = arcpy.Point(extent.XMin, extent.YMin)

        # save the raster
        aspect_tif = os.path.join(config.scratch_folder, "aspect.tif")
        aspect_ras = arcpy.NumPyArrayToRaster(aspect, pnt, cell_size_h,
                                              cell_size_w)
        arcpy.CopyRaster_management(aspect_ras, aspect_tif)
        arcpy.DefineProjection_management(aspect_tif,
                                          order_obj.spatial_ref_gcs)
        # slope map
        slope_tif = os.path.join(config.scratch_folder, "slope.tif")
        slope_ras = arcpy.NumPyArrayToRaster(slope, pnt, cell_size_h,
                                             cell_size_w)
        arcpy.CopyRaster_management(slope_ras, slope_tif)
        arcpy.DefineProjection_management(slope_tif, order_obj.spatial_ref_gcs)

        aspect_tif_pcs = os.path.join(config.scratch_folder, "aspect_pcs.tif")
        arcpy.ProjectRaster_management(aspect_tif, aspect_tif_pcs,
                                       order_obj.spatial_ref_pcs)

        #Calculate aspect
        aspect_text, utm_x, utm_y = calculate_aspect(order_obj, aspect_tif_pcs)
        site_elevation = utility.get_elevation(
            order_obj.geometry.trueCentroid.X,
            order_obj.geometry.trueCentroid.Y)

        #Update DB
        psr_obj = models.PSR()
        psr_obj.update_order(order_obj.id, str(utm_x), str(utm_y),
                             order_obj.spatial_ref_pcs.name, site_elevation,
                             aspect_text)

    end = timeit.default_timer()
    arcpy.AddMessage(
        ('-- End generating PSR aspect map. Duration:', round(end - start, 4)))
Ejemplo n.º 3
0
def generate_multipage_report(order_obj, cellids_selected, mxd_relief,
                              df_relief, output_jpg_relief):
    df_mm_relief = None
    grid_lyr_shp = os.path.join(config.scratch_folder, 'grid_lyr_relief.shp')
    arcpy.GridIndexFeatures_cartography(grid_lyr_shp, config.order_buffer_shp,
                                        "", "", "", config.grid_size,
                                        config.grid_size)
    # part 1: the overview map
    # add grid layer
    grid_layer = arcpy.mapping.Layer(config.grid_lyr_file)
    grid_layer.replaceDataSource(config.scratch_folder, "SHAPEFILE_WORKSPACE",
                                 "grid_lyr_relief")
    arcpy.mapping.AddLayer(df_relief, grid_layer, "Top")

    df_relief.extent = grid_layer.getExtent()
    df_relief.scale = df_relief.scale * 1.1

    mxd_relief.saveACopy(os.path.join(config.scratch_folder, "mxd_relief.mxd"))
    arcpy.mapping.ExportToJPEG(mxd_relief, output_jpg_relief, "PAGE_LAYOUT",
                               480, 640, 150, "False", "24-BIT_TRUE_COLOR", 85)

    if not os.path.exists(
            os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
        os.mkdir(os.path.join(config.report_path, 'PSRmaps', order_obj.number))
    shutil.copy(output_jpg_relief,
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
    arcpy.AddMessage(
        '      - output jpg image page 1: %s' %
        os.path.join(config.report_path, 'PSRmaps', order_obj.number,
                     os.path.basename(output_jpg_relief)))
    del mxd_relief
    del df_relief

    # part 2: the data driven pages
    page = int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) + 1
    mxd_mm_relief = arcpy.mapping.MapDocument(config.mxd_mm_file_relief)

    df_mm_relief = arcpy.mapping.ListDataFrames(mxd_mm_relief, "*")[0]
    df_mm_relief.spatialReference = order_obj.spatial_ref_pcs

    ### add order geometry and it's bugger to mxd
    utility.add_layer_to_mxd("order_buffer", df_mm_relief,
                             config.buffer_lyr_file, 1.1)
    utility.add_layer_to_mxd("order_geometry_pcs", df_mm_relief,
                             config.order_geom_lyr_file, 1)

    grid_layer_mm = arcpy.mapping.ListLayers(mxd_mm_relief, "Grid",
                                             df_mm_relief)[0]
    grid_layer_mm.replaceDataSource(config.scratch_folder,
                                    "SHAPEFILE_WORKSPACE", "grid_lyr_relief")
    arcpy.CalculateAdjacentFields_cartography(grid_lyr_shp, "PageNumber")
    mxd_mm_relief.saveACopy(
        os.path.join(config.scratch_folder, "mxd_mm_relief.mxd"))

    for item in cellids_selected:
        item = item[:-4]
        relief_layer = arcpy.mapping.Layer(config.relief_lyr_file)
        shutil.copyfile(
            os.path.join(config.path_shaded_relief, item + '_hs.img'),
            os.path.join(
                config.scratch_folder,
                item + '_hs.img'))  #make a local copy, will make it run faster
        relief_layer.replaceDataSource(config.scratch_folder,
                                       "RASTER_WORKSPACE", item + '_hs.img')
        relief_layer.name = item
        arcpy.mapping.AddLayer(df_mm_relief, relief_layer, "BOTTOM")

        for i in range(
                1,
                int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) + 1):
            arcpy.SelectLayerByAttribute_management(
                grid_layer_mm, "NEW_SELECTION", ' "PageNumber" =  ' + str(i))
            df_mm_relief.extent = grid_layer_mm.getSelectedExtent(True)
            df_mm_relief.scale = df_mm_relief.scale * 1.1
            arcpy.SelectLayerByAttribute_management(grid_layer_mm,
                                                    "CLEAR_SELECTION")
            arcpy.mapping.ExportToJPEG(
                mxd_mm_relief, output_jpg_relief[0:-4] + str(i) + ".jpg",
                "PAGE_LAYOUT", 480, 640, 150, "False", "24-BIT_TRUE_COLOR", 85)

            if not os.path.exists(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number)):
                os.mkdir(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number))
            shutil.copy(
                output_jpg_relief[0:-4] + str(i) + ".jpg",
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))

    del mxd_mm_relief
    del df_mm_relief
    #insert generated .jpg report path into eris_maps_psr table
    psr_obj = models.PSR()
    if os.path.exists(
            os.path.join(config.report_path, 'PSRmaps', order_obj.number,
                         order_obj.number + '_US_RELIEF.jpg')):
        psr_obj.insert_map(order_obj.id, 'RELIEF',
                           order_obj.number + '_US_RELIEF.jpg', 1)
        for i in range(1, page):
            psr_obj.insert_map(
                order_obj.id, 'RELIEF',
                order_obj.number + '_US_RELIEF' + str(i) + '.jpg', i + 1)
    else:
        arcpy.AddMessage('No Relief map is available')
Ejemplo n.º 4
0
def generate_flood_report(order_obj):
    arcpy.AddMessage('  -- Start generating PSR flood report...')
    start = timeit.default_timer()   
  
    eris_id = 0
    Setting.output_jpg_flood = config.output_jpg(order_obj,config.Report_Type.flood)
    page = 1
    if '10683' not in order_obj.psr.search_radius.keys():
        arcpy.AddMessage('      -- Flood search radius is not availabe')
        return
    config.buffer_dist_flood = str(order_obj.psr.search_radius['10683']) + ' MILES'
    arcpy.Buffer_analysis(config.order_geometry_pcs_shp, config.order_buffer_shp, config.buffer_dist_flood) ### create buffer map based on order geometry
    
    arcpy.MakeFeatureLayer_management(config.data_flood, 'flood_lyr') 
    arcpy.SelectLayerByLocation_management('flood_lyr', 'intersect',  config.order_buffer_shp)
    arcpy.CopyFeatures_management('flood_lyr', config.flood_selectedby_order_shp)
    
    arcpy.MakeFeatureLayer_management(config.data_flood_panel, 'flood_panel_lyr') 
    arcpy.SelectLayerByLocation_management('flood_panel_lyr', 'intersect',  config.order_buffer_shp)
    arcpy.CopyFeatures_management('flood_panel_lyr', config.flood_panel_selectedby_order_shp)
    
    arcpy.Statistics_analysis(config.flood_selectedby_order_shp, os.path.join(config.scratch_folder,"summary_flood.dbf"), [['FLD_ZONE','FIRST'], ['ZONE_SUBTY','FIRST']],'ERIS_CLASS')
    arcpy.Sort_management(os.path.join(config.scratch_folder,"summary_flood.dbf"), os.path.join(config.scratch_folder,"summary_sorted_flood.dbf"), [["ERIS_CLASS", "ASCENDING"]])
    
    mxd_flood = arcpy.mapping.MapDocument(config.mxd_file_flood)
    df_flood = arcpy.mapping.ListDataFrames(mxd_flood,"Flood*")[0]
    df_flood.spatialReference = order_obj.spatial_ref_pcs
    
    df_flood_small = arcpy.mapping.ListDataFrames(mxd_flood,"Study*")[0]
    df_flood_small.spatialReference = order_obj.spatial_ref_pcs
    del df_flood_small
    
    utility.add_layer_to_mxd("order_buffer",df_flood,config.buffer_lyr_file, 1.1)
    utility.add_layer_to_mxd("order_geometry_pcs", df_flood,config.order_geom_lyr_file,1)
    arcpy.RefreshActiveView()
    
    psr_obj = models.PSR()
    
    if not config.if_multi_page: # single-page
        mxd_flood.saveACopy(os.path.join(config.scratch_folder, "mxd_flood.mxd"))  
        arcpy.mapping.ExportToJPEG(mxd_flood, Setting.output_jpg_flood, "PAGE_LAYOUT", resolution=75, jpeg_quality=40)
        if not os.path.exists(os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
            os.mkdir(os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        arcpy.AddMessage('      - output jpg image path (overview map): %s' % os.path.join(config.report_path, 'PSRmaps', order_obj.number,os.path.basename(Setting.output_jpg_flood)))
        shutil.copy(Setting.output_jpg_flood, os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        del mxd_flood
        del df_flood
    else: # multi-page
        grid_lyr_shp = os.path.join(config.scratch_folder, 'grid_lyr_flood.shp')
        arcpy.GridIndexFeatures_cartography(grid_lyr_shp, config.order_buffer_shp, "", "", "", config.grid_size, config.grid_size)
        
        # part 1: the overview map
        # add grid layer
        grid_layer = arcpy.mapping.Layer(config.grid_lyr_file)
        grid_layer.replaceDataSource(config.scratch_folder,"SHAPEFILE_WORKSPACE","grid_lyr_flood")
        arcpy.mapping.AddLayer(df_flood,grid_layer,"Top")

        df_flood.extent = grid_layer.getExtent()
        df_flood.scale = df_flood.scale * 1.1

        mxd_flood.saveACopy(os.path.join(config.scratch_folder, "mxd_flood.mxd"))
        arcpy.mapping.ExportToJPEG(mxd_flood, Setting.output_jpg_flood, "PAGE_LAYOUT", 480, 640, 75, "False", "24-BIT_TRUE_COLOR", 40)
        if not os.path.exists(os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
            os.mkdir(os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        shutil.copy(Setting.output_jpg_flood, os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        arcpy.AddMessage('      - output jpg image page 1: %s' % os.path.join(config.report_path, 'PSRmaps', order_obj.number,os.path.basename(Setting.output_jpg_flood)))
        del mxd_flood
        del df_flood
        # part 2: the data driven pages
        
        page = int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0))  + page
        Setting.mxd_multi_flood = arcpy.mapping.MapDocument(config.mxd_mm_file_flood)

        Setting.df_mm_flood = arcpy.mapping.ListDataFrames(Setting.mxd_multi_flood,"Flood*")[0]
        Setting.df_mm_flood.spatialReference = order_obj.spatial_ref_pcs
        utility.add_layer_to_mxd("order_buffer",Setting.df_mm_flood,config.buffer_lyr_file,1.1)
        utility.add_layer_to_mxd("order_geometry_pcs", Setting.df_mm_flood,config.order_geom_lyr_file,1)
        
        Setting.grid_layer_mm = arcpy.mapping.ListLayers(Setting.mxd_multi_flood,"Grid" ,Setting.df_mm_flood)[0]
        Setting.grid_layer_mm.replaceDataSource(config.scratch_folder,"SHAPEFILE_WORKSPACE","grid_lyr_flood")
        arcpy.CalculateAdjacentFields_cartography(grid_lyr_shp, "PageNumber")
        Setting.mxd_multi_flood.saveACopy(os.path.join(config.scratch_folder, "mxd_mm_flood.mxd"))
       
        if not os.path.exists(os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
            os.mkdir(os.path.join(config.report_path, 'PSRmaps', order_obj.number))

        for i in range(1,page):
            arcpy.SelectLayerByAttribute_management(Setting.grid_layer_mm, "NEW_SELECTION", ' "PageNumber" =  ' + str(i))
            Setting.df_mm_flood.extent = Setting.grid_layer_mm.getSelectedExtent(True)
            Setting.df_mm_flood.scale = Setting.df_mm_flood.scale * 1.1
            arcpy.SelectLayerByAttribute_management(Setting.grid_layer_mm, "CLEAR_SELECTION")

            title_text = arcpy.mapping.ListLayoutElements(Setting.mxd_multi_flood, "TEXT_ELEMENT", "title")[0]
            title_text.text = '      - Flood Hazard Zones - Page ' + str(i)
            title_text.elementPositionX = 0.5946
            arcpy.RefreshTOC()

            arcpy.mapping.ExportToJPEG(Setting.mxd_multi_flood, Setting.output_jpg_flood[0:-4]+str(i)+".jpg", "PAGE_LAYOUT", 480, 640, 75, "False", "24-BIT_TRUE_COLOR", 40)
          
            shutil.copy(Setting.output_jpg_flood[0:-4]+str(i)+".jpg", os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        
        del Setting.mxd_multi_flood
        del Setting.df_mm_flood
        
        ### update tables in DB
        for i in range(1,page):
            psr_obj.insert_map(order_obj.id, 'FLOOD', order_obj.number + '_US_FLOOD' + str(i) + '.jpg', i + 1)
        
    flood_panels = ''
    if (int(arcpy.GetCount_management(os.path.join(config.scratch_folder,"summary_flood.dbf")).getOutput(0))== 0):
        # no floodplain records selected....
        arcpy.AddMessage('      - No floodplain records are selected....')
        if (int(arcpy.GetCount_management(config.flood_panel_selectedby_order_shp).getOutput(0))== 0):
            # no panel available, means no data
            arcpy.AddMessage('      - no panels available in the area')
        else:
            # panel available, just not records in area
            in_rows = arcpy.SearchCursor(config.flood_panel_selectedby_order_shp)
            for in_row in in_rows:
                # arcpy.AddMessage('      - : ' + in_row.FIRM_PAN)    # panel number
                # arcpy.AddMessage('      - %s' % in_row.EFF_DATE)      # effective date
                flood_panels = flood_panels + in_row.FIRM_PAN+'(effective:' + str(in_row.EFF_DATE)[0:10]+') '
                del in_row
            del in_rows
        
        if len(flood_panels) > 0:
            eris_id += 1
            # arcpy.AddMessage('      - erisid for flood_panels is ' + str(eris_id))
            psr_obj.insert_order_detail(order_obj.id,eris_id, '10683')   
            psr_obj.insert_flex_rep(order_obj, eris_id, '10683', 2, 'N', 1, 'Available FIRM Panels in area: ', flood_panels)
        psr_obj.insert_map(order_obj.id, 'FLOOD', order_obj.number + '_US_FLOOD.jpg', 1)
    else:
        in_rows = arcpy.SearchCursor(config.flood_panel_selectedby_order_shp)
        for in_row in in_rows:
            # arcpy.AddMessage('      : ' + in_row.FIRM_PAN)      # panel number
            # arcpy.AddMessage('      - %s' %in_row.EFF_DATE)             # effective date
            flood_panels = flood_panels + in_row.FIRM_PAN+'(effective:' + str(in_row.EFF_DATE)[0:10]+') '
            del in_row
        del in_rows

        in_rows = arcpy.SearchCursor(os.path.join(config.scratch_folder,"summary_flood.dbf"))
        eris_id += 1
        psr_obj.insert_order_detail(order_obj.id , eris_id, '10683')
        psr_obj.insert_flex_rep(order_obj.id, eris_id, '10683', 2, 'N', 1, 'Available FIRM Panels in area: ', flood_panels)
        
        for in_row in in_rows:
            # note the column changed in summary dbf
            # arcpy.AddMessage('      : ' + in_row.ERIS_CLASS)    # eris label
            # arcpy.AddMessage('      : ' + (in_row.FIRST_FLD_))           # zone type
            # arcpy.AddMessage('      : '+ (in_row.FIRST_ZONE))           # subtype

            eris_id += 1
            config.flood_ids.append([in_row.ERIS_CLASS,eris_id])
            
            psr_obj.insert_order_detail(order_obj.id,eris_id, '10683')   
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10683', 2, 'S1', 1, 'Flood Zone ' + in_row.ERIS_CLASS, '')
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10683', 2, 'N', 2, 'Zone: ', in_row.FIRST_FLD_)
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10683', 2, 'N', 3, 'Zone subtype: ', in_row.FIRST_ZONE)
            del in_row
        del in_rows
        psr_obj.insert_map(order_obj.id, 'FLOOD', order_obj.number + '_US_FLOOD.jpg'+'.jpg', 1)
    end = timeit.default_timer()
    arcpy.AddMessage((' -- End generating PSR flood report. Duration:', round(end -start,4)))
Ejemplo n.º 5
0
def create_map(order_obj):
    point = arcpy.Point()
    array = arcpy.Array()
    feature_list = []
    width = arcpy.Describe(config.order_buffer_shp).extent.width / 2
    height = arcpy.Describe(config.order_buffer_shp).extent.height / 2

    if (width > 662 or height > 662):
        if (width / height > 1):
            # buffer has a wider shape
            width = width * 1.1
            height = width

        else:
            # buffer has a vertically elonged shape
            height = height * 1.1
            width = height
    else:
        width = 662 * 1.1
        height = 662 * 1.1
    width = width + 6400  #add 2 miles to each side, for multipage soil
    height = height + 6400  #add 2 miles to each side, for multipage soil
    xCentroid = (arcpy.Describe(config.order_buffer_shp).extent.XMax +
                 arcpy.Describe(config.order_buffer_shp).extent.XMin) / 2
    yCentroid = (arcpy.Describe(config.order_buffer_shp).extent.YMax +
                 arcpy.Describe(config.order_buffer_shp).extent.YMin) / 2
    point.X = xCentroid - width
    point.Y = yCentroid + height
    array.add(point)
    point.X = xCentroid + width
    point.Y = yCentroid + height
    array.add(point)
    point.X = xCentroid + width
    point.Y = yCentroid - height
    array.add(point)
    point.X = xCentroid - width
    point.Y = yCentroid - height
    array.add(point)
    point.X = xCentroid - width
    point.Y = yCentroid + height
    array.add(point)
    feat = arcpy.Polygon(array, order_obj.spatial_ref_pcs)
    array.removeAll()
    feature_list.append(feat)

    frame_soil = os.path.join(config.scratch_folder, "frame_soil.shp")
    arcpy.CopyFeatures_management(feature_list, frame_soil)
    arcpy.SelectLayerByLocation_management(config.soil_lyr, 'intersect',
                                           frame_soil)
    arcpy.CopyFeatures_management(config.soil_lyr,
                                  config.soil_selectedby_frame)

    # add another column to soil_disp just for symbology purpose
    arcpy.AddField_management(
        os.path.join(config.scratch_folder, config.soil_selectedby_frame),
        "FIDCP", "TEXT", "", "", "", "", "NON_NULLABLE", "REQUIRED", "")
    arcpy.CalculateField_management(
        os.path.join(config.scratch_folder, config.soil_selectedby_frame),
        'FIDCP', '!FID!', "PYTHON_9.3")

    mxd_soil = arcpy.mapping.MapDocument(config.mxd_file_soil)
    df_soil = arcpy.mapping.ListDataFrames(mxd_soil, "*")[0]
    df_soil.spatialReference = order_obj.spatial_ref_pcs

    ssurgo_lyr = arcpy.mapping.ListLayers(mxd_soil, "SSURGO*", df_soil)[0]
    ssurgo_lyr.replaceDataSource(config.scratch_folder, "SHAPEFILE_WORKSPACE",
                                 'soil_selectedby_frame')
    ssurgo_lyr.symbology.addAllValues()
    config.soil_lyr = ssurgo_lyr

    utility.add_layer_to_mxd("order_buffer", df_soil, config.buffer_lyr_file,
                             1.1)
    utility.add_layer_to_mxd("order_geometry_pcs", df_soil,
                             config.order_geom_lyr_file, 1)
    arcpy.RefreshActiveView()
    output_jpg_soil = config.output_jpg(order_obj, config.Report_Type.soil)
    if not config.if_multi_page:  # single-page
        mxd_soil.saveACopy(os.path.join(config.scratch_folder, "mxd_soil.mxd"))
        arcpy.mapping.ExportToJPEG(mxd_soil, output_jpg_soil, "PAGE_LAYOUT",
                                   480, 640, 150, "False", "24-BIT_TRUE_COLOR",
                                   85)
        if not os.path.exists(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
            os.mkdir(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        shutil.copy(
            output_jpg_soil,
            os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        arcpy.AddMessage(
            '      - output jpg image %s' %
            os.path.join(config.report_path, 'PSRmaps', order_obj.number,
                         os.path.basename(output_jpg_soil)))
        del mxd_soil
        del df_soil
    else:  # multipage
        grid_lyr_shp = os.path.join(config.scratch_folder, 'grid_lyr_soil.shp')
        arcpy.GridIndexFeatures_cartography(grid_lyr_shp,
                                            config.order_buffer_shp, "", "",
                                            "", config.grid_size,
                                            config.grid_size)
        # part 1: the overview map
        # add grid layer
        grid_layer = arcpy.mapping.Layer(config.grid_lyr_file)
        grid_layer.replaceDataSource(config.scratch_folder,
                                     "SHAPEFILE_WORKSPACE", "grid_lyr_soil")
        arcpy.mapping.AddLayer(df_soil, grid_layer, "Top")

        df_soil.extent = grid_layer.getExtent()
        df_soil.scale = df_soil.scale * 1.1

        mxd_soil.saveACopy(os.path.join(config.scratch_folder, "mxd_soil.mxd"))
        arcpy.mapping.ExportToJPEG(mxd_soil, output_jpg_soil, "PAGE_LAYOUT",
                                   480, 640, 150, "False", "24-BIT_TRUE_COLOR",
                                   85)
        if not os.path.exists(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
            os.mkdir(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        shutil.copy(
            output_jpg_soil,
            os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        arcpy.AddMessage(
            '      - output jpg image page 1: %s' %
            os.path.join(config.report_path, 'PSRmaps', order_obj.number,
                         os.path.basename(output_jpg_soil)))
        del mxd_soil
        del df_soil

        # part 2: the data driven pages maps
        page = int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) + 1
        mxd_mm_soil = arcpy.mapping.MapDocument(config.mxd_mm_file_soil)

        df_mm_soil = arcpy.mapping.ListDataFrames(mxd_mm_soil, "*")[0]
        df_mm_soil.spatialReference = order_obj.spatial_ref_pcs

        utility.add_layer_to_mxd("order_buffer", df_mm_soil,
                                 config.buffer_lyr_file, 1.1)
        utility.add_layer_to_mxd("order_geometry_pcs", df_mm_soil,
                                 config.order_geom_lyr_file, 1)

        ssurgo_lyr = arcpy.mapping.ListLayers(mxd_mm_soil, "SSURGO*",
                                              df_mm_soil)[0]
        ssurgo_lyr.replaceDataSource(config.scratch_folder,
                                     "SHAPEFILE_WORKSPACE",
                                     "soil_selectedby_frame")
        ssurgo_lyr.symbology.addAllValues()
        config.soil_lyr = ssurgo_lyr

        grid_layer_mm = arcpy.mapping.ListLayers(mxd_mm_soil, "Grid",
                                                 df_mm_soil)[0]
        grid_layer_mm.replaceDataSource(config.scratch_folder,
                                        "SHAPEFILE_WORKSPACE", "grid_lyr_soil")
        arcpy.CalculateAdjacentFields_cartography(grid_lyr_shp, 'PageNumber')
        mxd_mm_soil.saveACopy(
            os.path.join(config.scratch_folder, "mxd_mm_soil.mxd"))

        for i in range(
                1,
                int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) + 1):
            arcpy.SelectLayerByAttribute_management(
                grid_layer_mm, "NEW_SELECTION", ' "PageNumber" =  ' + str(i))
            df_mm_soil.extent = grid_layer_mm.getSelectedExtent(True)
            df_mm_soil.scale = df_mm_soil.scale * 1.1
            arcpy.SelectLayerByAttribute_management(grid_layer_mm,
                                                    "CLEAR_SELECTION")

            title_text = arcpy.mapping.ListLayoutElements(
                mxd_mm_soil, "TEXT_ELEMENT", "title")[0]
            title_text.text = "SSURGO Soils - Page " + str(i)
            title_text.elementPositionX = 0.6156
            arcpy.RefreshTOC()

            arcpy.mapping.ExportToJPEG(mxd_mm_soil,
                                       output_jpg_soil[0:-4] + str(i) + ".jpg",
                                       "PAGE_LAYOUT", 480, 640, 150, "False",
                                       "24-BIT_TRUE_COLOR", 85)
            if not os.path.exists(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number)):
                os.mkdir(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number))
            shutil.copy(
                output_jpg_soil[0:-4] + str(i) + ".jpg",
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        del mxd_mm_soil
        del df_mm_soil
        ### Update DB if multiple pages
        psr_obj = models.PSR()
        for i in range(1, page):
            psr_obj.insert_map(order_obj.id, 'SOIL',
                               order_obj.number + str(i) + '.jpg', i + 1)

    eris_id = 0
    ### Udpate DB
    psr_obj = models.PSR()
    for map_unit in config.report_data:
        eris_id = eris_id + 1
        mu_key = str(map_unit['Mukey'])
        config.soil_ids.append([map_unit['Musym'], eris_id])
        psr_obj.insert_order_detail(order_obj.id, eris_id, mu_key)
        psr_obj.insert_flex_rep(
            order_obj.id, eris_id, '9334', 2, 'S1', 1, 'Map Unit ' +
            map_unit['Musym'] + " (%s)" % map_unit["Soil_Percent"], '')
        psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2, 'N', 2,
                                'Map Unit Name:', map_unit['Map Unit Name'])
        if len(map_unit) < 6:  #for Water, Urbanland and Gravel Pits
            psr_obj.insert_flex_rep(
                order_obj.id, eris_id, '9334', 2, 'N', 3,
                'No more attributes available for this map unit', '')
        else:  # not do for Water or urban land
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2, 'N', 3,
                                    'Bedrock Depth - Min:',
                                    map_unit['Bedrock Depth - Min'])
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2, 'N', 4,
                                    'Watertable Depth - Annual Min:',
                                    map_unit['Watertable Depth - Annual Min'])
            if (map_unit['Drainage Class - Dominant'] == '-99'):
                psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2, 'N',
                                        5, 'Drainage Class - Dominant:',
                                        'null')
            else:
                psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2, 'N',
                                        5, 'Drainage Class - Dominant:',
                                        map_unit['Drainage Class - Dominant'])
            if (map_unit['Hydrologic Group - Dominant'] == '-99'):
                psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2, 'N',
                                        6, 'Hydrologic Group - Dominant:',
                                        'null')
            else:
                psr_obj.insert_flex_rep(
                    order_obj.id, eris_id, '9334', 2, 'N', 6,
                    'Hydrologic Group - Dominant:',
                    map_unit['Hydrologic Group - Dominant'] + ' - ' +
                    config.hydrologic_dict[
                        map_unit['Hydrologic Group - Dominant']])
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2, 'N', 7,
                                    'Major components are printed below', '')

            k = 7
            if 'component' in map_unit.keys():
                k = k + 1
                for comp in map_unit['component']:
                    psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334', 2,
                                            'S2', k, comp[0][0], '')
                    for i in range(1, len(comp)):
                        k = k + 1
                        psr_obj.insert_flex_rep(order_obj.id, eris_id, '9334',
                                                2, 'S3', k, comp[i][0],
                                                comp[i][1])
    psr_obj.insert_map(order_obj.id, 'SOIL', order_obj.number + 'US_SOIL.jpg',
                       1)
Ejemplo n.º 6
0
def generate_geology_report(order_obj):
    arcpy.AddMessage('  -- Start generating PSR geology report...')
    start = timeit.default_timer()
    ### set scratch folder
    arcpy.env.workspace = config.scratch_folder
    arcpy.env.overwriteOutput = True
    output_jpg_geology = config.output_jpg(order_obj,
                                           config.Report_Type.geology)
    page = 1
    if '10685' not in order_obj.psr.search_radius.keys():
        arcpy.AddMessage('      -- Geology search radius is not availabe')
        return
    config.buffer_dist_geology = str(
        order_obj.psr.search_radius['10685']) + ' MILES'

    ### create buffer map based on order geometry
    arcpy.Buffer_analysis(config.order_geometry_pcs_shp,
                          config.order_buffer_shp, config.buffer_dist_geology)

    arcpy.MakeFeatureLayer_management(config.data_geology, 'geology_lyr')
    arcpy.SelectLayerByLocation_management('geology_lyr', 'intersect',
                                           config.order_buffer_shp)
    arcpy.CopyFeatures_management('geology_lyr',
                                  config.geology_selectedby_order_shp)

    arcpy.Statistics_analysis(
        config.geology_selectedby_order_shp,
        os.path.join(config.scratch_folder, "summary_geology.dbf"),
        [['UNIT_NAME', 'FIRST'], ['UNIT_AGE', 'FIRST'], ['ROCKTYPE1', 'FIRST'],
         ['ROCKTYPE2', 'FIRST'], ['UNITDESC', 'FIRST'],
         ['ERIS_KEY_1', 'FIRST']], 'ORIG_LABEL')
    arcpy.Sort_management(
        os.path.join(config.scratch_folder, "summary_geology.dbf"),
        os.path.join(config.scratch_folder, "summary_sorted_geol.dbf"),
        [["ORIG_LABEL", "ASCENDING"]])

    mxd_geology = arcpy.mapping.MapDocument(config.mxd_file_geology)
    df_geology = arcpy.mapping.ListDataFrames(mxd_geology, "*")[0]
    df_geology.spatialReference = order_obj.spatial_ref_pcs

    ### add order and order_buffer layers to geology mxd file
    utility.add_layer_to_mxd("order_buffer", df_geology,
                             config.buffer_lyr_file, 1.1)
    utility.add_layer_to_mxd("order_geometry_pcs", df_geology,
                             config.order_geom_lyr_file, 1)

    if not config.if_multi_page:  # single-page
        #df.scale = 5000
        mxd_geology.saveACopy(
            os.path.join(config.scratch_folder, "mxd_geology.mxd"))
        arcpy.mapping.ExportToJPEG(mxd_geology, output_jpg_geology,
                                   "PAGE_LAYOUT", 480, 640, 150, "False",
                                   "24-BIT_TRUE_COLOR", 85)
        if not os.path.exists(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
            os.mkdir(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        shutil.copy(
            output_jpg_geology,
            os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        arcpy.AddMessage(
            '      - output jpg image: %s' %
            os.path.join(config.report_path, 'PSRmaps', order_obj.number,
                         os.path.basename(output_jpg_geology)))
        del mxd_geology
        del df_geology
    else:  # multipage
        grid_lyr_shp = os.path.join(config.scratch_folder,
                                    'grid_lyr_geology.shp')
        arcpy.GridIndexFeatures_cartography(grid_lyr_shp,
                                            config.order_buffer_shp, "", "",
                                            "", config.grid_size,
                                            config.grid_size)

        # part 1: the overview map
        # add grid layer
        grid_layer = arcpy.mapping.Layer(config.grid_lyr_file)
        grid_layer.replaceDataSource(config.scratch_folder,
                                     "SHAPEFILE_WORKSPACE", "grid_lyr_geology")
        arcpy.mapping.AddLayer(df_geology, grid_layer, "Top")

        df_geology.extent = grid_layer.getExtent()
        df_geology.scale = df_geology.scale * 1.1

        mxd_geology.saveACopy(
            os.path.join(config.scratch_folder, "mxd_geology.mxd"))
        arcpy.mapping.ExportToJPEG(mxd_geology, output_jpg_geology,
                                   "PAGE_LAYOUT", 480, 640, 150, "False",
                                   "24-BIT_TRUE_COLOR", 85)

        if not os.path.exists(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number)):
            os.mkdir(
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        shutil.copy(
            output_jpg_geology,
            os.path.join(config.report_path, 'PSRmaps', order_obj.number))
        arcpy.AddMessage(
            '      - output jpg image page 1: %s' %
            os.path.join(config.report_path, 'PSRmaps', order_obj.number,
                         os.path.basename(output_jpg_geology)))
        del mxd_geology
        del df_geology

        # part 2: the data driven pages

        page = int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) + 1
        mxd_mm_geology = arcpy.mapping.MapDocument(config.mxd_mm_file_geology)

        df_mm_geology = arcpy.mapping.ListDataFrames(mxd_mm_geology, "*")[0]
        df_mm_geology.spatialReference = order_obj.spatial_ref_pcs
        utility.add_layer_to_mxd("order_buffer", df_mm_geology,
                                 config.buffer_lyr_file, 1.1)
        utility.add_layer_to_mxd("order_geometry_pcs", df_mm_geology,
                                 config.order_geom_lyr_file, 1)

        grid_layer_mm = arcpy.mapping.ListLayers(mxd_mm_geology, "Grid",
                                                 df_mm_geology)[0]
        grid_layer_mm.replaceDataSource(config.scratch_folder,
                                        "SHAPEFILE_WORKSPACE",
                                        "grid_lyr_geology")
        arcpy.CalculateAdjacentFields_cartography(grid_lyr_shp, "PageNumber")
        mxd_mm_geology.saveACopy(
            os.path.join(config.scratch_folder, "mxd_mm_geology.mxd"))

        for i in range(
                1,
                int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) + 1):
            arcpy.SelectLayerByAttribute_management(
                grid_layer_mm, "NEW_SELECTION", ' "PageNumber" =  ' + str(i))
            df_mm_geology.extent = grid_layer_mm.getSelectedExtent(True)
            df_mm_geology.scale = df_mm_geology.scale * 1.1
            arcpy.SelectLayerByAttribute_management(grid_layer_mm,
                                                    "CLEAR_SELECTION")

            title_text = arcpy.mapping.ListLayoutElements(
                mxd_mm_geology, "TEXT_ELEMENT", "title")[0]
            title_text.text = "Geologic Units - Page " + str(i)
            title_text.elementPositionX = 0.6303
            arcpy.RefreshTOC()

            arcpy.mapping.ExportToJPEG(
                mxd_mm_geology, output_jpg_geology[0:-4] + str(i) + ".jpg",
                "PAGE_LAYOUT", 480, 640, 150, "False", "24-BIT_TRUE_COLOR", 85)
            if not os.path.exists(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number)):
                os.mkdir(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number))
            shutil.copy(
                output_jpg_geology[0:-4] + str(i) + ".jpg",
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
            # arcpy.AddMessage('      - output jpg image: %s' % os.path.join(config.report_path, 'PSRmaps', order_obj.number, os.path.basename(output_jpg_geology[0:-4]+str(i)+".jpg")))
        del mxd_mm_geology
        del df_mm_geology
        psr_obj = models.PSR()
        for i in range(1, page):
            psr_obj.insert_map(order_obj.id, 'GEOL',
                               order_obj.number + '_US_GEOL' + str(i) + '.jpg',
                               i + 1)

    if (int(
            arcpy.GetCount_management(
                os.path.join(config.scratch_folder,
                             "summary_sorted_geol.dbf")).getOutput(0)) == 0):
        # no geology polygon selected...., need to send in map only
        arcpy.AddMessage('No geology polygon is selected....')
        psr_obj = models.PSR()
        psr_obj.insert_map(order_obj.id, 'GEOL',
                           order_obj.number + '_US_GEOLOGY.jpg',
                           1)  #note type 'SOIL' or 'GEOL' is used internally
    else:
        eris_id = 0
        psr_obj = models.PSR()
        in_rows = arcpy.SearchCursor(
            os.path.join(config.scratch_folder,
                         config.geology_selectedby_order_shp))
        for in_row in in_rows:
            # note the column changed in summary dbf
            # arcpy.AddMessage("Unit label is: " + in_row.ORIG_LABEL)
            # arcpy.AddMessage(in_row.UNIT_NAME)     # unit name
            # arcpy.AddMessage(in_row.UNIT_AGE)      # unit age
            # arcpy.AddMessage( in_row.ROCKTYPE1)      # rocktype 1
            # arcpy.AddMessage( in_row.ROCKTYPE2)      # rocktype2
            # arcpy.AddMessage( in_row.UNITDESC)       # unit description
            # arcpy.AddMessage( in_row.ERIS_KEY_1)     # eris key created from upper(unit_link)
            eris_id = eris_id + 1
            config.geology_ids.append([in_row.ERIS_KEY_1, eris_id])
            psr_obj.insert_order_detail(order_obj.id, eris_id, '10685')
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10685', 2, 'S1', 1,
                                    'Geologic Unit ' + in_row.ORIG_LABEL, '')
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10685', 2, 'N', 2,
                                    'Unit Name: ', in_row.UNIT_NAME)
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10685', 2, 'N', 3,
                                    'Unit Age: ', in_row.UNIT_AGE)
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10685', 2, 'N', 4,
                                    'Primary Rock Type ', in_row.ROCKTYPE1)
            psr_obj.insert_flex_rep(order_obj.id, eris_id, '10685', 2, 'N', 4,
                                    'Secondary Rock Type: ', in_row.ROCKTYPE2)
            if in_row.UNITDESC == None:
                node_scr = 'No description available.'
                psr_obj.insert_flex_rep(order_obj.id, eris_id, '10685', 2, 'N',
                                        6, 'Unit Description: ', node_scr)
            else:
                psr_obj.insert_flex_rep(order_obj.id, eris_id, '10685', 2, 'N',
                                        6, 'Unit Description: ',
                                        in_row.UNITDESC.encode('utf-8'))
            del in_row
        del in_rows
        psr_obj.insert_map(order_obj.id, 'GEOL',
                           order_obj.number + '_US_GEOLOGY.jpg', 1)

    end = timeit.default_timer()
    arcpy.AddMessage((' -- End generating PSR geology report. Duration:',
                      round(end - start, 4)))
Ejemplo n.º 7
0
def generate_wetland_report(order_obj):
    arcpy.AddMessage('  -- Start generating PSR wetland report...')
    start = timeit.default_timer()
    ### set scratch folder
    arcpy.env.workspace = config.scratch_folder
    arcpy.env.overwriteOutput = True

    output_jpg_wetland = config.output_jpg(order_obj,
                                           config.Report_Type.wetland)
    output_jpg_ny_wetland = os.path.join(
        config.scratch_folder,
        str(order_obj.number) + '_NY_WETL.jpg')

    ### Wetland Map
    if '10684' not in order_obj.psr.search_radius.keys():
        arcpy.AddMessage('      -- Wetland search radius is not availabe')
        return
    config.buffer_dist_wetland = str(
        order_obj.psr.search_radius['10684']) + ' MILES'
    arcpy.Buffer_analysis(config.order_geometry_pcs_shp,
                          config.order_buffer_shp, config.buffer_dist_wetland)
    mxd_wetland = arcpy.mapping.MapDocument(config.mxd_file_wetland)
    df_wetland = arcpy.mapping.ListDataFrames(mxd_wetland, "big")[0]
    df_wetland.spatialReference = order_obj.spatial_ref_pcs
    df_wetland_small = arcpy.mapping.ListDataFrames(mxd_wetland, "small")[0]
    df_wetland_small.spatialReference = order_obj.spatial_ref_pcs
    del df_wetland_small
    ### add order and order_buffer layers to wetland mxd file
    utility.add_layer_to_mxd("order_buffer", df_wetland,
                             config.buffer_lyr_file, 1.1)
    utility.add_layer_to_mxd("order_geometry_pcs", df_wetland,
                             config.order_geom_lyr_file, 1)

    # print the maps
    if not config.if_multi_page:  # sinle page report
        generate_singlepage_report(order_obj, mxd_wetland, output_jpg_wetland,
                                   config.scratch_folder)
    else:  # multipage report
        generate_multipage_report(order_obj, mxd_wetland, output_jpg_wetland,
                                  df_wetland)
### Create wetland report for Newyork province
    if order_obj.province == 'NY':
        arcpy.AddMessage("      - Starting NY wetland section: " +
                         time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
        buffer_wetland_shp = os.path.join(config.scratch_folder,
                                          "buffer_wetland.shp")
        mxd_wetland_ny = arcpy.mapping.MapDocument(config.mxd_file_wetlandNY)
        df_wetland_ny = arcpy.mapping.ListDataFrames(mxd_wetland_ny, "big")[0]
        df_wetland_ny.spatialReference = order_obj.spatial_ref_pcs
        ### add order and order_buffer layers to wetland Newyork mxd file
        utility.add_layer_to_mxd("order_buffer", df_wetland_ny,
                                 config.buffer_lyr_file, 1.1)
        utility.add_layer_to_mxd("order_geometry_pcs", df_wetland_ny,
                                 config.order_geom_lyr_file, 1)

        page = 1
        ### print the maps
        if not config.if_multi_page:
            mxd_wetland_ny.saveACopy(
                os.path.join(config.scratch_folder, "mxd_wetland_ny.mxd"))
            arcpy.mapping.ExportToJPEG(mxd_wetland_ny, output_jpg_ny_wetland,
                                       "PAGE_LAYOUT", 480, 640, 150, "False",
                                       "24-BIT_TRUE_COLOR", 85)
            shutil.copy(
                output_jpg_ny_wetland,
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
            arcpy.AddMessage(
                '      - Wetland Output for NY state: %s' %
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))
            del mxd_wetland_ny
            del df_wetland_ny
        else:  # multipage
            grid_lyr_shp = os.path.join(config.scratch_folder,
                                        'grid_lyr_wetland.shp')
            arcpy.GridIndexFeatures_cartography(
                grid_lyr_shp, buffer_wetland_shp, "", "", "", config.grid_size,
                config.grid_size
            )  #note the tool takes featureclass name only, not the full path

            # part 1: the overview map
            # add grid layer
            grid_layer = arcpy.mapping.Layer(config.grid_lyr_file)
            grid_layer.replaceDataSource(config.scratch_folder,
                                         "SHAPEFILE_WORKSPACE",
                                         "grid_lyr_wetland")
            arcpy.mapping.AddLayer(df_wetland_ny, grid_layer, "Top")

            df_wetland_ny.extent = grid_layer.getExtent()
            df_wetland_ny.scale = df_wetland_ny.scale * 1.1

            mxd_wetland_ny.saveACopy(
                os.path.join(config.scratch_folder, "mxd_wetland_ny.mxd"))
            arcpy.mapping.ExportToJPEG(mxd_wetland_ny, output_jpg_ny_wetland,
                                       "PAGE_LAYOUT", 480, 640, 150, "False",
                                       "24-BIT_TRUE_COLOR", 85)
            if not os.path.exists(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number)):
                os.mkdir(
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number))
            shutil.copy(
                output_jpg_ny_wetland,
                os.path.join(config.report_path, 'PSRmaps', order_obj.number))

            del mxd_wetland_ny
            del df_wetland_ny

            # part 2: the data driven pages

            page = int(
                arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) + page
            mxd_mm_wetland_NY = arcpy.mapping.MapDocument(
                config.mxdMMfile_wetlandNY)

            df_mm_wetland_ny = arcpy.mapping.ListDataFrames(
                mxd_mm_wetland_NY, "big")[0]
            df_mm_wetland_ny.spatialReference = order_obj.spatial_ref_pcs
            ### add order and order_buffer layers to wetland Newyork multipage mxd file
            utility.add_layer_to_mxd("order_buffer", df_mm_wetland_ny,
                                     config.buffer_lyr_file, 1.1)
            utility.add_layer_to_mxd("order_geometry_pcs", df_mm_wetland_ny,
                                     config.order_geom_lyr_file, 1)

            grid_layer_mm = arcpy.mapping.ListLayers(mxd_mm_wetland_NY, "Grid",
                                                     df_mm_wetland_ny)[0]
            grid_layer_mm.replaceDataSource(config.scratch_folder,
                                            "SHAPEFILE_WORKSPACE",
                                            "grid_lyr_wetland")
            arcpy.CalculateAdjacentFields_cartography(grid_lyr_shp,
                                                      "PageNumber")
            mxd_mm_wetland_NY.saveACopy(
                os.path.join(config.scratch_folder, "mxd_mm_wetland_NY.mxd"))

            for i in range(
                    1,
                    int(arcpy.GetCount_management(grid_lyr_shp).getOutput(0)) +
                    1):
                arcpy.SelectLayerByAttribute_management(
                    grid_layer_mm, "NEW_SELECTION",
                    ' "PageNumber" =  ' + str(i))
                df_mm_wetland_ny.extent = grid_layer_mm.getSelectedExtent(True)
                df_mm_wetland_ny.scale = df_mm_wetland_ny.scale * 1.1
                arcpy.SelectLayerByAttribute_management(
                    grid_layer_mm, "CLEAR_SELECTION")
                title_text = arcpy.mapping.ListLayoutElements(
                    df_mm_wetland_ny, "TEXT_ELEMENT", "title")[0]
                title_text.text = "NY Wetland Type - Page " + str(i)
                title_text.elementPositionX = 0.468
                arcpy.RefreshTOC()
                arcpy.mapping.ExportToJPEG(
                    mxd_mm_wetland_NY,
                    output_jpg_ny_wetland[0:-4] + str(i) + ".jpg",
                    "PAGE_LAYOUT", 480, 640, 150, "False", "24-BIT_TRUE_COLOR",
                    85)
                if not os.path.exists(
                        os.path.join(config.report_path, 'PSRmaps',
                                     order_obj.number)):
                    os.mkdir(
                        os.path.join(config.report_path, 'PSRmaps',
                                     order_obj.number))
                shutil.copy(
                    output_jpg_ny_wetland[0:-4] + str(i) + ".jpg",
                    os.path.join(config.report_path, 'PSRmaps',
                                 order_obj.number))
                arcpy.AddMessage(
                    '      -- Wetland Output for NY state: %s' % os.path.join(
                        config.report_path, 'PSRmaps', order_obj.number))
            del mxd_mm_wetland_NY
            del df_mm_wetland_ny

        psr_obj = models.PSR()
        for i in range(
                1, page
        ):  #insert generated .jpg report path into eris_maps_psr table
            psr_obj.insert_map(order_obj.id, 'WETLAND',
                               order_obj.number + '_NY_WETL' + str(i) + '.jpg',
                               int(i) + 1)

    end = timeit.default_timer()
    arcpy.AddMessage((' -- End generating PSR Wetland report. Duration:',
                      round(end - start, 4)))
Ejemplo n.º 8
0
def generate_radon_report(order_obj):
    arcpy.AddMessage('  -- Start generating PSR Radon report...')
    start = timeit.default_timer()
    ### set scratch folder
    arcpy.env.workspace = config.scratch_folder
    arcpy.env.overwriteOutput = True
    if '10689' not in order_obj.psr.search_radius.keys():
        arcpy.AddMessage('      -- Radon search radius is not availabe')
        return
    config.buffer_dist_radon = str(
        order_obj.psr.search_radius['10689']) + ' MILES'

    ### create buffer map based on order geometry
    arcpy.Buffer_analysis(config.order_geometry_pcs_shp,
                          config.order_buffer_shp, config.buffer_dist_radon)

    states_lyr = arcpy.MakeFeatureLayer_management(config.master_lyr_states,
                                                   'states_lyr')
    arcpy.SelectLayerByLocation_management(states_lyr, 'intersect',
                                           config.order_buffer_shp)

    counties_lyr = arcpy.MakeFeatureLayer_management(
        config.master_lyr_counties, 'counties_lyr')
    arcpy.SelectLayerByLocation_management('counties_lyr', 'intersect',
                                           config.order_buffer_shp)

    cities_lyr = arcpy.MakeFeatureLayer_management(config.master_lyr_cities,
                                                   'cities_lyr')
    arcpy.SelectLayerByLocation_management('cities_lyr', 'intersect',
                                           config.order_buffer_shp)

    zip_codes_lyr = arcpy.MakeFeatureLayer_management(
        config.master_lyr_zip_codes, 'zip_codes_lyr')
    arcpy.SelectLayerByLocation_management('zip_codes_lyr', 'intersect',
                                           config.order_buffer_shp)

    state_list = ''
    in_rows = arcpy.SearchCursor(states_lyr)
    for in_row in in_rows:
        state_list = state_list + ',' + in_row.STUSPS
    state_list = state_list.strip(',')  #two letter state
    state_list_str = str(state_list)
    del in_rows
    del in_row

    county_list = ''
    in_rows = arcpy.SearchCursor(counties_lyr)
    for in_row in in_rows:
        county_list = county_list + ',' + in_row.NAME
    county_list = county_list.strip(',')
    county_list_str = str(
        county_list.replace(u'\xed', 'i').replace(u'\xe1',
                                                  'a').replace(u'\xf1', 'n'))
    del in_rows
    if 'in_row' in locals():  #sometimes returns no city
        del in_row

    city_list = ''
    in_rows = arcpy.SearchCursor(cities_lyr)
    for in_row in in_rows:
        city_list = city_list + ',' + in_row.NAME
    city_list = city_list.strip(',')
    del in_rows
    if 'in_row' in locals():  #sometimes returns no city
        del in_row

    if 'NH' in state_list:
        town_lyr = arcpy.MakeFeatureLayer_management(
            config.master_lyr_nh_towns, 'town_lyr')
        arcpy.SelectLayerByLocation_management('town_lyr', 'intersect',
                                               config.order_buffer_shp)

        in_rows = arcpy.SearchCursor(town_lyr)
        for in_row in in_rows:
            city_list = city_list + ',' + in_row.NAME
        city_list = city_list.strip(',')
        del in_rows
        if 'in_row' in locals():  #sometimes returns no city
            del in_row
    city_list_str = str(
        city_list.replace(u'\xed', 'i').replace(u'\xe1',
                                                'a').replace(u'\xf1', 'n'))

    zip_list = ''
    in_rows = arcpy.SearchCursor(zip_codes_lyr)
    for in_row in in_rows:
        zip_list = zip_list + ',' + in_row.ZIP
    zip_list = zip_list.strip(',')
    zip_list_str = str(zip_list)
    del in_rows
    if 'in_row' in locals():
        del in_row
    ### update DB
    psr_obj = models.PSR()
    psr_obj = models.PSR()
    psr_obj.get_radon(order_obj.id, state_list_str, zip_list_str,
                      county_list_str, city_list_str)

    end = timeit.default_timer()
    arcpy.AddMessage((' -- End generating PSR Radon report. Duration:',
                      round(end - start, 4)))