Ejemplo n.º 1
0
def update_cache(map_service_path, scratch_workspace, gfw_env):

    logging.debug('Updating tiles for cached map service {0}'.format(map_service_path))

    # Start the service on localhost-- should be shut off when not in use
    manage_service('dev', map_service_path, 'start')

    source_mxd, local_cache_dir = find_src_mxd_and_cache_dir(map_service_path)
    logging.debug("Found source MXD: {0}".format(source_mxd))

    output_dir = util.create_temp_dir(scratch_workspace)

    # Zoom levels 0 - 6
    scale_aoi = "591657527.591555;295828763.795777;147914381.897889;73957190.948944;" \
                "36978595.474472;18489297.737236;9244648.868618"

    min_scale = scale_aoi.split(';')[0]
    max_scale = scale_aoi.split(';')[-1]

    logging.debug("Generating tiles . . . ")
    cache_dir_name = 'cache'
    arcpy.ManageTileCache_management(output_dir, "RECREATE_ALL_TILES", cache_dir_name, source_mxd,
                                     "ARCGISONLINE_SCHEME", "", scale_aoi, "", "", min_scale, max_scale)

    logging.debug("Copying to local and production cache directories")
    src_cache_dir = os.path.join(output_dir, cache_dir_name, 'Layers')

    if gfw_env == 'prod':
        push_to_production(src_cache_dir, local_cache_dir, map_service_path)
    else:
        logging.debug("Nothing pushed to prod dir; just testing cache generation process")
        logging.debug('Cached tiles are here: {0}'.format(local_cache_dir))

    # Stop the map service-- no need for it to be serving on the DM machine
    manage_service('dev', map_service_path, 'stop')
def createCache(dataSource,outputPath,tilingScheme):

	import arcpy,os,shutil

	#standard web map scales	
	webMapScaleList = ["591657527.591555","295828763.795777","147914381.897889","73957190.948944","36978595.474472","18489297.737236","9244648.868618","4622324.434309","2311162.217155","1155581.108577","577790.554289","288895.277144","144447.638572","72223.819286","36111.909643","18055.954822","9027.977411","4513.988705","2256.994353","1128.497176"]

	scaleList = webMapScaleList [startScale:endScale]

	#use datasource filename as cacheName
	cacheName = os.path.splitext(os.path.basename(dataSource))[0]

	#final output path includes '_alllayers'
	fullCachePath = outputPath + '/' + cacheName + "/_alllayers/"

	#kill output folder tree if it exists
	if os.path.exists(outputPath):
		shutil.rmtree(outputPath)

	#create output folder
	os.makedirs(outputPath)

	#overwrite outputs
	arcpy.env.overwriteOutput = True

	arcpy.ManageTileCache_management(in_cache_location=outputPath, manage_mode="RECREATE_ALL_TILES", in_cache_name=cacheName, in_datasource=dataSource, tiling_scheme="IMPORT_SCHEME", import_tiling_scheme=tilingScheme, scales=scaleList, area_of_interest="", max_cell_size="", min_cached_scale=scaleList[0], max_cached_scale=scaleList[-1])    

	return fullCachePath
