예제 #1
0
def matrix_od_mean_dist_by_group(MATRIX_OD, ORIGIN_COL, GROUP_ORIGIN_ID,
                                 GROUP_ORIGIN_NAME, GROUP_DESTINA_ID,
                                 GROUP_DESTINA_NAME, TIME_COL, epsg, db,
                                 RESULT_MATRIX):
    """
    Calculate Mean GROUP distance from OD Matrix
    
    OD MATRIX EXAMPLE
    | origin_entity | origin_group | destina_entity | destina_group | distance
    |     XXXX      |     XXXX     |      XXXX      |      XXX      |   XXX
    
    OUTPUT EXAMPLE
    | origin_group | destina_group | mean_distance
    |     XXXX     |      XXXX     |      XXXX
    """

    import os
    from gasp.pyt.oss import fprop
    from gasp.gql.to import shp_to_psql
    from gasp.sql.db import create_db
    from gasp.sql.to import q_to_ntbl
    from gasp.to import db_to_tbl

    db = create_db(fprop(MATRIX_OD, 'fn'), overwrite=True, api='psql')

    TABLE = shp_to_psql(db,
                        MATRIX_OD,
                        pgTable="tbl_{}".format(db),
                        api="pandas",
                        srsEpsgCode=epsg)

    OUT_TABLE = q_to_ntbl(
        db,
        fprop(RESULT_MATRIX, 'fn'),
        ("SELECT {groupOriginCod}, {groupOriginName}, {groupDestCod}, "
         "{groupDestName}, AVG(mean_time) AS mean_time FROM ("
         "SELECT {origin}, {groupOriginCod}, {groupOriginName}, "
         "{groupDestCod}, {groupDestName}, "
         "AVG({timeCol}) AS mean_time FROM {t} "
         "GROUP BY {origin}, {groupOriginCod}, {groupOriginName}, "
         "{groupDestCod}, {groupDestName}"
         ") AS foo "
         "GROUP BY {groupOriginCod}, {groupOriginName}, "
         "{groupDestCod}, {groupDestName} "
         "ORDER BY {groupOriginCod}, {groupDestCod}").format(
             groupOriginCod=GROUP_ORIGIN_ID,
             groupOriginName=GROUP_ORIGIN_NAME,
             groupDestCod=GROUP_DESTINA_ID,
             groupDestName=GROUP_DESTINA_NAME,
             origin=ORIGIN_COL,
             timeCol=TIME_COL,
             t=TABLE),
        api='psql')

    return db_to_tbl(db,
                     "SELECT * FROM {}".format(OUT_TABLE),
                     RESULT_MATRIX,
                     sheetsNames="matrix",
                     dbAPI='psql')
예제 #2
0
파일: brk.py 프로젝트: jasp382/gasp
def break_lines_on_points(lineShp, pntShp, outShp, lnhidonpnt,
                          api='shply', db=None):
    """
    Break lines on points location
    
    api's available:
    - shply (shapely);
    - psql (postgis);
    """
    
    if api == 'shply':
        result = shply_break_lines_on_points(
            lineShp, pntShp, lnhidonpnt, outShp)
    
    elif api == 'psql':
        from gasp.pyt.oss     import fprop
        from gasp.sql.db      import create_db
        from gasp.gql.to      import shp_to_psql
        from gasp.gt.toshp.db import dbtbl_to_shp
        from gasp.gql.brk     import split_lines_on_pnt
        
        # Create DB
        if not db:
            db = create_db(fprop(lineShp, 'fn', forceLower=True), api='psql')
        
        else:
            from gasp.sql.i import db_exists

            isDb = db_exists(db)
            
            if not isDb:
                db = create_db(db, api='psql')
        
        # Send Data to BD
        lnhTbl = shp_to_psql(db, lineShp, api="shp2pgsql")
        pntTbl = shp_to_psql(db,  pntShp, api="shp2pgsql")
        
        # Get result
        outTbl = split_lines_on_pnt(
            db, lnhTbl, pntTbl,
            fprop(outShp, 'fn', forceLower=True),
            lnhidonpnt, 'gid'
        )
        
        # Export result
        result = dbtbl_to_shp(
            db, outTbl, "geom", outShp, inDB='psql', tableIsQuery=None,
            api="pgsql2shp"
        )
    
    else:
        raise ValueError(
            "API {} is not available".format(api)
        )
    
    return result
