Пример #1
0
def grs_vect_selbyarea(osmdb, polyTbl, UPPER=True, apidb='SQLITE'):
    """
    Select features with area upper than.
    
    A field with threshold is needed in the database.
    """

    import datetime
    from glass.g.gp.gen import dissolve
    from glass.g.tbl.grs import add_table
    from glass.ete.osm2lulc import GEOM_AREA
    from glass.ng.prop.sql import row_num as cnt_row
    from glass.g.it.shp import dbtbl_to_shp as db_to_shp

    OPERATOR = ">" if UPPER else "<"
    DIRECTION = "upper" if UPPER else "lower"

    WHR = "{ga} {op} t_area_{r} and area_{r} IS NOT NULL".format(op=OPERATOR,
                                                                 r=DIRECTION,
                                                                 ga=GEOM_AREA)

    # Check if we have interest data
    time_a = datetime.datetime.now().replace(microsecond=0)
    N = cnt_row(osmdb,
                polyTbl,
                where=WHR,
                api='psql' if apidb == 'POSTGIS' else 'sqlite')
    time_b = datetime.datetime.now().replace(microsecond=0)

    if not N: return None, {0: ('count_rows', time_b - time_a)}

    # Data to GRASS GIS
    grsVect = db_to_shp(osmdb,
                        polyTbl,
                        "geometry",
                        "area_{}".format(DIRECTION),
                        where=WHR,
                        inDB='psql' if apidb == 'POSTGIS' else 'sqlite',
                        filterByReg=True,
                        outShpIsGRASS=True)
    time_c = datetime.datetime.now().replace(microsecond=0)

    dissVect = dissolve(grsVect,
                        "diss_area_{}".format(DIRECTION),
                        "area_{}".format(DIRECTION),
                        api="grass")

    add_table(dissVect, None, lyrN=1, asCMD=True)
    time_d = datetime.datetime.now().replace(microsecond=0)

    return dissVect, {
        0: ('count_rows', time_b - time_a),
        1: ('import', time_c - time_b),
        2: ('dissolve', time_d - time_c)
    }
Пример #2
0
def grs_vect_bbuffer(osmdata, lineTbl, api_db='SQLITE'):
    """
    Basic Buffer strategie
    """

    import datetime
    from glass.g.gp.prox.bfing import _buffer
    from glass.g.gp.gen import dissolve
    from glass.g.tbl.grs import add_table
    from glass.ng.prop.sql import row_num as cnt_row
    from glass.g.it.shp import dbtbl_to_shp as db_to_shp

    WHR = "basic_buffer IS NOT NULL"

    # Check if we have data
    time_a = datetime.datetime.now().replace(microsecond=0)
    N = cnt_row(osmdata,
                lineTbl,
                where=WHR,
                api='psql' if api_db == 'POSTGIS' else 'sqlite')
    time_b = datetime.datetime.now().replace(microsecond=0)

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

    grsVect = db_to_shp(osmdata,
                        lineTbl,
                        "geometry",
                        "bb_lnh",
                        where=WHR,
                        filterByReg=True,
                        inDB='psql' if api_db == 'POSTGIS' else 'sqlite',
                        outShpIsGRASS=True)
    time_c = datetime.datetime.now().replace(microsecond=0)

    grsBuf = _buffer(grsVect,
                     "bf_basic_buffer",
                     "bb_poly",
                     api="grass",
                     geom_type="line")
    time_d = datetime.datetime.now().replace(microsecond=0)

    grsDiss = dissolve(grsBuf, "bb_diss", "basic_buffer", api="grass")
    add_table(grsDiss, None, lyrN=1, asCMD=True)
    time_e = datetime.datetime.now().replace(microsecond=0)

    return grsDiss, {
        0: ('count_rows', time_b - time_a),
        1: ('import', time_c - time_b),
        2: ('buffer', time_d - time_c),
        3: ('dissolve', time_e - time_d)
    }
