Пример #1
0
def osm_to_featurecls(osmXml, outGeoDatabase):
    """
    OSM XML File to Feature Classes
    """

    import os
    from glass.pys.oss import get_filename
    from glass.cpu.arcg.mng.wspace import create_geodb

    PATH_TO_OSMTBX = r'C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcToolbox\Toolboxes\OpenStreetMap Toolbox.tbx'

    arcpy.ImportToolbox(PATH_TO_OSMTBX)

    geodb = create_geodb(os.path.dirname(outGeoDatabase),
                         get_filename(outGeoDatabase))

    base_name = get_filename(osmXml)
    featDataset = os.path.join(outGeoDatabase, base_name)

    arcpy.OSMGPFileLoader_osmtools(osmXml, "CONSERVE_MEMORY", "ALL",
                                   featDataset,
                                   os.path.join(featDataset, 'osm_pnts'),
                                   os.path.join(featDataset, 'osm_lnhs'),
                                   os.path.join(featDataset, 'osm_poly'))

    return outGeoDatabase, base_name, {
        "POINTS": base_name + '_osm_pt',
        "LINES": base_name + '_osm_ln',
        "POLYGONS": base_name + 'osm_ply'
    }
Пример #2
0
def rst_resampling(inRst, outRst, outCell, template=None, technique=None):
    """
    Change the spatial resolution of your raster dataset and set rules for
    aggregating or interpolating values across the new pixel sizes.
    
    technique options:
    * NEAREST
    * MAJORITY
    * BILINEAR
    * CUBIC
    """

    import os
    from glass.pys import obj_to_lst

    inRst = obj_to_lst(inRst)

    # Get outputs
    outRst = obj_to_lst(outRst)

    if len(inRst) != len(outRst):
        from glass.pys.oss import get_filename

        OUT_FOLDER = outRst[0] if os.path.isdir(outRst[0]) else \
            os.path.dirname(outRst[0]) if os.path.isfile(outRst[0]) else \
            None

        if not OUT_FOLDER:
            raise ValueError('outRst value is not valid')

        outRst = [
            os.path.join(OUT_FOLDER,
                         get_filename(i) + '.tif') for i in inRst
        ]

        for i in range(len(inRst)):
            if inRst[i] == outRst[i]:
                outRst[i] = os.path.join(
                    OUT_FOLDER, "res_{}.tif".format(get_filename(outRst[i])))

    if template:
        tempEnvironment0 = arcpy.env.extent
        arcpy.env.extent = template

    technique = "NEAREST" if not technique else technique

    CELLSIZE = "{a} {a}".format(a=str(outCell))
    for i in range(len(inRst)):
        arcpy.Resample_management(inRst[i], outRst[i], CELLSIZE, technique)

    if template:
        arcpy.env.extent = tempEnvironment0

    return outRst
Пример #3
0
def write_maps_forFolderMXDs(folder, map_format='.jpg'):
    """
    Export map for all mxd in one folder
    """

    import os
    from glass.pys.oss import lst_ff, get_filename

    mxds = lst_ff(folder, file_format='.mxd')

    for mxd in mxds:
        __mxd = arcpy.mapping.MapDocument(mxd)

        write_map(__mxd, os.path.join(folder, get_filename(mxd) + map_format))