예제 #3
0
def viewshed(demrst, obsShp, output):
    """
    This tool computes a visibility analysis using observer points from
    a point shapefile.
    """

    import os
    from gasp import exec_cmd
    from gasp.pyt.oss import fprop
    from gasp.gt.torst import saga_to_tif

    SAGA_RASTER = os.path.join(os.path.dirname(output),
                               "sg_{}.sgrd".format(fprop(output, 'fn')))

    cmd = ("saga_cmd ta_lighting 6 -ELEVATION {elv} -POINTS {pnt} "
           "-VISIBILITY {out} -METHOD 0").format(elv=demrst,
                                                 pnt=obsShp,
                                                 out=SAGA_RASTER)

    outcmd = exec_cmd(cmd)

    # Convert to Tiif
    saga_to_tif(SAGA_RASTER, output)

    return output
예제 #4
0
파일: dns.py 프로젝트: jasp382/gasp
def kernel_density(pnt_feat, popField, radius, template, outRst):
    """
    Kernel density estimation. If any point is currently 
    in selection only selected points are taken into account.
    """

    import os
    from gasp.gt.torst import saga_to_tif
    from gasp.gt.prop.rst import rst_ext, get_cellsize
    from gasp.pyt.oss import fprop

    left, right, bottom, top = rst_ext(template)
    cellsize = get_cellsize(template)

    SAGA_RASTER = os.path.join(os.path.dirname(outRst),
                               'saga_{}.sgrd'.format(fprop(outRst, 'fn')))

    cmd = ("saga_cmd grid_gridding 6 -POINTS {} -POPULATION {} "
           "-RADIUS {} -TARGET_DEFINITION 0 -TARGET_USER_SIZE {} "
           "-TARGET_USER_XMIN {} -TARGET_USER_XMAX {} "
           "-TARGET_USER_YMIN {} -TARGET_USER_YMAX {} "
           "-TARGET_OUT_GRID {}").format(pnt_feat, popField, str(radius),
                                         str(abs(cellsize)), str(left),
                                         str(right), str(bottom), str(top),
                                         SAGA_RASTER)

    outcmd = exec_cmd(cmd)

    # Convert to tiff
    saga_to_tif(SAGA_RASTER, outRst)

    return outRst
예제 #5
0
def mdl_to_kml(mdl, outKml, filter=None):
    """
    Query a database table and convert it to a KML File
    """

    import json
    import os
    from django.http import HttpResponse
    from gasp.pyt.oss import fprop
    from gasp.web.djg.mdl.serial import mdl_serialize_to_json
    from gasp.gt.toshp.cff import shp_to_shp

    # Write data in JSON
    JSON_FILE = os.path.join(os.path.dirname(outKml),
                             fprop(outKml, 'fn') + '.json')

    mdl_serialize_to_json(mdl, 'geojson', JSON_FILE, filterQ=filter)

    # Convert JSON into KML
    shp_to_shp(JSON_FILE, outKml, gisApi='ogr')

    # Create a valid DOWNLOAD RESPONSE
    with open(outKml, 'rb') as f:
        response = HttpResponse(f.read())

        response['content_type'] = 'text/xml'
        response['Content-Disposition'] = 'attachment;filename={}'.format(
            os.path.basename(outKml))

        return response
예제 #6
0
def foldershp_to_foldershp(inFld, outFld, destiny_file_format,
                           file_format='.shp', useApi='ogr'):
    """
    Execute shp_to_shp for every file in inFld (path to folder)
    
    useApi options:
    * ogr;
    """
    
    import os
    from gasp.pyt.oss import lst_ff, fprop
    
    if not os.path.exists(outFld):
        from gasp.pyt.oss import mkdir
        mkdir(outFld)
    
    geo_files = lst_ff(inFld, file_format=file_format)
    
    for f in geo_files:
        shp_to_shp(f, os.path.join(outFld, '{}.{}'.format(
            fprop(f, 'fn'), destiny_file_format if \
                destiny_file_format[0] == '.' else '.' + destiny_file_format
        )), gisApi=useApi)
    
    return outFld