Пример #3
0
def grs_vector(db, polyTable, apidb='SQLITE'):
    """
    Simple Selection using GRASS GIS
    """

    import datetime
    from glass.g.gp.gen import dissolve
    from glass.g.tbl.grs import add_table
    from glass.ng.prop.sql import row_num as cont_row
    from glass.g.it.shp import dbtbl_to_shp as db_to_grs

    WHR = "selection IS NOT NULL"

    # Check if we have interest data
    time_a = datetime.datetime.now().replace(microsecond=0)
    N = cont_row(db,
                 polyTable,
                 where=WHR,
                 api='psql' if apidb == 'POSTGIS' else 'sqlite')
    time_b = datetime.datetime.now().replace(microsecond=0)

    if not N: return None, {0: ('count_rows', time_b - time_a)}

    # Data to GRASS GIS
    grsVect = db_to_grs(db,
                        polyTable,
                        "geometry",
                        "sel_rule",
                        where=WHR,
                        filterByReg=True,
                        inDB='psql' if apidb == 'POSTGIS' else 'sqlite',
                        outShpIsGRASS=True)
    time_c = datetime.datetime.now().replace(microsecond=0)

    dissVect = dissolve(grsVect, "diss_sel_rule", "selection", api="grass")

    add_table(dissVect, None, lyrN=1, asCMD=True)
    time_d = datetime.datetime.now().replace(microsecond=0)

    return dissVect, {
        0: ('count_rows', time_b - time_a),
        1: ('import', time_c - time_b),
        2: ('dissolve', time_d - time_c)
    }
Пример #4
0
def grs_vec_roads(osmdb, lineTbl, polyTbl):
    """
    Select Roads for GRASS GIS
    """

    import datetime
    from glass.ng.prop.sql import row_num
    from glass.g.it.shp import dbtbl_to_shp
    from glass.g.gp.prox.bfing import _buffer
    from glass.g.gp.gen import dissolve
    from glass.g.tbl.grs import add_table

    # Roads to GRASS GIS
    time_a = datetime.datetime.now().replace(microsecond=0)
    NR = row_num(osmdb, lineTbl, where="roads IS NOT NULL", api='sqlite')
    time_b = datetime.datetime.now().replace(microsecond=0)

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

    roadsVect = dbtbl_to_shp(osmdb,
                             lineTbl,
                             "geometry",
                             "all_roads",
                             where="roads IS NOT NULL",
                             inDB='sqlite',
                             outShpIsGRASS=True)
    time_c = datetime.datetime.now().replace(microsecond=0)

    # Buildings to GRASS GIS
    NB = row_num(osmdb, polyTbl, where="building IS NOT NULL", api='sqlite')
    time_d = datetime.datetime.now().replace(microsecond=0)

    if NB:
        from glass.g.gp.prox import grs_near as near
        from glass.g.tbl.grs import update_table

        builds = dbtbl_to_shp(osmdb,
                              polyTbl,
                              "geometry",
                              "all_builds",
                              where="building IS NOT NULL",
                              filterByReg=True,
                              inDB='sqlite',
                              outShpIsGRASS=True)
        time_e = datetime.datetime.now().replace(microsecond=0)

        near(roadsVect, builds, nearDistCol="todist", maxDist=12, as_cmd=True)
        time_f = datetime.datetime.now().replace(microsecond=0)
        update_table(roadsVect,
                     "bf_roads",
                     "round(todist,0)",
                     "\"todist > 0\"",
                     lyrN=1,
                     ascmd=True)
        time_g = datetime.datetime.now().replace(microsecond=0)

    else:
        time_e = None
        time_f = None
        time_g = None

    # Run Buffer tool
    roadsBf = _buffer(roadsVect,
                      "bf_roads",
                      "bf_roads",
                      api='grass',
                      geom_type="line")
    time_h = datetime.datetime.now().replace(microsecond=0)

    # Dissolve Roads
    roadsDiss = dissolve(roadsBf, "diss_roads", "roads", api="grass")

    add_table(roadsDiss, None, lyrN=1, asCMD=True)
    time_i = datetime.datetime.now().replace(microsecond=0)

    return roadsDiss, {
        0: ('count_rows_roads', time_b - time_a),
        1: ('import_roads', time_c - time_b),
        2: ('count_rows_build', time_d - time_c),
        3: None if not time_e else ('import_builds', time_e - time_d),
        4: None if not time_f else ('near_analysis', time_f - time_e),
        5: None if not time_g else ('update_buffer_tbl', time_g - time_f),
        6: ('buffer_roads', time_h - time_g if time_g else time_h - time_d),
        7: ('diss_roads', time_i - time_h)
    }
