Example #1
0
    def exportAndBufferB(CLS, cnt):
        # Run BUFFER Tool
        time_x = datetime.datetime.now().replace(microsecond=0)
        bb_file = st_buffer(osmLink,
                            lineTbl,
                            "bf_basic_buffer",
                            "geometry",
                            os.path.join(
                                folder,
                                'bb_rule5_{}.shp'.format(str(int(CLS)))),
                            whrClause="basic_buffer={}".format(str(int(CLS))),
                            outTblIsFile=True,
                            dissolve=None,
                            cols_select="basic_buffer")
        time_y = datetime.datetime.now().replace(microsecond=0)

        # To raster
        rstCls = shp_to_raster(bb_file,
                               None,
                               cells,
                               0,
                               os.path.join(folder,
                                            'rst_bbfr_{}.tif'.format(CLS)),
                               epsg=srscode,
                               rst_template=rtemplate,
                               api='gdal')
        time_z = datetime.datetime.now().replace(microsecond=0)

        clsRst[CLS] = rstCls
        timeGasto[cnt + 1] = ('buffer_{}'.format(str(CLS)), time_y - time_x)
        timeGasto[cnt + 2] = ('torst_{}'.format(str(CLS)), time_z - time_y)
Example #2
0
def pg_num_roads(osmLink, nom, lnhTbl, polyTbl, folder, cellsize, srs, rstT):
    """
    Select, Calculate Buffer distance using POSTGIS, make buffer of roads
    and convert roads to raster
    """

    import datetime
    import os
    from osgeo import gdal
    from gasp.sql.mng.tbl import row_num
    from gasp.sql.anls.prox import st_buffer
    from gasp.to.rst import shp_to_raster

    # There are roads?
    time_a = datetime.datetime.now().replace(microsecond=0)
    NR = row_num(osmLink, lnhTbl, where="roads IS NOT NULL", api='psql')
    time_b = datetime.datetime.now().replace(microsecond=0)

    if not NR: return None, {0: ('count_rows_roads', time_b - time_a)}

    # There are buildings?
    NB = row_num(osmLink, polyTbl, where="building IS NOT NULL", api='psql')
    time_c = datetime.datetime.now().replace(microsecond=0)

    if NB:
        from gasp.sql.anls.prox import st_near
        from gasp.sql.mng.qw import exec_write_q

        nroads = st_near(
            osmLink,
            ("(SELECT gid, roads, bf_roads, geometry FROM {} "
             "WHERE roads IS NOT NULL)").format(lnhTbl),
            "gid",
            "geometry",
            ("(SELECT * FROM {} WHERE building IS NOT NULL)").format(polyTbl),
            "geometry",
            "near_roads",
            untilDist="12",
            near_col="dist_near")
        time_d = datetime.datetime.now().replace(microsecond=0)

        exec_write_q(osmLink, [(
            "UPDATE near_roads SET "
            "bf_roads = CAST(round(CAST(dist_near AS numeric), 0) AS integer) "
            "WHERE dist_near >= 1 AND dist_near <= 12"
        ), "CREATE INDEX near_dist_idx ON near_roads USING gist (geometry)"])
        time_e = datetime.datetime.now().replace(microsecond=0)

    else:
        nroads = ("(SELECT roads, bf_roads, geometry FROM {} "
                  "WHERE roads IS NOT NULL) AS foo").format(lnhTbl)

        time_d = None
        time_e = None

    # Execute Buffer
    bufferShp = st_buffer(osmLink,
                          nroads,
                          "bf_roads",
                          "geometry",
                          os.path.join(folder, "bf_roads.shp"),
                          cols_select="roads",
                          outTblIsFile=True,
                          dissolve=None)
    time_f = datetime.datetime.now().replace(microsecond=0)

    # Convert to Raster
    roadsRst = shp_to_raster(bufferShp,
                             None,
                             cellsize,
                             0,
                             os.path.join(folder, "rst_roads.tif"),
                             epsg=srs,
                             rst_template=rstT,
                             api='gdal')
    time_g = datetime.datetime.now().replace(microsecond=0)

    LULC_CLS = '1221' if nom != "GLOBE_LAND_30" else '801'

    return {
        int(LULC_CLS): roadsRst
    }, {
        0: ('count_rows_roads', time_b - time_a),
        1: ('count_rows_build', time_c - time_b),
        2: None if not time_d else ('near_analysis', time_d - time_c),
        3: None if not time_e else ('update_buffer_tbl', time_e - time_d),
        4: ('buffer_roads', time_f - time_e if time_e else time_f - time_c),
        5: ('roads_to_raster', time_g - time_f)
    }