예제 #7
0
파일: to.py 프로젝트: jasp382/gasp
def txts_to_db(folder, db, delimiter, __encoding='utf-8', apidb='psql',
               rewrite=None):
    """
    Executes tbl_to_db for every file in a given folder
    
    The file name will be the table name
    """
    
    from gasp.pyt.oss import lst_ff, fprop
    from gasp.sql.i   import db_exists
    
    if not db_exists(db):
        # Create database
        from gasp.sql.db import create_db
        
        create_db(db, api=apidb, overwrite=None)
    
    else:
        if rewrite:
            from gasp.sql.db import create_db
            create_db(db, api=db, overwrite=True)
    
    __files = lst_ff(folder, file_format=['.txt', '.csv', '.tsv'])
    
    """
    Send data to DB using Pandas
    """
    for __file in __files:
        tbl_to_db(
            __file, db, fprop(__file, 'fn'),
            delimiter=delimiter, encoding_=__encoding, api_db=apidb
        )
예제 #8
0
def split_shp_by_attr(inShp, attr, outDir, _format='.shp'):
    """
    Create a new shapefile for each value in a column
    """

    import os
    from gasp.gt.fmshp import shp_to_obj
    from gasp.pyt.oss import fprop
    from gasp.pyt.df.fld import col_distinct
    from gasp.gt.toshp import df_to_shp

    # Sanitize format
    FFF = _format if _format[0] == '.' else '.' + _format

    # SHP TO DF
    dataDf = shp_to_obj(inShp)

    # Get values in attr
    uniqueAttr = col_distinct(dataDf, attr)

    # Export Features with the same value in attr to a new File
    BASENAME = fprop(inShp, 'fn', forceLower=True)
    SHPS_RESULT = {}
    i = 1
    for val in uniqueAttr:
        df = dataDf[dataDf[attr] == val]

        newShp = df_to_shp(
            df, os.path.join(outDir, "{}_{}{}".format(BASENAME, str(i), FFF)))

        SHPS_RESULT[val] = newShp

        i += 1

    return SHPS_RESULT
예제 #9
0
파일: coord.py 프로젝트: jasp382/gasp
def pnts_to_boundary(pntShp, outBound, distMeters):
    """
    Create a boundary from Point using a tolerance in meters
    """
    
    from osgeo            import ogr
    from gasp.pyt.oss     import fprop
    from gasp.gt.prop.ff  import drv_name
    from gasp.g.to        import new_pnt
    from gasp.gt.prop.prj import get_shp_sref
    
    SRS = get_shp_sref(pntShp)
    
    shp = ogr.GetDriverByName(drv_name(pntShp)).Open(pntShp)
    lyr = shp.GetLayer()
    
    outShp = ogr.GetDriverByName(drv_name(outBound)).CreateDataSource(outBound)
    outLyr = outShp.CreateLayer(
        fprop(outBound, 'fn', forceLower=True), SRS,
        geom_type=ogr.wkbPolygon
    )
    
    outDefn = outLyr.GetLayerDefn()
    
    for feat in lyr:
        __feat = ogr.Feature(outDefn)
        ring = ogr.Geometry(ogr.wkbLinearRing)
        
        geom = feat.GetGeometryRef()
        X, Y = geom.GetX(), geom.GetY()
        
        boundary_points = [
            new_pnt(X - distMeters, Y + distMeters), # Topleft
            new_pnt(X + distMeters, Y + distMeters), # TopRight
            new_pnt(X + distMeters, Y - distMeters), # Lower Right
            new_pnt(X - distMeters, Y - distMeters), # Lower Left
            new_pnt(X - distMeters, Y + distMeters)
        ]
        
        for pnt in boundary_points:
            ring.AddPoint(pnt.GetX(), pnt.GetY())
        
        polygon = ogr.Geometry(ogr.wkbPolygon)
        polygon.AddGeometry(ring)
        
        __feat.SetGeometry(polygon)
        
        outLyr.CreateFeature(__feat)
        
        feat.Destroy()
        
        __feat  = None
        ring    = None
        polygon = None
    
    shp.Destroy()
    outShp.Destroy()
    
    return outBound