Пример #4
0
def dem_from_tin(countors,
                 elevation_field,
                 boundary_tin,
                 boundary_mdt,
                 cellsize,
                 w,
                 output,
                 hidrology=None,
                 __hillshade=None,
                 snapRst=None,
                 prj=None):
    """
    Create a Digital Elevation Model based on a TIN
    """

    import os
    from glass.pys.oss import get_filename, get_fileformat
    from gesri.df.lyr import feat_lyr
    from gesri.dct.torst import tin_to_raster
    from gesri.df.mng.rst.proc import clip_raster

    if not os.path.exists(w):
        from glass.pys.oss import create_folder
        create_folder(w, overwrite=None)

    # Check Extension
    arcpy.CheckOutExtension("3D")
    # Configure workspace
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = w

    prj = os.path.splitext(countors)[0] + '.prj' if not prj else prj

    if type(prj) == int:
        from glass.gt.prop.prj import get_prj_web

        prj = get_prj_web(prj, os.path.join(w, 'prj_{}.prj'.format(str(prj))))

    else:
        if not os.path.exists(prj):
            prj = os.path.splitext(boundary_mdt)[0] + '.prj'
            if not os.path.exists(prj):
                proj = os.path.splitext(boundary_tin)[0] + '.prj'
                if not os.path.exists(prj):
                    raise ValueError('On of the inputs must have a prj file')

    # Create TIN
    tin = create_TINdem(countors, elevation_field, boundary_tin, prj,
                        'tin_tmp', hidrology)
    # TIN2Raster
    rst_tin = tin_to_raster(tin,
                            cellsize,
                            '{}_extra{}'.format(get_filename(output),
                                                get_fileformat(output)),
                            snapRst=snapRst)

    # Clip Raster
    lmt_clip = feat_lyr(boundary_mdt)
    dem_clip = clip_raster(rst_tin, lmt_clip, output, snap=snapRst)

    # Create Hillshade just for fun
    if __hillshade:
        from gesri.df.spanlst.surf import hillshade

        hillshd = hillshade(
            output,
            os.path.join(
                os.path.dirname(output),
                '{}hsd{}'.format(get_filename(output),
                                 get_fileformat(output))))
