Esempio n. 1
0
File: to.py Progetto: jasp382/gasp
def shpext_to_boundary(in_shp, out_srs=None):
    """
    Read one feature class extent and create a boundary with that
    extent
    """

    from gasp.gt.prop.ext import get_ext
    from gasp.g.to import create_polygon

    # Get Extent
    ext = get_ext(in_shp)

    # Create points of the new boundary based on the extent
    boundary_points = [(ext[0], ext[3]), (ext[1], ext[3]), (ext[1], ext[2]),
                       (ext[0], ext[2]), (ext[0], ext[3])]
    polygon = create_polygon(boundary_points)

    if out_srs:
        from gasp.gt.prop.prj import get_epsg_shp

        in_srs = get_epsg_shp(in_shp)

        if in_srs != out_srs:
            from gasp.g.prj import prj_ogrgeom

            poly = prj_ogrgeom(polygon, in_srs, out_srs, api='shply')

            return poly

        else:
            return polygon
    else:
        return polygon
Esempio n. 2
0
def shpext_to_rst(inShp,
                  outRaster,
                  cellsize=None,
                  epsg=None,
                  invalidResultAsNone=None,
                  outEpsg=None):
    """
    Extent to raster
    
    if invalidResultAsNone - if for some reason something went wrong, the 
    result of this method will be a None Object if there is an error on the
    numpy array creation. If False, an error will be raised.
    """

    import os
    import numpy
    from osgeo import ogr, gdal
    from gasp.gt.prop.ff import drv_name
    from gasp.gt.prop.ext import get_ext

    cellsize = 10 if not cellsize else cellsize

    # Get extent
    try:
        left, right, bottom, top = get_ext(inShp)
    except:
        left, right, bottom, top = inShp.GetEnvelope()

    return ext_to_rst((left, top), (right, bottom),
                      outRaster,
                      cellsize=cellsize,
                      epsg=epsg,
                      outEpsg=outEpsg,
                      invalidResultAsNull=invalidResultAsNone)
Esempio n. 3
0
def create_fishnet(boundary, shpfishnet, x, y, xy_row_col=None, srs=None):
    """
    Create a Fishnet
    """

    import os
    from gasp.gt.prop.ext import get_ext
    from gasp.gt.prop.prj import get_epsg
    from gasp.g.smp import fishnet

    # Check Path
    if not os.path.exists(os.path.dirname(shpfishnet)):
        raise ValueError('The path for the output doesn\'t exist')

    # Get boundary extent
    xmin, xmax, ymin, ymax = get_ext(boundary)
    # Get SRS
    epsg = get_epsg(boundary) if not srs else int(srs)

    return fishnet((xmin, ymax), (xmax, ymin),
                   shpfishnet,
                   x,
                   y,
                   xy_row_col=xy_row_col,
                   epsg=epsg)
Esempio n. 4
0
File: to.py Progetto: jasp382/gasp
def fext_to_geof(inF, outF, ocellsize=10):
    """
    Extent of a File to Raster or Shapefile
    """

    from gasp.gt.prop.ext import get_ext
    from gasp.gt.prop.ff import check_isRaster
    from gasp.gt.prop.prj import get_epsg

    # Get extent
    left, right, bottom, top = get_ext(inF)

    # Get EPSG of inF
    EPSG = get_epsg(inF)

    # Export Boundary
    isRst = check_isRaster(outF)

    if isRst:
        from gasp.gt.torst import ext_to_rst

        return ext_to_rst((left, top), (right, bottom),
                          outF,
                          cellsize=ocellsize,
                          epsg=EPSG,
                          invalidResultAsNull=None)
    else:
        from gasp.gt.prop.ff import check_isShp

        isShp = check_isShp(outF)

        if isShp:
            from gasp.gt.toshp.coord import coords_to_boundshp

            return coords_to_boundshp((left, top), (right, bottom), EPSG, outF)

        else:
            raise ValueError(
                '{} is not recognized as a file with GeoData'.format(inF))