예제 #10
0
        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")
예제 #11
0
def thrd_lulc_by_cell(thrd_id, df_fishnet, l_lulc, result):
    # Create folder for this thread
    t_folder = mkdir(os.path.join(result, 'thrd_' + str(thrd_id)))

    # For each fishnet, do the job
    for idx, row in df_fishnet.iterrows():
        rf = mkdir(os.path.join(result, fprop(row.fishnet, 'fn')))

        lulc_by_cell(int(idx), row.bound, l_lulc, row.fishnet, rf, t_folder)
예제 #12
0
파일: torst.py 프로젝트: jasp382/gasp
def rsts_to_gpkg(in_rsts, gpkg, rst_ff='.tif', basename=None):
    """
    Raster Files to GeoPackage
    """

    import os
    import numpy as np
    from gasp import exec_cmd
    from gasp.pyt.oss import fprop
    from gasp.gt.prop.rst import rst_dtype

    if type(in_rsts) == list:
        rsts = in_rsts

    elif os.path.isdir(in_rsts):
        from gasp.pyt.oss import lst_ff

        rsts = lst_ff(in_rsts, file_format='.tif' if not rst_ff else rst_ff)

    else:
        rsts = [in_rsts]

    new_cmd = "gdal_translate -of GPKG {} {} -CO RASTER_TABLE={}{}"
    upd_cmd = ("gdal_translate -of GPKG {} {} -co APPEND_SUBDATASET=YES -CO "
               "RASTER_TABLE={}{}")

    for r in range(len(rsts)):
        rst_type = rst_dtype(rsts[r])

        tname = fprop(rsts[r], 'fn') if not basename else \
            "{}_{}".format(basename, fprop(rsts[r], 'fn').split('_')[-1])

        if not r and not os.path.exists(gpkg):
            rcmd = exec_cmd(
                new_cmd.format(
                    rsts[r], gpkg, tname,
                    " -ot Float32" if rst_type == np.float64 else ""))
        else:
            rcmd = exec_cmd(
                upd_cmd.format(
                    rsts[r], gpkg, tname,
                    " -ot Float32" if rst_type == np.float64 else ""))

    return gpkg
예제 #13
0
def sel_by_loc(shp, boundary_filter, filtered_output):
    """
    Filter a shp using the location of a boundary_filter shp
    
    For now the boundary must have only one feature
    
    Writes the filter on a new shp
    """

    import os
    from osgeo import ogr
    from gasp.gt.prop.ff import drv_name
    from gasp.gt.prop.feat import get_gtype
    from gasp.g.lyr.fld import copy_flds
    from gasp.gt.toshp import copy_feat
    from gasp.pyt.oss import fprop

    # Open main data
    dtSrc = ogr.GetDriverByName(drv_name(shp)).Open(shp, 0)
    lyr = dtSrc.GetLayer()

    # Get filter geom
    filter_shp = ogr.GetDriverByName(drv_name(boundary_filter)).Open(
        boundary_filter, 0)
    filter_lyr = filter_shp.GetLayer()

    c = 0
    for f in filter_lyr:
        if c:
            break
        geom = f.GetGeometryRef()
        c += 1

    filter_shp.Destroy()

    # Apply filter
    lyr.SetSpatialFilter(geom)

    # Copy filter objects to a new shape
    out = ogr.GetDriverByName(
        drv_name(filtered_output)).CreateDataSource(filtered_output)

    outLyr = out.CreateLayer(fprop(filtered_output, 'fn'),
                             geom_type=get_gtype(shp,
                                                 gisApi='ogr',
                                                 name=None,
                                                 py_cls=True))

    # Copy fields
    copy_flds(lyr, outLyr)

    copy_feat(lyr,
              outLyr,
              outDefn=outLyr.GetLayerDefn(),
              only_geom=False,
              gisApi='ogrlyr')