Example #3
0
def roads_sqdb(osmcon, lnhTbl, plTbl, apidb='SQLITE', asRst=None):
    """
    Raods procedings using SQLITE
    """

    import datetime
    from gasp.sql.mng.tbl import row_num as cnt_rows
    if apidb == 'SQLITE':
        from gasp.sql.anls.prox import splite_buffer as st_buffer
        from gasp.to.shp.grs import sqlite_to_shp as db_to_shp
    else:
        from gasp.sql.anls.prox import st_buffer
        from gasp.to.shp.grs import psql_to_grs as db_to_shp

    time_a = datetime.datetime.now().replace(microsecond=0)
    NR = cnt_rows(osmcon,
                  lnhTbl,
                  where="roads IS NOT NULL",
                  api='psql' if apidb == 'POSTGIS' else 'sqlite')
    time_b = datetime.datetime.now().replace(microsecond=0)

    if not NR: return None, {0: ('count_rows_roads', time_b - time_a)}

    NB = cnt_rows(osmcon,
                  plTbl,
                  where="building IS NOT NULL",
                  api='psql' if apidb == 'POSTGIS' else 'sqlite')
    time_c = datetime.datetime.now().replace(microsecond=0)

    if NB:
        from gasp.sql.mng.qw import exec_write_q

        ROADS_Q = "(SELECT{} roads, bf_roads, geometry FROM {} WHERE roads IS NOT NULL)".format(
            "" if apidb == 'SQLITE' else " gid,", lnhTbl)
        if apidb == 'SQLITE':
            from gasp.sql.anls.prox import splite_near

            nroads = splite_near(osmcon,
                                 ROADS_Q,
                                 plTbl,
                                 "geometry",
                                 "geometry",
                                 "near_roads",
                                 whrNear="building IS NOT NULL")
            time_d = datetime.datetime.now().replace(microsecond=0)

            # Update buffer distance field
            exec_write_q(osmcon, [
                ("UPDATE near_roads SET bf_roads = CAST(round(dist_near, 0) AS integer) "
                 "WHERE dist_near >= 1 AND dist_near <= 12"),
                ("UPDATE near_roads SET bf_roads = 1 WHERE dist_near >= 0 AND "
                 "dist_near < 1")
            ],
                         api='sqlite')
            time_e = datetime.datetime.now().replace(microsecond=0)

        else:
            from gasp.sql.anls.prox import st_near

            nroads = st_near(
                osmcon,
                ROADS_Q,
                'gid',
                'geometry',
                "(SELECT * FROM {} WHERE building IS NOT NULL)".format(plTbl),
                "geometry",
                "near_roads",
                untilDist="12",
                near_col="dist_near")
            time_d = datetime.datetime.now().replace(microsecond=0)

            exec_write_q(osmcon, [
                ("UPDATE near_roads SET "
                 "bf_roads = CAST(round(CAST(dist_near AS numeric), 0) AS integer) "
                 "WHERE dist_near >= 1 AND dist_near <= 12"),
                ("UPDATE near_roads SET bf_roads = 1 WHERE dist_near >= 0 AND "
                 "dist_near < 1"),
                ("CREATE INDEX near_dist_idx ON near_roads USING gist (geometry)"
                 )
            ],
                         api='psql')
            time_e = datetime.datetime.now().replace(microsecond=0)

    else:
        nroads = ("(SELECT roads, bf_roads, geometry "
                  "FROM {} WHERE roads IS NOT NULL) AS foo").format(lnhTbl)

        time_d = None
        time_e = None

    # Execute Buffer
    bfTbl = st_buffer(osmcon,
                      nroads,
                      "bf_roads",
                      "geometry",
                      "bf_roads",
                      cols_select="roads",
                      outTblIsFile=None,
                      dissolve="ALL")
    time_f = datetime.datetime.now().replace(microsecond=0)

    # Send data to GRASS GIS
    roadsGrs = db_to_shp(osmcon,
                         bfTbl,
                         "froads",
                         notTable=None,
                         filterByReg=True)
    time_g = datetime.datetime.now().replace(microsecond=0)

    if asRst:
        from gasp.to.rst import shp_to_raster

        roadsGrs = shp_to_raster(roadsGrs,
                                 int(asRst),
                                 None,
                                 None,
                                 "rst_roads",
                                 api="grass")

        time_h = datetime.datetime.now().replace(microsecond=0)
    else:
        time_h = None

    return roadsGrs, {
        0: ('count_rows_roads', time_b - time_a),
        1: ('count_rows_build', time_c - time_b),
        2: None if not time_d else ('near_analysis', time_d - time_c),
        3: None if not time_e else ('update_buffer_tbl', time_e - time_d),
        4: ('buffer_roads', time_f - time_e if time_e else time_f - time_c),
        5: ('import_roads', time_g - time_f),
        6: None if not time_h else ('roads_to_raster', time_h - time_g)
    }