Ejemplo n.º 3
0
def ManageTileCache(input_layer, cache_directory, output_scheme, scale_level):
    try:
        # variables
        scales = [1128.497176,2256.994353,4513.988705,9027.977411,18055.954822,36111.909643,72223.819286,144447.638572,
                  288895.277144,577790.554289,1155581.108577,2311162.217155,4622324.434309,9244648.868618,18489297.737236,
                  36978595.474472,73957190.948944,147914381.897889,295828763.795777,591657527.591555]

        list_length = len(scales)

        folder = cache_directory
        mode = "RECREATE_ALL_TILES"
        cacheName = getNameFromFeatureClass(input_layer) + "_cache"
        dataSource = input_layer
        method = "IMPORT_SCHEME"
        tilingScheme = output_scheme
        scale_default = "#"
        areaofinterest = "#"
        maxcellsize = "#"
        maxcachedscale = str(scales[0])
        mincachedscale = str(scales[list_length - 1 - scale_level])

        #  check if directory is present
        if arcpy.Exists(folder+"\\"+cacheName):
            shutil.rmtree(folder+"\\"+cacheName)
            arcpy.AddMessage("Deleted old cache directory: "+folder+"\\"+cacheName)

        arcpy.AddMessage("Creating Tile Cache with "+str(list_length - scale_level)+" levels: L"+str(scale_level)+":"+mincachedscale+" down to L:"+str(list_length - 1)+":"+maxcachedscale)

        result = arcpy.ManageTileCache_management(
            folder, mode, cacheName, dataSource, method, tilingScheme,
            scale_default, areaofinterest, maxcellsize, mincachedscale, maxcachedscale)

        ##arcpy.AddMessage(result.status)

        # return obstruction FC
        return (folder+"\\"+cacheName)

    except arcpy.ExecuteWarning:
        print((arcpy.GetMessages(1)))
        arcpy.AddWarning(arcpy.GetMessages(1))

    except arcpy.ExecuteError:
        print((arcpy.GetMessages(2)))
        arcpy.AddError(arcpy.GetMessages(2))

    # Return any other type of error
    except:
        # By default any other errors will be caught here
        #
        e = sys.exc_info()[1]
        print((e.args[0]))
        arcpy.AddError(e.args[0])
Ejemplo n.º 4
0
def createCache(dataSource, outputPath, tilingScheme):

    import arcpy, os

    #standard web map scales
    webMapScaleList = [
        "591657527.591555", "295828763.795777", "147914381.897889",
        "73957190.948944", "36978595.474472", "18489297.737236",
        "9244648.868618", "4622324.434309", "2311162.217155", "1155581.108577",
        "577790.554289", "288895.277144", "144447.638572", "72223.819286",
        "36111.909643", "18055.954822", "9027.977411", "4513.988705",
        "2256.994353", "1128.497176"
    ]

    #get sitetype from MXD name
    siteType = os.path.basename(dataSource).split('.mxd')[0]

    #default scale range is 0-8 but we need an extra level for gw sites
    scaleList = webMapScaleList[0:9]
    if siteType.split('_')[0] == 'gw':
        scaleList = webMapScaleList[0:10]

    #siteCachePath includes layer group in MXD so we need to include
    siteCachePath = outputPath + '/' + siteType + "/NWIS/_alllayers/"

    #create output folder if it doesnt exist
    if not os.path.exists(outputPath):
        os.makedirs(outputPath)

    #overwrite outputs
    arcpy.env.overwriteOutput = True

    arcpy.ManageTileCache_management(in_cache_location=outputPath,
                                     manage_mode="RECREATE_ALL_TILES",
                                     in_cache_name=siteType,
                                     in_datasource=dataSource,
                                     tiling_scheme="IMPORT_SCHEME",
                                     import_tiling_scheme=tilingScheme,
                                     scales=scaleList,
                                     area_of_interest="",
                                     max_cell_size="",
                                     min_cached_scale=scaleList[0],
                                     max_cached_scale=scaleList[-1])

    return siteCachePath