예제 #14
0
파일: osm.py 프로젝트: jasp382/gasp
def osm_to_featcls(xmlOsm,
                   output,
                   fileFormat='.shp',
                   useXmlName=None,
                   outepsg=4326):
    """
    OSM to ESRI Shapefile
    """

    import os
    from gasp.gt.attr import sel_by_attr
    from gasp.pyt.oss import fprop, del_file

    # Convert xml to sqliteDB
    gpkg = osm_to_gpkg(xmlOsm,
                       os.path.join(output,
                                    fprop(xmlOsm, 'fn') + '.gpkg'))

    # sqliteDB to Feature Class
    TABLES = {
        'points': 'pnt',
        'lines': 'lnh',
        'multilinestrings': 'mlnh',
        'multipolygons': 'poly'
    }

    for T in TABLES:
        sel_by_attr(
            gpkg,
            "SELECT * FROM {}".format(T),
            os.path.join(
                output, "{}{}{}".format(
                    "" if not useXmlName else fprop(xmlOsm, 'fn') + "_",
                    TABLES[T],
                    fileFormat if fileFormat[0] == '.' else "." + fileFormat)),
            api_gis='ogr',
            oEPSG=None if outepsg == 4326 else outepsg,
            iEPSG=4326)

    # Del temp DB
    del_file(gpkg)

    return output
예제 #15
0
def clip(inFeat, clipFeat, outFeat, api_gis="grass", clip_by_region=None):
    """
    Clip Analysis
    
    api_gis Options:
    * grass
    * pygrass
    * ogr2ogr
    """

    if api_gis == "pygrass":
        from grass.pygrass.modules import Module

        if not clip_by_region:
            vclip = Module("v.clip",
                           input=inFeat,
                           clip=clipFeat,
                           output=outFeat,
                           overwrite=True,
                           run_=False,
                           quiet=True)
        else:
            vclip = Module("v.clip",
                           input=inFeat,
                           output=outFeat,
                           overwrite=True,
                           flags='r',
                           run_=False,
                           quiet=True)

        vclip()

    elif api_gis == "grass":
        from gasp import exec_cmd

        rcmd = exec_cmd(
            "v.clip input={}{} output={} {}--overwrite --quiet".format(
                inFeat, " clip={}".format(clipFeat) if clipFeat else "",
                outFeat, "-r " if not clipFeat else ""))

    elif api_gis == 'ogr2ogr':
        from gasp import exec_cmd
        from gasp.pyt.oss import fprop
        from gasp.gt.prop.ff import drv_name

        rcmd = exec_cmd(
            ("ogr2ogr -f \"{}\" {} {} -clipsrc {} -clipsrclayer {}").format(
                drv_name(outFeat), outFeat, inFeat, clipFeat,
                fprop(clipFeat, 'fn')))

    else:
        raise ValueError("{} is not available!".format(api_gis))

    return outFeat
예제 #16
0
def check_isShp(_file):
    from gasp.pyt.oss import fprop

    lst = vector_formats()

    file_ext = fprop(_file, 'ff')

    if file_ext not in lst:
        return None
    else:
        return True
예제 #17
0
def check_isRaster(_file):
    from gasp.pyt.oss import fprop

    rst_lst = raster_formats()

    file_ext = fprop(_file, 'ff')

    if file_ext not in rst_lst:
        return None
    else:
        return True
예제 #18
0
def multi_run(ti, df, ofolder):
    loc_name = 'loc_{}'.format(str(ti))
    grsbase = run_grass(ofolder, location=loc_name, srs=srs_epsg)
    
    import grass.script.setup as gsetup
    gsetup.init(grsbase, ofolder, loc_name, 'PERMANENT')
    
    from gasp.gt.toshp.cff import shp_to_grs, grs_to_shp
    from gasp.gt.gop.ovlay import union
    
    for idx, row in df.iterrows():
        # Import data into GRASS GIS
        lyr_a = shp_to_grs(df.shp_a, fprop(df.shp_a, 'fn'), asCMD=True)
        lyr_b = shp_to_grs(df.shp_b, fprop(df.shp_b, 'fn'), asCMD=True)
        
        # Run Union
        shpUnion = union(lyr_a, lyr_b, lyr_a[:10] + '_' + lyr_b[:10], api_gis="grass")
        
        # Export data
        result = grs_to_shp(shpUnion, os.path.join(ofolder, shpUnion + '.shp'), "area")