Пример #5
0
def maps_by_extent(extentSource,
                   baseMxd,
                   MAIN_DATAFRAME,
                   EXTENT_LYR_NAME,
                   CONTEXT_DATAFRAME,
                   MAP_GRAPH_ELEM,
                   outputMxd,
                   outputMaps=None,
                   mapsFormat='.pdf'):
    """
    Export a new map for each "Extent".
    
    Extent is a bounding box file
    
    * extentSource - path to the folder with the bounding boxes files.
    A new map will be generated for every file in this folder.
    
    * baseMxd - path to an template MXD file. The new maps will have the same
    structure.
    
    * MAIN_DATAFRAME - name of the dataframe in the baseMxd that will have
    it extent changed from map to map.
    
    * EXTENT_LYR_NAME - name of the layer that will have it extent changed
    from map to map.
    
    * CONTEXT_DATAFRAME - dict with metainfo about other dataframe with
    data about the geographic context.
    E.g.
    CONTEXT_DATAFRAME = {
        "zoom_pt" : {
            "PATHDATA" : r'D:\TRENMO_JASP\SITIOS_FATIMA\acf_versao_final\lmt_conc',
            "LAYERS" : {
                # LAYER NAME : FIELD IN EXTENT_LAYER WITH THE NAME OF THE FILE
                # THAT SOULD BE USED IN THE LAYER WITH KEY NAME
                "ref_concelho" : "concelho"
            }
        },
        "zoom_concelho" : {
            "PATHDATA" : r'D:\TRENMO_JASP\SITIOS_FATIMA\acf_versao_final\lmt_conc',
            "LAYERS" : {
                "ref_concelho" : "concelho",
                "ref_grid" : "EXTENTFILE"
            },
            "ISZOOM" : "ref_concelho",
            "EXTENT_TOLERANCE" : 1000
        }
    }
    
    MAP_GRAPH_ELEM - file with the indication of what text should appear
    in each text element of the layout when some file is used as data source
    of the EXTENT_LYR;
    
                   | id_element_1 | ... | id_element_n
    file_lyr_name1 |  some_text   | ... |  some_text
    file_lyr_name2 |  some_text   | ... |  some_text
    """

    import os
    from glass.dct import tbl_to_obj
    from glass.pys.oss import get_filename
    from glass.cpu.arcg.mng.fld import list_fields
    from glass.maps.arctbx.lyrs import lst_layers
    from glass.maps.arctbx.lyrs import get_layers_by_dataframe
    from glass.maps.arctbx.dframe import lst_dataframe
    from glass.maps.arctbx.graph import get_elem_by_name
    from glass.maps.arctbx.wmap import write_map
    from glass.maps.arctbx.wmap import mxd_saveas

    outputMaps = outputMxd if not os.path.isdir(outputMaps) else outputMaps
    mapsFormat = mapsFormat if mapsFormat[0] == '.' else '.' + mapsFormat

    # List extent files
    arcpy.env.workspace = extentSource
    maps_ext = arcpy.ListFeatureClasses()

    attrs_data = {}
    for extFile in maps_ext:
        cursor = arcpy.SearchCursor(os.path.join(extentSource, extFile))

        lnh = cursor.next()

        fields = list_fields(os.path.join(extentSource, extFile))

        c = 0
        while lnh:
            if c:
                break

            fdata = {
                field : lnh.getValue(
                    field
                ) for field in fields if field != 'Shape' and \
                    field != "FID" and field != "Id"
            }

            c += 1
            lnh = cursor.next()

        attrs_data[extFile] = fdata

    # List graphic elements to be changed
    files_and_element = tbl_to_obj(MAP_GRAPH_ELEM,
                                   useFirstColAsIndex=True,
                                   output='dict')
    """
    For each extent file, open the main mxd and change extent
    of the main dataframe and change some layers in contextual dataframes
    """
    for extFile in maps_ext:
        # Open mxd
        mxd = arcpy.mapping.MapDocument(baseMxd)

        # Get Text elements in the main mxd to be changed
        elem_names = files_and_element[files_and_element.keys()[0]].keys()
        text_elem = get_elem_by_name(mxd, elem_names)

        if not text_elem:
            print 'No Graphic/Text element was finded'

        # Change every text elements
        for elem_name in text_elem:
            text_elem[elem_name].text = files_and_element[os.path.splitext(
                extFile)[0]][elem_name]

        # Set new map extension
        refDf = lst_dataframe(mxd, dfNames=[MAIN_DATAFRAME])
        refLyr = lst_layers_by_name(mxd,
                                    dataFrames=MAIN_DATAFRAME,
                                    lyrNames=EXTENT_LYR_NAME)

        refLyr.replaceDataSource(extentSource, "NONE",
                                 os.path.splitext(extFile)[0], True)

        ext = refLyr.getExtent()

        newExtent = refDf.extent
        newExtent.XMin, newExtent.YMin = ext.XMin, ext.YMin
        newExtent.XMax, newExtent.YMax = ext.XMax, ext.YMax

        refDf.extent = newExtent

        # Change Context Dataframes
        lyrByDf = get_layers_by_dataframe(mxd)
        for df in CONTEXT_DATAFRAME:
            for mlyr in CONTEXT_DATAFRAME[df]["LAYERS"]:
                if CONTEXT_DATAFRAME[df]["LAYERS"][mlyr] != "EXTENTFILE":
                    mlyrsrc = os.path.join(
                        CONTEXT_DATAFRAME[df]["PATHDATA"], attrs_data[extFile][
                            CONTEXT_DATAFRAME[df]["LAYERS"][mlyr]])
                else:
                    mlyrsrc = os.path.join(extentSource, extFile)

                lyrByDf[df][mlyr].replaceDataSource(
                    os.path.dirname(mlyrsrc), "NONE",
                    os.path.splitext(os.path.basename(mlyrsrc))[0], True)

                if "ISZOOM" in CONTEXT_DATAFRAME[df]:
                    if mlyr == CONTEXT_DATAFRAME[df]["ISZOOM"]:
                        cdf = list_dataframe_by_name(mxd, [df])[0]

                        if "EXTENT_TOLERANCE" in CONTEXT_DATAFRAME[df]:
                            extTol = CONTEXT_DATAFRAME[df]["EXTENT_TOLERANCE"]
                        else:
                            extTol = 0

                        lext = lyrByDf[df][mlyr].getExtent()
                        nExt = cdf.extent
                        nExt.XMin = lext.XMin - extTol
                        nExt.YMin = lext.YMin - extTol
                        nExt.XMax = lext.XMax + extTol
                        nExt.YMax = lext.YMax + extTol

                        cdf.extent = nExt

        # Create a new mxd and export map
        outmap = write_map(
            mxd, os.path.join(outputMaps,
                              get_filename(extFile) + mapsFormat))

        mxd_saveas(
            os.path.join(outputMxd, '{}.mxd'.format(get_filename(extFile))))

        del mxd