Ejemplo n.º 5
0
    def add_dataset(self, filename):
        raster_desc = arcpy.Describe(filename)
        if raster_desc is None:
            arcpy.AddError("Failed to describe input")
            return False

        arcpy.AddMessage("Raster described {0}".format(filename))

        srs = raster_desc.spatialReference
        extent = raster_desc.Extent

        #compute the new projected extent
        new_srs = arcpy.SpatialReference(3857)
        new_extent = extent.projectAs(new_srs)

        #compute the new projected source cellsize using the extent of one cell from the input
        #extent of one cell in the input is:
        #xmin = input_xmin
        #xmax = input_xmin + input_x_cell_size
        #ymin = input_ymin
        #ymax = input_ymin + input_y_cell_size

        input_cs_x = float(
            str(arcpy.GetRasterProperties_management(filename, 'CELLSIZEX')))
        input_cs_y = float(
            str(arcpy.GetRasterProperties_management(filename, 'CELLSIZEY')))

        arcpy.AddMessage("Input CS X: {0}".format(input_cs_x))
        arcpy.AddMessage("Input CS Y: {0}".format(input_cs_y))

        #update the 'extent' with cell extent
        extent.XMax = extent.XMin + input_cs_x
        extent.YMax = extent.YMin + input_cs_y

        new_cell_extent = extent.projectAs(new_srs)

        # Get the cell size of the projected_raster
        pixel_x_size = new_cell_extent.width
        pixel_y_size = new_cell_extent.height

        # Get the extent of the projected_raster
        max_y = new_extent.YMax
        min_y = new_extent.YMin
        min_x = new_extent.XMin
        max_x = new_extent.XMax

        if pixel_x_size == 0 or pixel_y_size == 0:
            print("Invalid pixel sizes")
            return False

        if min_x == 0.0 or min_y == 0.0 or max_x == 0.0 or max_y == 0.0:
            print("Invalid extent")
            return False

        # Set the source_pixel_size to twice the original resolution to compute the max scale
        source_pixel_size = pixel_x_size

        # Set the max cell size to twice the cell size required for a super tile size of 512
        # This seems to be inaccurate
        # max_pixel_size = (max_x - min_x) / 256

        new_dims = (max_x - min_x) // source_pixel_size + (
            (max_x - min_x) % source_pixel_size > 0)
        zoom_levels = int(math.log(new_dims / 512.0, 2) +
                          1) if new_dims >= 512 else 0

        #Prefering SubScaling
        for counter, lod_info in enumerate(self.tile_lod_info):
            print(lod_info[0], lod_info[1])
            if source_pixel_size > lod_info[0]:
                break
            max_scale = lod_info[1]

        min_scale = (self.tile_lod_info[counter - 1 -
                                        zoom_levels][1] if counter - 1 -
                     zoom_levels > 0 else self.tile_lod_info[0][1])

        tempFolder = tempfile.mkdtemp(suffix='_gpkg_cache')
        cacheName = os.path.basename(filename)

        arcmap_dir = arcpy.GetInstallInfo()['InstallDir']
        if (arcpy.Exists(arcmap_dir) == False):
            raise arcpy.ExecuteError("Arc Installation not found.")
        arcmap_tilingscheme_dir = os.path.join(arcmap_dir, 'TilingSchemes',
                                               'gpkg_scheme.xml')

        if os.path.isfile(arcmap_tilingscheme_dir) == False:
            raise arcpy.ExecuteError("Tiling Scheme File is Missing.")

        arcpy.AddMessage("Generating tiles in {0}".format(tempFolder))

        arcpy.ManageTileCache_management(
            in_cache_location=tempFolder,
            manage_mode='RECREATE_ALL_TILES',
            in_cache_name=cacheName,
            in_datasource=filename,
            tiling_scheme='IMPORT_SCHEME',
            import_tiling_scheme=arcmap_tilingscheme_dir,
            max_cached_scale=max_scale,
            min_cached_scale=min_scale)

        arcpy.AddMessage("Creating GeoPackage {0}".format(self.filename))

        cachePath = tempFolder + "/" + cacheName
        cache2gpkg.cache2gpkg(cachePath, self.filename, True)

        arcpy.AddMessage("GeoPackage {0} created".format(self.filename))

        ### Cleanup
        new_srs = None
        new_extent = None
        raster_desc = None
        shutil.rmtree(tempFolder)

        return True
Ejemplo n.º 6
0
XML_SCHEME = arcpy.GetParameterAsText(6)