예제 #19
0
def line_intersect_to_pnt(inShp, outShp, db=None):
    """
    Get Points where two line features of the same feature class
    intersects.
    """

    from gasp.pyt.oss import fprop
    from gasp.gt.toshp.db import dbtbl_to_shp
    from gasp.sql.db import create_db
    from gasp.gql.to import shp_to_psql
    from gasp.gql.ovly import line_intersection_pnt

    # Create DB if necessary
    if not db:
        db = create_db(fprop(inShp, 'fn', forceLower=True), api='psql')

    else:
        from gasp.sql.i import db_exists

        isDb = db_exists(db)

        if not isDb:
            create_db(db, api='psql')

    # Send data to DB
    inTbl = shp_to_psql(db, inShp, api="shp2pgsql")

    # Get result
    outTbl = line_intersection_pnt(db, inTbl,
                                   fprop(outShp, 'fn', forceLower=True))

    # Export data from DB
    outShp = dbtbl_to_shp(db,
                          outTbl,
                          "geom",
                          outShp,
                          inDB='psql',
                          tableIsQuery=None,
                          api="pgsql2shp")

    return outShp
예제 #20
0
파일: joins.py 프로젝트: jasp382/gasp
def field_sum_by_table_folder(folderOne, joinFieldOne, folderTwo, joinFieldTwo,
                              sum_field, outFolder):

    import os
    from gasp.pyt.oss import lst_ff, fprop

    tablesOne = lst_ff(folderOne, file_format=['.xls', '.xlsx'])
    tablesTwo = lst_ff(folderTwo, file_format=['.xls', '.xlsx'])

    for table in tablesOne:
        table_name = fprop(table, 'fn')

        for __table in tablesTwo:
            __table_name = fprop(__table, 'fn')

            if table_name == __table_name:
                field_sum_two_tables(
                    table, __table, joinFieldOne, joinFieldTwo, sum_field,
                    os.path.join(outFolder, os.path.basename(table)))

                break
예제 #21
0
def priority_rule(osmshps, priorities, gis_software, db=None):
    """
    Priority rule in Arcgis
    """

    import copy
    import os
    if gis_software != 'psql':
        from gasp.gt.gop.ovlay import erase
    else:
        from gasp.gql.ovly import pg_erase
    from gasp.pyt.oss import fprop

    osmNameRef = copy.deepcopy(osmshps)

    for e in range(len(priorities)):
        if e + 1 == len(priorities): break

        if priorities[e] not in osmshps:
            continue

        else:
            for i in range(e + 1, len(priorities)):
                if priorities[i] not in osmshps:
                    continue

                else:
                    if gis_software == 'arcpy':
                        tmpOut = os.path.join(
                            os.path.dirname(osmshps[priorities[i]]),
                            "{}_{}.shp".format(
                                fprop(osmNameRef[priorities[i]], 'fn'), e))

                    else:
                        tmpOut = "{}_{}".format(osmNameRef[priorities[i]], e)

                    if gis_software == 'psql':
                        osmshps[priorities[i]] = pg_erase(
                            db, osmshps[priorities[i]], osmshps[priorities[e]],
                            'geom', 'geom', tmpOut)

                    else:
                        osmshps[priorities[i]] = erase(osmshps[priorities[i]],
                                                       osmshps[priorities[e]],
                                                       tmpOut,
                                                       api=gis_software)

    return osmshps
예제 #22
0
파일: coord.py 프로젝트: jasp382/gasp
def shpext_to_boundshp(inShp, outShp, epsg=None):
    """
    Read one feature class extent and create a boundary with that
    extent
    
    The outFile could be a Feature Class or one Raster Dataset
    """
    
    import os
    from osgeo           import ogr
    from gasp.gt.prop.ff import drv_name
    from gasp.pyt.oss    import fprop
    from gasp.g.to       import new_pnt
    from gasp.g.to       import shpext_to_boundary
    
    # Get SRS for the output
    if not epsg:
        from gasp.gt.prop.prj import get_shp_sref
        
        srs = get_shp_sref(inShp)
    
    else:
        from gasp.gt.prop.prj import get_sref_from_epsg
        
        srs= get_sref_from_epsg(epsg)
    
    # Write new file
    shp = ogr.GetDriverByName(
        drv_name(outShp)).CreateDataSource(outShp)
    
    lyr = shp.CreateLayer(
        fprop(outShp, 'fn', forceLower=True),
        srs, geom_type=ogr.wkbPolygon
    )
    
    outDefn = lyr.GetLayerDefn()
    
    feat = ogr.Feature(outDefn)
    polygon = shpext_to_boundary(inShp)
    
    feat.SetGeometry(polygon)
    lyr.CreateFeature(feat)
    
    feat.Destroy()
    shp.Destroy()
    
    return outShp