Пример #5
0
def distance_between_catpoints(srcShp, facilitiesShp, networkShp,
                               speedLimitCol, onewayCol, grsWorkspace,
                               grsLocation, outputShp):
    """
    Path bet points
    
    TODO: Work with files with cat
    """

    import os
    from glass.pys.oss import fprop
    from glass.g.wenv.grs import run_grass
    from glass.g.dp.mge import shps_to_shp
    from glass.g.prop.feat import feat_count

    # Merge Source points and Facilities into the same Feature Class
    SRC_NFEAT = feat_count(srcShp, gisApi='pandas')
    FACILITY_NFEAT = feat_count(facilitiesShp, gisApi='pandas')

    POINTS = shps_to_shp([srcShp, facilitiesShp],
                         os.path.join(os.path.dirname(outputShp),
                                      "points_net.shp"),
                         api='pandas')

    # Open an GRASS GIS Session
    gbase = run_grass(grsWorkspace,
                      grassBIN="grass76",
                      location=grsLocation,
                      srs=networkShp)

    import grass.script as grass
    import grass.script.setup as gsetup
    gsetup.init(gbase, grsWorkspace, grsLocation, 'PERMANENT')

    # Import GRASS GIS Module
    from glass.g.it.shp import shp_to_grs, grs_to_shp
    from glass.g.tbl.attr import geomattr_to_db
    from glass.g.cp import copy_insame_vector
    from glass.g.tbl import category
    from glass.g.tbl.grs import add_table, update_table
    from glass.g.mob.grstbx.vnet import network_from_arcs
    from glass.g.mob.grstbx.vnet import add_pnts_to_network
    from glass.g.mob.grstbx.vnet import netpath

    # Add Data to GRASS GIS
    rdvMain = shp_to_grs(networkShp, fprop(networkShp, 'fn', forceLower=True))
    pntShp = shp_to_grs(POINTS, "points_net")
    """Get closest facility layer:"""
    # Connect Points to Network
    newNetwork = add_pnts_to_network(rdvMain, pntShp, "rdv_points")

    # Sanitize Network Table and Cost Columns
    newNetwork = category(newNetwork,
                          "rdv_points_time",
                          "add",
                          LyrN="3",
                          geomType="line")

    add_table(newNetwork,
              ("cat integer,kph double precision,length double precision,"
               "ft_minutes double precision,"
               "tf_minutes double precision,oneway text"),
              lyrN=3)

    copy_insame_vector(newNetwork, "kph", speedLimitCol, 3, geomType="line")
    copy_insame_vector(newNetwork, "oneway", onewayCol, 3, geomType="line")

    geomattr_to_db(newNetwork,
                   "length",
                   "length",
                   "line",
                   createCol=False,
                   unit="meters",
                   lyrN=3)

    update_table(newNetwork, "kph", "3.6", "kph IS NULL", lyrN=3)
    update_table(newNetwork,
                 "ft_minutes",
                 "(length * 60) / (kph * 1000.0)",
                 "ft_minutes IS NULL",
                 lyrN=3)
    update_table(newNetwork,
                 "tf_minutes",
                 "(length * 60) / (kph * 1000.0)",
                 "tf_minutes IS NULL",
                 lyrN=3)

    # Exagerate Oneway's
    update_table(newNetwork, "ft_minutes", "1000", "oneway = 'TF'", lyrN=3)
    update_table(newNetwork, "tf_minutes", "1000", "oneway = 'FT'", lyrN=3)

    # Produce result
    result = netpath(newNetwork,
                     "",
                     "ft_minutes",
                     "tf_minutes",
                     fprop(outputShp, 'fn'),
                     arcLyr=3,
                     nodeLyr=2)

    return grs_to_shp(result, outputShp, geomType="line", lyrN=3)