arcpy.AddMessage("\nExecutando seleção do filtro {0} no dataset {1}".format(
    AOI_FILTER, AOI_DATASET))
arcpy.Delete_management(AOI_SELECTION)
arcpy.Select_analysis(AOI_DATASET, AOI_SELECTION, AOI_FILTER)

DT_INICIO = datetime.now()
arcpy.AddMessage("\nGerando o Tile Cache para o projeto de mapa: {0}".format(
    PROJETO_MAPA_BASE))

NOME_TILE_CACHE = ntpath.basename((PROJETO_MAPA_BASE.split("."))[0])

arcpy.ManageTileCache_management(OUTPUT_DIR, "RECREATE_ALL_TILES",
                                 NOME_TILE_CACHE, PROJETO_MAPA_BASE,
                                 "IMPORT_SCHEME", XML_SCHEME, BASEMAP_SCALES,
                                 "", "", MIN_SCALE, MAX_SCALE)

DT_FIM = datetime.now()
arcpy.AddMessage(
    "Finalizado tile cache em {0} (horas:minutos:segundos)".format(
        (DT_FIM - DT_INICIO)))

with arcpy.da.SearchCursor(AOI_SELECTION,
                           ['SHAPE@', AOI_FIELD_NAME]) as cursor:
    for row in cursor:
        DT_INICIO = datetime.now()

        AOI = row[0]
        NOME_AOI = row[1]
Ejemplo n.º 7
0
        scaleList = webMapScaleList[0:9]
        if siteType.split('_')[0] == 'gw':
            scaleList = webMapScaleList[0:10]

        siteCachePath = cachePath + '/' + siteType + "/NWIS/_alllayers/"

        print(siteType + '\n----------------------')

        print "Step 1 -- Creating tile cache..."
        arcpy.ManageTileCache_management(in_cache_location=cachePath,
                                         manage_mode="RECREATE_ALL_TILES",
                                         in_cache_name=siteType,
                                         in_datasource=mxd,
                                         tiling_scheme="IMPORT_SCHEME",
                                         import_tiling_scheme=tilingScheme,
                                         scales=scaleList,
                                         area_of_interest="",
                                         max_cell_size="",
                                         min_cached_scale=scaleList[0],
                                         max_cached_scale=scaleList[-1])
        print "Step 1 -- Done..."

        print "Step 2 -- Removing empty PNG files from cache..."
        subprocess.call(
            ["python", scriptPath + "/2a_removeEmptyPNG.py", siteCachePath])
        print "Step 2 -- Done..."

        print "Step 3 -- Removing empty folders from cache..."
        subprocess.call(
            ["python", scriptPath + "/2b_removeEmptyDirs.py", siteCachePath])
Ejemplo n.º 8
0
maxcellsize = sys.argv[9]
if maxcellsize == '#' or not maxcellsize:
	maxcellsize = "#"

mincachedscale = sys.argv[10]
if mincachedscale == '#' or not mincachedscale:
	mincachedscale = "8000"

maxcachedscale = sys.argv[11]
if maxcachedscale == '#' or not maxcachedscale:
	maxcachedscale = "2000"
#Intializing Staring Time
StTme = datetime.datetime.now()

#Running main script
arcpy.ManageTileCache_management(cachefolder, mode, cacheName, dataSource, method, tilingScheme,scales,areaofinterest, maxcellsize, mincachedscale, maxcachedscale)

#Intializing Ending Time
EdTme = datetime.datetime.now()

#finding the time difference
elapsedTime = EdTme - StTme

#Caluclating time difference in hh:mm:ss
hours = math.floor(elapsedTime / (60*60))
elapsedTime = elapsedTime - hours * (60*60);
minutes = math.floor(elapsedTime / 60)
elapsedTime = elapsedTime - minutes * (60);
seconds = math.floor(elapsedTime);
elapsedTime = elapsedTime - seconds;
ms = elapsedTime * 1000;