예제 #23
0
파일: torst.py 프로젝트: jasp382/gasp
def saga_to_tif(inFile, outFile):
    """
    SAGA GIS format to GeoTIFF
    """

    from gasp import exec_cmd
    from gasp.pyt.oss import fprop

    # Check if outFile is a GeoTiff
    if fprop(outFile, 'ff') != '.tif':
        raise ValueError('Outfile should have GeoTiff format')

    cmd = ("saga_cmd io_gdal 2 -GRIDS {} " "-FILE {}").format(inFile, outFile)

    outcmd = exec_cmd(cmd)

    return outFile
예제 #24
0
def rstcalc(expression, output, api='saga', grids=None):
    """
    Basic Raster Calculator
    """

    if api == 'saga':
        # Using SAGA GIS

        import os
        from gasp import exec_cmd
        from gasp.pyt.oss import fprop
        from gasp.gt.torst import saga_to_tif

        SAGA_RASTER = os.path.join(os.path.dirname(output),
                                   "sag_{}.sgrd".format(fprop(output, 'fn')))

        cmd = ("saga_cmd grid_calculus 1 -FORMULA \"{}\" -GRIDS \"{}\" "
               "-RESULT {} -RESAMPLING 0").format(expression, ";".join(grids),
                                                  SAGA_RASTER)

        outcmd = exec_cmd(cmd)

        # Convert to tiff
        saga_to_tif(SAGA_RASTER, output)

    elif api == 'pygrass':
        from grass.pygrass.modules import Module

        rc = Module('r.mapcalc',
                    '{} = {}'.format(output, expression),
                    overwrite=True,
                    run_=False,
                    quiet=True)

        rc()

    elif api == 'grass':
        from gasp import exec_cmd

        rcmd = exec_cmd(("r.mapcalc \"{} = {}\" --overwrite --quiet").format(
            output, expression))

    else:
        raise ValueError("{} is not available!".format(api))

    return output
예제 #25
0
파일: gen.py 프로젝트: jasp382/gasp
def sheets_into_file(xlsFolder, outXls, intSheets):
    """
    For each xls file in one folder, pick one interest sheet
    and save all sheets in a single file
    """

    from gasp.pyt import obj_to_lst
    from gasp.pyt.oss import lst_ff, fprop
    from gasp.pyt.xls.sheet import copy_sheet_to_file

    xls_s = lst_ff(xlsFolder, file_format=['.xls', '.xlsx'])

    for xlsPath in xls_s:
        copy_sheet_to_file(xlsPath, outXls, intSheets,
                           {intSheets: fprop(xlsPath, 'fn', forceLower=True)})

    return outXls
예제 #26
0
def cols_name(ff, sheetName=None, sheetIdx=None):
    """
    Get Columns Name in File, regardeless the type of file
    """
    
    from gasp.pyt.oss import fprop
    
    fFormat = fprop(ff, 'ff')
    
    if fFormat == '.xlsx' or fFormat == '.xls':
        from gasp.pyt.xls.fld import col_name
        
        cols = col_name(ff, sheet_name=sheetName, sheet_index=sheetIdx)
    
    else:
        raise ValueError(
            'File format is not valid!'
        )
    
    return cols