Esempio n. 5
0
def optimized_union_anls(lyr_a,
                         lyr_b,
                         outShp,
                         ref_boundary,
                         workspace=None,
                         multiProcess=None):
    """
    Optimized Union Analysis
    
    Goal: optimize v.overlay performance for Union operations
    """

    import os
    from gasp.pyt.oss import fprop, lst_ff
    from gasp.pyt.oss import cpu_cores
    from gasp.gt.sample import create_fishnet
    from gasp.gt.wenv.grs import run_grass
    from gasp.gt.toshp import eachfeat_to_newshp
    from gasp.gt.toshp.mtos import shps_to_shp
    from gasp.gt.attr import split_shp_by_attr
    from gasp.gt.torst import shpext_to_rst
    from gasp.gt.prop.ext import get_ext

    if workspace:
        if not os.path.exists(workspace):
            from gasp.pyt.oss import mkdir

            mkdir(workspace, overwrite=True)

    else:
        from gasp.pyt.oss import mkdir

        workspace = mkdir(os.path.join(os.path.dirname(outShp), "union_work"))

    # Create Fishnet
    ncpu = cpu_cores()
    if ncpu == 12:
        nrow = 4
        ncol = 3
    elif ncpu == 8:
        nrow = 4
        ncol = 2
    else:
        nrow = 2
        ncol = 2

    ext = get_ext(ref_boundary)
    width = (ext[1] - ext[0]) / ncol
    height = (ext[3] - ext[2]) / nrow

    gridShp = create_fishnet(ref_boundary,
                             os.path.join(workspace, 'ref_grid.shp'),
                             width,
                             height,
                             xy_row_col=None)

    # Split Fishnet in several files
    cellsShp = eachfeat_to_newshp(gridShp, workspace)

    if not multiProcess:
        # INIT GRASS GIS Session
        grsbase = run_grass(workspace, location="grs_loc", srs=ref_boundary)

        import grass.script.setup as gsetup

        gsetup.init(grsbase, workspace, "grs_loc", 'PERMANENT')

        # Add data to GRASS GIS
        from gasp.gt.toshp.cff import shp_to_grs

        cellsShp = [
            shp_to_grs(shp, fprop(shp, 'fn'), asCMD=True) for shp in cellsShp
        ]

        LYR_A = shp_to_grs(lyr_a, fprop(lyr_a, 'fn'), asCMD=True)
        LYR_B = shp_to_grs(lyr_b, fprop(lyr_b, 'fn'), asCMD=True)

        # Clip Layers A and B for each CELL in fishnet
        LYRS_A = [
            clip(LYR_A, cellsShp[x], LYR_A + "_" + str(x), api_gis="grass")
            for x in range(len(cellsShp))
        ]
        LYRS_B = [
            clip(LYR_B, cellsShp[x], LYR_B + "_" + str(x), api_gis="grass")
            for x in range(len(cellsShp))
        ]

        # Union SHPS
        UNION_SHP = [
            union(LYRS_A[i], LYRS_B[i], "un_{}".format(i), api_gis="grass")
            for i in range(len(cellsShp))
        ]

        # Export Data
        from gasp.gt.toshp.cff import grs_to_shp

        _UNION_SHP = [
            grs_to_shp(shp, os.path.join(workspace, shp + ".shp"), "area")
            for shp in UNION_SHP
        ]

    else:

        def clip_and_union(la, lb, cell, work, proc, output):
            ref_rst = shpext_to_rst(cell,
                                    os.path.join(os.path.dirname(cell),
                                                 fprop(cell, 'fn') + '.tif'),
                                    cellsize=10)

            # Start GRASS GIS Session
            loc = "proc_" + str(proc)
            grsbase = run_grass(work, location=loc, srs=ref_rst)
            import grass.script.setup as gsetup
            gsetup.init(grsbase, work, loc, 'PERMANENT')

            # Import GRASS GIS modules
            from gasp.gt.toshp.cff import shp_to_grs, grs_to_shp
            from gasp.gt.prop.feat import feat_count

            # Add data to GRASS
            a = shp_to_grs(la, fprop(la, 'fn'), filterByReg=True, asCMD=True)
            b = shp_to_grs(lb, fprop(lb, 'fn'), filterByReg=True, asCMD=True)

            if not feat_count(a, gisApi="grass", work=work, loc=loc):
                return

            if not feat_count(b, gisApi="grass", work=work, loc=loc):
                return

            # Clip
            a_clip = clip(a,
                          None,
                          "{}_clip".format(a),
                          api_gis="grass",
                          clip_by_region=True)
            b_clip = clip(b,
                          None,
                          "{}_clip".format(b),
                          api_gis="grass",
                          clip_by_region=True)

            # Union
            u_shp = union(a_clip,
                          b_clip,
                          "un_{}".format(fprop(cell, 'fn')),
                          api_gis="grass")

            # Export
            o = grs_to_shp(u_shp, output, "area")

        import multiprocessing

        thrds = [
            multiprocessing.Process(
                target=clip_and_union,
                name="th-{}".format(i),
                args=(lyr_a, lyr_b, cellsShp[i],
                      os.path.join(workspace, "th_{}".format(i)), i,
                      os.path.join(workspace, "uniao_{}.shp".format(i))))
            for i in range(len(cellsShp))
        ]

        for t in thrds:
            t.start()

        for t in thrds:
            t.join()

        ff_shp = lst_ff(workspace, file_format='.shp')
        _UNION_SHP = []
        for i in range(len(cellsShp)):
            p = os.path.join(workspace, "uniao_{}.shp".format(i))

            if p in ff_shp:
                _UNION_SHP.append(p)
            else:
                continue

    # Merge all union into the same layer
    MERGED_SHP = shps_to_shp(_UNION_SHP, outShp, api="ogr2ogr")

    return MERGED_SHP