Пример #6
0
def prod_matrix(origins,
                destinations,
                networkGrs,
                speedLimitCol,
                onewayCol,
                thrdId="1",
                asCmd=None):
    """
    Get Matrix Distance:
    """

    from glass.g.tbl import category
    from glass.g.tbl.filter import sel_by_attr
    from glass.g.tbl.col import add_fields
    from glass.g.tbl.grs import add_table, update_table
    from glass.g.mob.grstbx.vnet import add_pnts_to_network
    from glass.g.mob.grstbx.vnet import run_allpairs
    from glass.g.cp import copy_insame_vector
    from glass.g.tbl.attr import geomattr_to_db
    from glass.g.dp.mge import shps_to_shp
    from glass.g.prop.feat import feat_count
    from glass.g.it.shp import shp_to_grs

    # Merge Origins and Destinations into the same Feature Class
    ORIGINS_NFEAT = feat_count(origins, gisApi='pandas')
    DESTINATIONS_NFEAT = feat_count(destinations, gisApi='pandas')

    ORIGINS_DESTINATIONS = shps_to_shp([origins, destinations],
                                       os.path.join(
                                           os.path.dirname(origins),
                                           "points_od_{}.shp".format(thrdId)),
                                       api='pandas')

    pointsGrs = shp_to_grs(ORIGINS_DESTINATIONS,
                           "points_od_{}".format(thrdId),
                           asCMD=asCmd)

    # Connect Points to Network
    newNetwork = add_pnts_to_network(networkGrs,
                                     pointsGrs,
                                     "rdv_points_{}".format(thrdId),
                                     asCMD=asCmd)

    # Sanitize Network Table and Cost Columns
    newNetwork = category(newNetwork,
                          "rdv_points_time_{}".format(thrdId),
                          "add",
                          LyrN="3",
                          geomType="line",
                          asCMD=asCmd)

    add_table(newNetwork,
              ("cat integer,kph double precision,length double precision,"
               "ft_minutes double precision,"
               "tf_minutes double precision,oneway text"),
              lyrN=3,
              asCMD=asCmd)

    copy_insame_vector(newNetwork,
                       "kph",
                       speedLimitCol,
                       3,
                       geomType="line",
                       asCMD=asCmd)
    copy_insame_vector(newNetwork,
                       "oneway",
                       onewayCol,
                       3,
                       geomType="line",
                       asCMD=asCmd)

    geomattr_to_db(newNetwork,
                   "length",
                   "length",
                   "line",
                   createCol=False,
                   unit="meters",
                   lyrN=3,
                   ascmd=asCmd)

    update_table(newNetwork, "kph", "3.6", "kph IS NULL", lyrN=3, ascmd=asCmd)
    update_table(newNetwork, "kph", "3.6", "oneway = 'N'", lyrN=3, ascmd=asCmd)
    update_table(newNetwork,
                 "ft_minutes",
                 "(length * 60) / (kph * 1000.0)",
                 "ft_minutes IS NULL",
                 lyrN=3,
                 ascmd=asCmd)
    update_table(newNetwork,
                 "tf_minutes",
                 "(length * 60) / (kph * 1000.0)",
                 "tf_minutes IS NULL",
                 lyrN=3,
                 ascmd=asCmd)

    # Exagerate Oneway's
    update_table(newNetwork,
                 "ft_minutes",
                 "1000",
                 "oneway = 'TF'",
                 lyrN=3,
                 ascmd=asCmd)
    update_table(newNetwork,
                 "tf_minutes",
                 "1000",
                 "oneway = 'FT'",
                 lyrN=3,
                 ascmd=asCmd)

    # Produce matrix
    matrix = run_allpairs(newNetwork,
                          "ft_minutes",
                          "tf_minutes",
                          'result_{}'.format(thrdId),
                          arcLyr=3,
                          nodeLyr=2,
                          asCMD=asCmd)

    # Exclude unwanted OD Pairs
    q = "({}) AND ({})".format(
        " OR ".join(
            ["from_cat={}".format(str(i + 1)) for i in range(ORIGINS_NFEAT)]),
        " OR ".join([
            "to_cat={}".format(str(ORIGINS_NFEAT + i + 1))
            for i in range(DESTINATIONS_NFEAT)
        ]))

    matrix_sel = sel_by_attr(matrix,
                             q,
                             "sel_{}".format(matrix),
                             geomType="line",
                             lyrN=3,
                             asCMD=asCmd)

    add_fields(matrix_sel, "from_fid", "INTEGER", lyrN=3, asCMD=asCmd)
    add_fields(matrix_sel, "to_fid", "INTEGER", lyrN=3, asCMD=asCmd)

    update_table(matrix_sel,
                 "from_fid",
                 "from_cat - 1",
                 "from_fid IS NULL",
                 lyrN=3,
                 ascmd=asCmd)
    update_table(matrix_sel,
                 "to_fid",
                 "to_cat - {} - 1".format(str(ORIGINS_NFEAT)),
                 "to_fid IS NULL",
                 lyrN=3,
                 ascmd=asCmd)

    return matrix_sel