How to use the geopandas.read_postgis function in geopandas

To help you get started, we’ve selected a few geopandas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github geopandas / geopandas / tests / test_io.py View on Github external
def test_read_postgis_custom_geom_col(self):
        con = tests.util.connect('test_geopandas')
        if con is None or not tests.util.create_db(self.df):
            raise unittest.case.SkipTest()

        try:
            sql = """SELECT
                     borocode, boroname, shape_leng, shape_area,
                     geom AS __geometry__
                     FROM nybb;"""
            df = read_postgis(sql, con, geom_col='__geometry__')
        finally:
            if PANDAS_NEW_SQL_API:
                # It's not really a connection, it's an engine
                con = con.connect()
            con.close()

        tests.util.validate_boro_df(self, df)
github geopandas / geopandas / tests / test_io.py View on Github external
def test_read_postgis_default(self):
        con = tests.util.connect('test_geopandas')
        if con is None or not tests.util.create_db(self.df):
            raise unittest.case.SkipTest()

        try:
            sql = "SELECT * FROM nybb;"
            df = read_postgis(sql, con)
        finally:
            if PANDAS_NEW_SQL_API:
                # It's not really a connection, it's an engine
                con = con.connect()
            con.close()

        tests.util.validate_boro_df(self, df)
github akrherz / iem / scripts / GIS / 24h_lsr.py View on Github external
def main():
    """Go Main Go"""
    pgconn = get_dbconn("postgis", user="nobody")

    os.chdir("/tmp/")

    # We set one minute into the future, so to get expiring warnings
    # out of the shapefile
    ets = datetime.datetime.utcnow() + datetime.timedelta(minutes=+1)

    df = read_postgis(
        """
        SELECT distinct geom,
        to_char(valid at time zone 'UTC', 'YYYYMMDDHH24MI') as VALID,
        coalesce(magnitude, 0)::float as MAG,
        wfo as WFO,
        type as TYPECODE,
        typetext as TYPETEXT,
        city as CITY,
        county as COUNTY,
        state as STATE,
        source as SOURCE,
        substr(coalesce(remark, ''), 1, 200) as REMARK,
        ST_x(geom) as LON,
        ST_y(geom) as LAT
        from lsrs WHERE valid > (now() -'1 day'::interval)
    """,
github akrherz / iem / htdocs / plotting / auto / scripts200 / p203.py View on Github external
"fnadd": "-wfo",
            "sortby": "wfo ASC, phenomena ASC, eventid ASC",
        },
        "S": {"fnadd": "", "sortby": "size DESC"},
        "T": {"fnadd": "-time", "sortby": "issue ASC"},
    }
    phenoms = {"W": ["TO", "SV"], "F": ["FF"], "M": ["MA"]}

    # Defaults
    thumbpx = 100
    cols = 10
    mybuffer = 10000
    header = 35

    # Find largest polygon either in height or width
    gdf = read_postgis(
        """
        SELECT wfo, phenomena, eventid, issue,
        ST_area2d(ST_transform(geom,2163)) as size,
        (ST_xmax(ST_transform(geom,2163)) +
         ST_xmin(ST_transform(geom,2163))) /2.0 as xc,
        (ST_ymax(ST_transform(geom,2163)) +
         ST_ymin(ST_transform(geom,2163))) /2.0 as yc,
        ST_transform(geom, 2163) as utmgeom,
        (ST_xmax(ST_transform(geom,2163)) -
         ST_xmin(ST_transform(geom,2163))) as width,
        (ST_ymax(ST_transform(geom,2163)) -
         ST_ymin(ST_transform(geom,2163))) as height
        from sbw_"""
        + str(sts.year)
        + """
        WHERE status = 'NEW' and issue >= %s and issue < %s and
github mthh / gpd_lite_toolbox / gpd_lite_toolbox / core.py View on Github external
raise ValueError('Column names have to be specified')

    if not conn and db_path:
        conn = db_connect(db_path)
    elif not conn:
        raise ValueError(
            'A connection object or a path to the DB have to be provided')

    if sql.lower().find('select') == 0 and sql.find(' ') == 6:
        sql = sql[:7] \
            + "HEX(ST_AsBinary({0})) as {0}, ".format(geom_col) + sql[7:]
    else:
        raise ValueError(
            'Unable to understand the query')

    return read_postgis(
        sql, conn, geom_col=geom_col, crs=crs, index_col=index_col,
        coerce_float=coerce_float, params=params
        )
github akrherz / iem / htdocs / plotting / auto / scripts200 / p207.py View on Github external
def load_data(ctx, basets, endts):
    """Generate a dataframe with the data we want to analyze."""
    pgconn = get_dbconn("postgis")
    df = read_postgis(
        """SELECT state, wfo,
        max(magnitude::real) as val, ST_x(geom) as lon, ST_y(geom) as lat,
        ST_Transform(geom, 2163) as geo
        from lsrs WHERE type in ('S') and magnitude >= 0 and
        valid >= %s and valid <= %s
        GROUP by state, wfo, lon, lat, geo
        ORDER by val DESC
        """,
        pgconn,
        params=(basets, endts),
        index_col=None,
        geom_col="geo",
    )
    df[USEME] = False
    df["nwsli"] = df.index.values
    df["plotme"] = False
github akrherz / iem / scripts / sbw / polygon_mosaic.py View on Github external
totalSvrCar = 100.0 * (1.0 - (svrSize / float(row['size'])))

    # Make mosaic image
    header = 35
    mosaic = Image.new('RGB',
                       (thumbpx*cols, ((int(i/cols)+1)*thumbpx) + header))
    draw = ImageDraw.Draw(mosaic)

    imagemap = open('imap.txt', 'w')
    utcnow = datetime.datetime.utcnow()
    imagemap.write("\n" % (utcnow.strftime("%Y-%m-%d %H:%M:%S"),
                                         sortOpt))
    imagemap.write("<map name="mymap">\n")

    # Find my polygons
    gdf = read_postgis("""
        SELECT *, ST_area2d(ST_transform(geom,2163)) as size,
        (ST_xmax(ST_transform(geom,2163)) +
         ST_xmin(ST_transform(geom,2163))) /2.0 as xc,
        (ST_ymax(ST_transform(geom,2163)) +
         ST_ymin(ST_transform(geom,2163))) /2.0 as yc,
         ST_transform(geom, 2163) as utmgeom
        from sbw_""" + str(sts.year) + """ WHERE
        status = 'NEW' and issue &gt;= %s and issue &lt; %s and
        phenomena IN ('TO','SV') and eventid is not null
        ORDER by """ + opts[sortOpt]['sortby'] + """
    """, pgconn, params=(sts, ets), geom_col='utmgeom', index_col=None)

    # Write metadata to image
    tmp = Image.open("logo_small.png")
    mosaic.paste(tmp, (3, 2))
    s = "IEM Summary of NWS Storm Based Warnings issued %s UTC" % (</map>
github akrherz / iem / htdocs / plotting / auto / scripts200 / p207.py View on Github external
geom_col="geo",
    )
    df[USEME] = False
    df["nwsli"] = df.index.values
    df["plotme"] = False
    df["source"] = "LSR"
    if ctx["coop"] == "no":
        return df
    # More work to do
    pgconn = get_dbconn("iem")
    days = []
    now = basets
    while now &lt;= endts:
        days.append(now.date())
        now += datetime.timedelta(hours=24)
    df2 = read_postgis(
        """SELECT state, wfo, id as nwsli,
        sum(snow) as val, ST_x(geom) as lon, ST_y(geom) as lat,
        ST_Transform(geom, 2163) as geo
        from summary s JOIN stations t on (s.iemid = t.iemid)
        WHERE s.day in %s and t.network ~* 'COOP' and snow &gt;= 0 and
        coop_valid &gt;= %s and coop_valid &lt;= %s
        GROUP by state, wfo, nwsli, lon, lat, geo
        ORDER by val DESC
        """,
        pgconn,
        params=(tuple(days), basets, endts),
        index_col=None,
        geom_col="geo",
    )
    df2[USEME] = False
    df2["plotme"] = False
github akrherz / iem / htdocs / plotting / auto / scripts / p90.py View on Github external
(east, north) = [x + 2 for x in (east, north)]
    # create grids
    griddelta = 0.01
    lons = np.arange(west, east, griddelta)
    lats = np.arange(south, north, griddelta)
    YSZ = len(lats)
    XSZ = len(lons)
    lons, lats = np.meshgrid(lons, lats)
    affine = Affine(griddelta, 0.0, west, 0.0, 0 - griddelta, north)
    ones = np.ones((int(YSZ), int(XSZ)))
    counts = np.zeros((int(YSZ), int(XSZ)))
    wfolimiter = ""
    if ctx["t"] == "cwa":
        wfolimiter = " wfo = '%s' and " % (station,)
    # do arbitrary buffer to prevent segfaults?
    df = read_postgis(
        f"""
    SELECT ST_Forcerhr(ST_Buffer(geom, 0.0005)) as geom, issue, expire,
    extract(epoch from %s::timestamptz - issue) / 86400. as days
    from sbw where {wfolimiter} {daylimiter}
    phenomena = %s and status = 'NEW' and significance = %s
    and ST_Within(geom, ST_GeomFromEWKT('SRID=4326;POLYGON((%s %s, %s %s,
    %s %s, %s %s, %s %s))')) and ST_IsValid(geom)
    and issue &gt;= %s and issue &lt;= %s ORDER by issue ASC
    """,
        pgconn,
        params=(
            ets,
            phenomena,
            significance,
            west,
            south,