예제 #27
0
파일: torst.py 프로젝트: jasp382/gasp
def grs_to_rst(grsRst, rst, as_cmd=None, allBands=None):
    """
    GRASS Raster to Raster
    """

    from gasp.gt.prop.ff import grs_rst_drv
    from gasp.pyt.oss import fprop

    rstDrv = grs_rst_drv()
    rstExt = fprop(rst, 'ff')

    if not as_cmd:
        from grass.pygrass.modules import Module

        m = Module(
            "r.out.gdal",
            input=grsRst,
            output=rst,
            format=rstDrv[rstExt],
            flags='c' if not allBands else '',
            createopt="INTERLEAVE=PIXEL,TFW=YES" if allBands else 'TFW=YES',
            overwrite=True,
            run_=False,
            quiet=True)

        m()

    else:
        from gasp import exec_cmd

        rcmd = exec_cmd((
            "r.out.gdal input={} output={} format={} "
            "{} -c --overwrite --quiet"
        ).format(
            grsRst, rst, rstDrv[rstExt],
            "createopt=\"TFW=YES\"" if not allBands else \
                "createopt=\"INTERLEAVE=PIXEL,TFW=YES\""
        ))

    return rst
예제 #28
0
파일: stores.py 프로젝트: jasp382/gasp
def shp_to_store(shape, store_name, workspace):
    """
    Create a new datastore
    """

    import os
    import requests
    from gasp.pyt.oss import lst_ff, fprop
    from gasp.cons.gsrv import con_gsrv

    conf = con_gsrv()

    url = ('{pro}://{host}:{port}/geoserver/rest/workspaces/{work}/datastores/'
           '{store}/file.shp').format(host=conf['HOST'],
                                      port=conf['PORT'],
                                      work=workspace,
                                      store=store_name,
                                      pro=conf['PROTOCOL'])

    shpp = fprop(shape, ['fn', 'ff'])
    fn, ff = shpp['filename'], shpp['fileformat']

    if ff != '.zip':
        from gasp.pyt.ff.zzip import zip_files

        shp_fld = os.path.dirname(shape)

        shapefiles = lst_ff(shp_fld, filename=fn)

        shape = os.path.join(shp_fld, fn + '.zip')
        zip_files(shapefiles, shape)

    with open(shape, 'rb') as f:
        r = requests.put(url,
                         data=f,
                         headers={'content-type': 'application/zip'},
                         auth=(conf['USER'], conf['PASSWORD']))

        return r
예제 #29
0
파일: prj.py 프로젝트: jasp382/gasp
def get_epsg_shp(shp, returnIsProj=None):
    """
    Return EPSG code of the Spatial Reference System of a Feature Class
    """

    from gasp.pyt.oss import fprop

    if fprop(shp, 'ff') != '.gml':
        proj = get_shp_sref(shp)
    else:
        epsg = get_gml_epsg(shp)

        if not epsg:
            raise ValueError(
                '{} file has not Spatial Reference assigned!'.format(shp))

        proj = get_sref_from_epsg(int(epsg))

    if not proj:
        raise ValueError(
            '{} file has not Spatial Reference assigned!'.format(shp))

    epsg = int(str(proj.GetAttrValue('AUTHORITY', 1)))

    if not returnIsProj:
        return epsg
    else:
        if proj.IsProjected:
            mod_proj = proj.GetAttrValue(str('projcs'))

            if not mod_proj:
                return epsg, None
            else:
                return epsg, True

        else:
            return epsg, None
예제 #30
0
def shps_to_gpkg(in_shps, gpkg, shp_ff='.shp', tbl_name=None):
    """
    Add Shapefile to GeoPackage File
    """

    import os
    from gasp         import exec_cmd
    from gasp.pyt.oss import fprop

    if type(in_shps) == list:
        shps = in_shps
    
    elif os.path.isdir(in_shps):
        from gasp.pyt.oss import lst_ff

        # List Feature Classes
        shps = lst_ff(in_shps, file_format='.shp' if not shp_ff else shp_ff)
    
    else:
        # Assuming in_shps as a file
        shps = [in_shps]
    
    new_cmd = "ogr2ogr -f \"GPKG\" {} -nln \"{}\" {}"
    upd_cmd = "ogr2ogr -update -append -f \"GPKG\" {} -nln \"{}\" {}"

    for s in range(len(shps)):
        if tbl_name and not s:
            tname = tbl_name
        else:
            tname = fprop(shps[s], 'fn')
        
        if not s and not os.path.exists(gpkg):
            rcmd = exec_cmd(new_cmd.format(gpkg, tname, shps[s]))
        else:
            rcmd = exec_cmd(upd_cmd.format(gpkg, tname, shps[s]))

    return gpkg