Example #4
0
def basic_buffer(osmLink, lineTable, dataFolder, apidb='SQLITE'):
    """
    Data from Lines table to Polygons using a basic buffering stratagie
    """

    import datetime
    from gasp.fm.sql import query_to_df
    if apidb == 'POSTGIS':
        from gasp.sql.anls.prox import st_buffer
    else:
        from gasp.sql.anls.prox import splite_buffer as st_buffer
    from gasp.to.rst import shp_to_raster
    from gasp.to.shp.grs import shp_to_grs

    time_a = datetime.datetime.now().replace(microsecond=0)
    lulcCls = query_to_df(
        osmLink,
        ("SELECT basic_buffer FROM {} WHERE basic_buffer IS NOT NULL "
         "GROUP BY basic_buffer").format(lineTable),
        db_api='psql'
        if apidb == 'POSTGIS' else 'sqlite').basic_buffer.tolist()
    time_b = datetime.datetime.now().replace(microsecond=0)

    timeGasto = {0: ('check_cls', time_b - time_a)}

    clsRst = {}
    tk = 1
    for cls in lulcCls:
        # Run BUFFER Tool
        time_x = datetime.datetime.now().replace(microsecond=0)
        bb_file = st_buffer(osmLink,
                            lineTable,
                            "bf_basic_buffer",
                            "geometry",
                            os.path.join(
                                dataFolder,
                                'bb_rule5_{}.shp'.format(str(int(cls)))),
                            whrClause="basic_buffer={}".format(str(int(cls))),
                            outTblIsFile=True,
                            dissolve="ALL",
                            cols_select="basic_buffer")
        time_y = datetime.datetime.now().replace(microsecond=0)

        # Data TO GRASS
        grsVect = shp_to_grs(bb_file,
                             "bb_{}".format(int(cls)),
                             asCMD=True,
                             filterByReg=True)
        time_z = datetime.datetime.now().replace(microsecond=0)

        # Data to Raster
        rstVect = shp_to_raster(grsVect,
                                int(cls),
                                None,
                                None,
                                "rbb_{}".format(int(cls)),
                                api="grass")
        time_w = datetime.datetime.now().replace(microsecond=0)

        clsRst[int(cls)] = rstVect

        timeGasto[tk] = ('do_buffer_{}'.format(cls), time_y - time_x)
        timeGasto[tk + 1] = ('import_{}'.format(cls), time_z - time_y)
        timeGasto[tk + 2] = ('torst_{}'.format(cls), time_w - time_z)

        tk += 3

    return clsRst, timeGasto