예제 #1
0
    def test_wkt(self):
        """ Try the properties function"""
        wkt = "SRID=4326;POINT(-99 43)"
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertEquals(Point(geom), Point([-99, 43]))

        wkt = """MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),
            ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),
            (30 20, 20 15, 20 25, 30 20)))"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(Polygon(geom[0]).area, 87.5, 1)

        wkt = """MULTILINESTRING ((10 10, 20 20, 10 40),
        (40 40, 30 30, 40 20, 30 10))"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(LineString(geom[0]).length, 36.5, 1)

        wkt = """LINESTRING (30 10, 10 30, 40 40)"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(LineString(geom).length, 59.9, 1)

        wkt = """POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(Polygon(geom[0]).area, 550., 1)

        wkt = """POLYGON q((30 10, 40 40, 20 40, 10 20, 30 10))q"""
        self.assertRaises(ValueError, wellknowntext.convert_well_known_text,
                          wkt)

        wkt = """RARRR q((30 10, 40 40, 20 40, 10 20, 30 10))q"""
        self.assertRaises(ValueError, wellknowntext.convert_well_known_text,
                          wkt)

        self.assertRaises(ValueError, wellknowntext.convert_well_known_text,
                          '')
예제 #2
0
    def test_wkt(self):
        """ Try the properties function"""
        wkt = "SRID=4326;POINT(-99 43)"
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertEquals(Point(geom), Point([-99, 43]))

        wkt = """MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),
            ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),
            (30 20, 20 15, 20 25, 30 20)))"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(Polygon(geom[0]).area, 87.5, 1)

        wkt = """MULTILINESTRING ((10 10, 20 20, 10 40),
        (40 40, 30 30, 40 20, 30 10))"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(LineString(geom[0]).length, 36.5, 1)

        wkt = """LINESTRING (30 10, 10 30, 40 40)"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(LineString(geom).length, 59.9, 1)

        wkt = """POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"""
        geom = wellknowntext.convert_well_known_text(wkt)
        self.assertAlmostEquals(Polygon(geom[0]).area, 550., 1)

        wkt = """POLYGON q((30 10, 40 40, 20 40, 10 20, 30 10))q"""
        self.assertRaises(ValueError,
                          wellknowntext.convert_well_known_text, wkt)

        wkt = """RARRR q((30 10, 40 40, 20 40, 10 20, 30 10))q"""
        self.assertRaises(ValueError,
                          wellknowntext.convert_well_known_text, wkt)

        self.assertRaises(ValueError,
                          wellknowntext.convert_well_known_text, '')
예제 #3
0
파일: idep2.py 프로젝트: pingyangtiaer/iem
def do(dt):
    """Generate for a given date """
    dbconn = psycopg2.connect(database='idep', host='iemdb', user='******')
    cursor = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    cursor.execute(
        """
    SELECT ST_AsText(i.geom), i.huc_12, coalesce(avg_precip, 0),
    coalesce(avg_loss, 0), coalesce(avg_runoff, 0),
    coalesce(avg_delivery, 0) from ia_huc12 i JOIN results_by_huc12 r
    on (r.huc_12 = i.huc_12) WHERE valid = %s
    """, (dt, ))

    os.chdir("/tmp")
    fn = "idepv2_%s" % (dt.strftime("%Y%m%d"), )
    shp = shapelib.create(fn, shapelib.SHPT_POLYGON)

    dbf = dbflib.create(fn)
    dbf.add_field("HUC_12", dbflib.FTString, 12, 0)
    dbf.add_field("PREC_MM", dbflib.FTDouble, 8, 2)
    dbf.add_field("LOS_KGM2", dbflib.FTDouble, 8, 2)
    dbf.add_field("RUNOF_MM", dbflib.FTDouble, 8, 2)
    dbf.add_field("DELI_KGM", dbflib.FTDouble, 8, 2)

    for i, row in enumerate(cursor):
        g = wellknowntext.convert_well_known_text(row[0])
        obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, g)
        shp.write_object(-1, obj)
        del (obj)

        dbf.write_record(
            i,
            dict(HUC_12=row[1],
                 PREC_MM=row[2],
                 LOS_KGM2=row[3],
                 RUNOF_MM=row[4],
                 DELI_KGM=row[5]))

    # hack way to close the files
    del (shp)
    del (dbf)

    shutil.copyfile("/mesonet/www/apps/iemwebsite/data/gis/meta/26915.prj",
                    fn + ".prj")
    z = zipfile.ZipFile(fn + ".zip", 'w', zipfile.ZIP_DEFLATED)
    suffixes = ['shp', 'shx', 'dbf', 'prj']
    for s in suffixes:
        z.write(fn + "." + s)
    z.close()

    sys.stdout.write("Content-type: application/octet-stream\n")
    sys.stdout.write(("Content-Disposition: attachment; filename=%s.zip\n\n"
                      "") % (fn, ))

    sys.stdout.write(file(fn + ".zip", 'r').read())

    suffixes.append('zip')
    for s in suffixes:
        os.remove(fn + "." + s)
예제 #4
0
파일: idep2.py 프로젝트: KayneWest/iem
def do(dt):
    """Generate for a given date """
    dbconn = psycopg2.connect(database='idep', host='iemdb', user='******')
    cursor = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    cursor.execute("""
    SELECT ST_AsText(i.geom), i.huc_12, coalesce(avg_precip, 0),
    coalesce(avg_loss, 0), coalesce(avg_runoff, 0),
    coalesce(avg_delivery, 0) from ia_huc12 i JOIN results_by_huc12 r
    on (r.huc_12 = i.huc_12) WHERE valid = %s
    """, (dt,))

    os.chdir("/tmp")
    fn = "idepv2_%s" % (dt.strftime("%Y%m%d"),)
    shp = shapelib.create(fn, shapelib.SHPT_POLYGON)

    dbf = dbflib.create(fn)
    dbf.add_field("HUC_12", dbflib.FTString, 12, 0)
    dbf.add_field("PREC_MM", dbflib.FTDouble, 8, 2)
    dbf.add_field("LOS_KGM2", dbflib.FTDouble, 8, 2)
    dbf.add_field("RUNOF_MM", dbflib.FTDouble, 8, 2)
    dbf.add_field("DELI_KGM", dbflib.FTDouble, 8, 2)

    for i, row in enumerate(cursor):
        g = wellknowntext.convert_well_known_text(row[0])
        obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, g)
        shp.write_object(-1, obj)
        del(obj)

        dbf.write_record(i, dict(HUC_12=row[1],
                                 PREC_MM=row[2],
                                 LOS_KGM2=row[3],
                                 RUNOF_MM=row[4],
                                 DELI_KGM=row[5]))

    # hack way to close the files
    del(shp)
    del(dbf)

    shutil.copyfile("/mesonet/www/apps/iemwebsite/data/gis/meta/26915.prj",
                    fn+".prj")
    z = zipfile.ZipFile(fn+".zip", 'w', zipfile.ZIP_DEFLATED)
    suffixes = ['shp', 'shx', 'dbf', 'prj']
    for s in suffixes:
        z.write(fn+"."+s)
    z.close()

    sys.stdout.write("Content-type: application/octet-stream\n")
    sys.stdout.write(("Content-Disposition: attachment; filename=%s.zip\n\n"
                      "") % (fn,))

    sys.stdout.write(file(fn+".zip", 'r').read())

    suffixes.append('zip')
    for s in suffixes:
        os.remove(fn+"."+s)
예제 #5
0
def main(year, etn, start_response):
    """Go Main Go"""
    pcursor = POSTGIS.cursor(cursor_factory=psycopg2.extras.DictCursor)

    basefn = "watch_%s_%s" % (year, etn)

    os.chdir("/tmp/")

    sql = """select
        ST_astext(ST_multi(ST_union(ST_SnapToGrid(u.geom,0.0001)))) as tgeom
        from warnings_%s w JOIN ugcs u on (u.gid = w.gid)
        WHERE significance = 'A'
        and phenomena IN ('TO','SV') and eventid = %s and
        ST_isvalid(u.geom)
        and issue < ((select issued from watches WHERE num = %s
        and extract(year from issued) = %s LIMIT 1) + '60 minutes'::interval)
    """ % (
        year,
        etn,
        etn,
        year,
    )
    pcursor.execute(sql)
    if pcursor.rowcount == 0:
        start_response("200 OK", [("Content-type", "text/plain")])
        return b"No Data Found"

    shpio = BytesIO()
    shxio = BytesIO()
    dbfio = BytesIO()
    with shapefile.Writer(shx=shxio, shp=shpio, dbf=dbfio) as shp:
        shp.field("SIG", "C", "1")
        shp.field("ETN", "I", "4")

        row = pcursor.fetchone()
        s = row["tgeom"]
        f = wellknowntext.convert_well_known_text(s)
        shp.poly(f)
        shp.record("A", etn)

    zio = BytesIO()
    with zipfile.ZipFile(zio, mode="w",
                         compression=zipfile.ZIP_DEFLATED) as zf:
        zf.writestr(basefn + ".prj",
                    open(("/opt/iem/data/gis/meta/4326.prj")).read())
        zf.writestr(basefn + ".shp", shpio.getvalue())
        zf.writestr(basefn + ".shx", shxio.getvalue())
        zf.writestr(basefn + ".dbf", dbfio.getvalue())

    headers = [
        ("Content-type", "application/octet-stream"),
        ("Content-Disposition", "attachment; filename=%s.zip" % (basefn, )),
    ]
    start_response("200 OK", headers)
    return zio.getvalue()
예제 #6
0
def main():
    """Go Main Go"""
    pgconn = get_dbconn('postgis', user='******')
    pcursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    pcursor.execute("SET TIME ZONE 'UTC'")

    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)

    w = shapefile.Writer(shapefile.POINT)
    w.field("VALID", 'C', 12, 0)
    w.field("MAG", 'F', 10, 2)
    w.field("WFO", 'C', 3, 0)
    w.field("TYPECODE", 'C', 1, 0)
    w.field("TYPETEXT", 'C', 40, 0)
    w.field("CITY", 'C', 40, 0)
    w.field("COUNTY", 'C', 40, 0)
    w.field("SOURCE", 'C', 40, 0)
    w.field("REMARK", 'C', 200, 0)

    pcursor.execute("""
        SELECT distinct *, ST_astext(geom) as tgeom from lsrs_%s WHERE
        valid > (now() -'1 day'::interval)
        """ % (ets.year, ))

    for row in pcursor:
        s = row["tgeom"]
        if s is None or s == "":
            continue
        f = wellknowntext.convert_well_known_text(s)
        w.point(f[0], f[1])
        w.record(row['valid'].strftime("%Y%m%d%H%M"),
                 float(row['magnitude'] or 0), row['wfo'], row['type'],
                 row['typetext'], row['city'], row['county'], row['source'],
                 row['remark'][:200])
    w.save("lsr_24hour.shp")
    zfh = zipfile.ZipFile("lsr_24hour.zip", 'w', zipfile.ZIP_DEFLATED)
    zfh.write("lsr_24hour.shp")
    zfh.write("lsr_24hour.shx")
    zfh.write("lsr_24hour.dbf")
    shutil.copy('/opt/iem/data/gis/meta/4326.prj', 'lsr_24hour.prj')
    zfh.write("lsr_24hour.prj")
    zfh.close()

    cmd = ("/home/ldm/bin/pqinsert "
           "-p \"zip c %s gis/shape/4326/us/lsr_24hour.zip "
           "bogus zip\" lsr_24hour.zip") % (ets.strftime("%Y%m%d%H%M"), )
    subprocess.call(cmd, shell=True)

    for suffix in ['shp', 'shx', 'dbf', 'prj', 'zip']:
        os.remove('lsr_24hour.%s' % (suffix, ))
예제 #7
0
def export_shapefile(txn, utc):
    """Export a Shapefile of Road Conditions"""
    os.chdir("/tmp")
    w = shapefile.Writer(shapefile.POLYLINE)
    w.field("SEGID", 'N', 6, 0)
    w.field("MAJOR", 'S', 10, 0)
    w.field("MINOR", 'S', 128, 0)
    w.field("US1", 'N', 4, 0)
    w.field("ST1", 'N', 4, 0)
    w.field("INT1", 'N', 4, 0)
    w.field("TYPE", 'N', 4, 0)
    w.field("VALID", 'S', 12, 0)
    w.field("COND_CODE", 'N', 4, 0)
    w.field("COND_TXT", 'S', 120, 0)
    w.field("BAN_TOW", 'S', 1, 0)
    w.field("LIM_VIS", 'S', 1, 0)

    txn.execute("""select b.*, c.*, ST_astext(b.geom) as bgeom from
         roads_base b, roads_current c WHERE b.segid = c.segid
         and valid is not null and b.geom is not null""")
    for row in txn:
        s = row["bgeom"]
        f = wellknowntext.convert_well_known_text(s)
        w.line(parts=f)
        w.record(row["segid"], row["major"], row["minor"], row["us1"],
                 row["st1"], row["int1"], row["type"],
                 row["valid"].strftime("%Y%m%d%H%M"), row["cond_code"],
                 row["raw"],
                 str(row["towing_prohibited"])[0],
                 str(row["limited_vis"])[0])

    w.save('iaroad_cond')
    z = zipfile.ZipFile("iaroad_cond.zip", 'w')
    z.write("iaroad_cond.shp")
    z.write("iaroad_cond.shx")
    z.write("iaroad_cond.dbf")
    o = open('iaroad_cond.prj', 'w')
    o.write(EPSG26915)
    o.close()
    z.write("iaroad_cond.prj")
    z.close()

    subprocess.call(
        ("/home/ldm/bin/pqinsert -p 'zip ac %s "
         "gis/shape/26915/ia/iaroad_cond.zip "
         "GIS/iaroad_cond_%s.zip zip' iaroad_cond.zip"
         "") % (utc.strftime("%Y%m%d%H%M"), utc.strftime("%Y%m%d%H%M")),
        shell=True)

    for suffix in ['shp', 'shx', 'dbf', 'prj', 'zip']:
        os.unlink("iaroad_cond.%s" % (suffix, ))
예제 #8
0
def main(year, etn):
    """Go Main Go"""
    pcursor = POSTGIS.cursor(cursor_factory=psycopg2.extras.DictCursor)

    basefn = "watch_%s_%s" % (year, etn)

    os.chdir("/tmp/")

    w = shapefile.Writer(shapefile.POLYGON)
    w.field('SIG', 'C', '1')
    w.field('ETN', 'I', '4')

    sql = """select
        ST_astext(ST_multi(ST_union(ST_SnapToGrid(u.geom,0.0001)))) as tgeom
        from warnings_%s w JOIN ugcs u on (u.gid = w.gid)
        WHERE significance = 'A'
        and phenomena IN ('TO','SV') and eventid = %s and
        ST_isvalid(u.geom)
        and issue < ((select issued from watches WHERE num = %s
        and extract(year from issued) = %s LIMIT 1) + '60 minutes'::interval)
    """ % (year, etn, etn, year)
    pcursor.execute(sql)

    if pcursor.rowcount == 0:
        sys.exit()

    row = pcursor.fetchone()
    s = row["tgeom"]
    f = wellknowntext.convert_well_known_text(s)
    w.poly(parts=f)
    w.record('A', etn)
    w.save(basefn)

    # Create zip file, send it back to the clients
    shutil.copyfile("/opt/iem/data/gis/meta/4326.prj", "%s.prj" % (basefn, ))
    z = zipfile.ZipFile(basefn + ".zip", 'w', zipfile.ZIP_DEFLATED)
    for suffix in ['shp', 'shx', 'dbf', 'prj']:
        z.write("%s.%s" % (basefn, suffix))
    z.close()

    ssw("Content-type: application/octet-stream\n")
    ssw(("Content-Disposition: attachment; filename=%s.zip\n\n") % (basefn, ))

    ssw(open("%s.zip" % (basefn, ), 'rb').read())

    for suffix in ['zip', 'shp', 'shx', 'dbf', 'prj']:
        os.remove("%s.%s" % (basefn, suffix))
예제 #9
0
def main(year, etn):
    """Go Main Go"""
    pcursor = POSTGIS.cursor(cursor_factory=psycopg2.extras.DictCursor)

    basefn = "watch_%s_%s" % (year, etn)

    os.chdir("/tmp/")

    sql = """select
        ST_astext(ST_multi(ST_union(ST_SnapToGrid(u.geom,0.0001)))) as tgeom
        from warnings_%s w JOIN ugcs u on (u.gid = w.gid)
        WHERE significance = 'A'
        and phenomena IN ('TO','SV') and eventid = %s and
        ST_isvalid(u.geom)
        and issue < ((select issued from watches WHERE num = %s
        and extract(year from issued) = %s LIMIT 1) + '60 minutes'::interval)
    """ % (year, etn, etn, year)
    pcursor.execute(sql)
    if pcursor.rowcount == 0:
        sys.exit()

    shpio = BytesIO()
    shxio = BytesIO()
    dbfio = BytesIO()
    with shapefile.Writer(shx=shxio, shp=shpio, dbf=dbfio) as shp:
        shp.field('SIG', 'C', '1')
        shp.field('ETN', 'I', '4')

        row = pcursor.fetchone()
        s = row["tgeom"]
        f = wellknowntext.convert_well_known_text(s)
        shp.poly(f)
        shp.record('A', etn)

    zio = BytesIO()
    zf = zipfile.ZipFile(zio, mode='w',
                         compression=zipfile.ZIP_DEFLATED)
    zf.writestr(basefn+'.prj',
                open(('/opt/iem/data/gis/meta/4326.prj'
                      )).read())
    zf.writestr(basefn+".shp", shpio.getvalue())
    zf.writestr(basefn+'.shx', shxio.getvalue())
    zf.writestr(basefn+'.dbf', dbfio.getvalue())
    zf.close()
    ssw(("Content-Disposition: attachment; filename=%s.zip\n\n") % (basefn,))
    ssw(zio.getvalue())
예제 #10
0
sql = """DELETE from nexrad_attributes WHERE valid < '%s+00'""" % (
							eTS.strftime("%Y-%m-%d %H:%M"), )
pcursor.execute( sql )
sql = """SELECT *, valid at time zone 'UTC' as utcvalid, 
	ST_astext(geom) as tgeom, ST_x(geom) as lon, 
       ST_y(geom) as lat from nexrad_attributes """
pcursor.execute( sql )

cnt = 0
for row in pcursor:
	#print rs[i]
	s = row["tgeom"]
	if s == None or s == "":
		continue
	f = wellknowntext.convert_well_known_text(s)
	
	d = {}
	d["VALID"] = row['utcvalid'].strftime("%Y%m%d%H%M")
	d["NEXRAD"] = row['nexrad']
	d["STORM_ID"] = row['storm_id']
	d["AZIMUTH"] = float(row['azimuth'])
	d["RANGE"] = float(row['range'])
	d["TVS"] = row['tvs']
	d["MESO"] = row['meso']
	d["POSH"] = float(row['posh'])
	d["POH"] = float(row['poh'])
	d["MAX_SIZE"] = float(row['max_size'])
	d["VIL"] = float(row['vil'])
	d["MAX_DBZ"] = float(row['max_dbz'])
	d["MAX_DBZ_H"] = float(row['max_dbz_height'])
예제 #11
0
    shp = shapelib.create(fp, shapelib.SHPT_POLYGON)
dbf = dbflib.create(fp)
dbf.add_field("DAY_STA", dbflib.FTString, 8, 0)
dbf.add_field("DAY_END", dbflib.FTString, 8, 0)
dbf.add_field("MODL_TWP", dbflib.FTString, 10, 0)
dbf.add_field("PRECIP", dbflib.FTDouble, 8, 4)
dbf.add_field("LOSS", dbflib.FTDouble, 8, 4)
dbf.add_field("RUNOFF", dbflib.FTDouble, 8, 4)

i = 0
for row in wcursor:
    m = row['model_twp']
    loss = row['avg_loss']
    runoff = row['avg_runoff']
    precip = row['avg_precip']
    f = wellknowntext.convert_well_known_text( twp[m] )
    if form.has_key("point"):
        obj = shapelib.SHPObject(shapelib.SHPT_POINT, 1, [[f]] )
    else:
        obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, f )
    shp.write_object(-1, obj)
    dbf.write_record(i, (day1,day2,m,precip,loss,runoff) )
    i += 1

del(dbf)
del(shp)
WEPP.close()

o = open(fp+".txt", 'w')
o.write("""
WEPP Modelled Erosion from the Iowa Daily Erosion Project
예제 #12
0
def export_shapefile(txn, tp):
    """Export a Shapefile of Road Conditions"""
    os.chdir("/tmp")
    dbf = dbflib.create("iaroad_cond")
    dbf.add_field("SEGID", dbflib.FTInteger, 4, 0)
    dbf.add_field("MAJOR", dbflib.FTString, 10, 0)
    dbf.add_field("MINOR", dbflib.FTString, 128, 0)
    dbf.add_field("US1", dbflib.FTInteger, 4, 0)
    dbf.add_field("ST1", dbflib.FTInteger, 4, 0)
    dbf.add_field("INT1", dbflib.FTInteger, 4, 0)
    dbf.add_field("TYPE", dbflib.FTInteger, 4, 0)
    dbf.add_field("VALID", dbflib.FTString, 12, 0)
    dbf.add_field("COND_CODE", dbflib.FTInteger, 4, 0)
    dbf.add_field("COND_TXT", dbflib.FTString, 120, 0)
    dbf.add_field("BAN_TOW", dbflib.FTString, 1, 0)
    dbf.add_field("LIM_VIS", dbflib.FTString, 1, 0)

    shp = shapelib.create("iaroad_cond", shapelib.SHPT_ARC)

    txn.execute("""select b.*, c.*, ST_astext(b.geom) as bgeom from
         roads_base b, roads_current c WHERE b.segid = c.segid
         and valid is not null and b.geom is not null""")
    i = 0
    for row in txn:
        s = row["bgeom"]
        f = wellknowntext.convert_well_known_text(s)
        valid = row["valid"]
        d = {}
        d["SEGID"] = row["segid"]
        d["MAJOR"] = row["major"]
        d["MINOR"] = row["minor"]
        d["US1"] = row["us1"]
        d["ST1"] = row["st1"]
        d["INT1"] = row["int1"]
        d["TYPE"] = row["type"]
        d["VALID"] = valid.strftime("%Y%m%d%H%M")
        d["COND_CODE"] = row["cond_code"]
        d["COND_TXT"] = row["raw"]
        d["BAN_TOW"] = str(row["towing_prohibited"])[0]
        d["LIM_VIS"] = str(row["limited_vis"])[0]

        obj = shapelib.SHPObject(shapelib.SHPT_ARC, 1, f)
        shp.write_object(-1, obj)
        dbf.write_record(i, d)

        del(obj)
        i += 1

    del(shp)
    del(dbf)
    z = zipfile.ZipFile("iaroad_cond.zip", 'w')
    z.write("iaroad_cond.shp")
    z.write("iaroad_cond.shx")
    z.write("iaroad_cond.dbf")
    o = open('iaroad_cond.prj', 'w')
    o.write(EPSG26915)
    o.close()
    z.write("iaroad_cond.prj")
    z.close()

    utc = tp + datetime.timedelta(hours=6)
    subprocess.call(("/home/ldm/bin/pqinsert -p 'zip ac %s "
                     "gis/shape/26915/ia/iaroad_cond.zip "
                     "GIS/iaroad_cond_%s.zip zip' iaroad_cond.zip"
                     "") % (utc.strftime("%Y%m%d%H%M"),
                            utc.strftime("%Y%m%d%H%M")), shell=True)

    for suffix in ['shp', 'shx', 'dbf', 'prj', 'zip']:
        os.unlink("iaroad_cond.%s" % (suffix,))
예제 #13
0
def test_wkt():
    """ Try the properties function"""
    wkt = "SRID=4326;POINT(-99 43)"
    geom = wellknowntext.convert_well_known_text(wkt)
    assert Point(geom) == Point([-99, 43])

    wkt = """MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),
        ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),
        (30 20, 20 15, 20 25, 30 20)))"""
    geom = wellknowntext.convert_well_known_text(wkt)
    assert abs(Polygon(geom[0]).area - 87.5) < 0.1

    wkt = """MULTILINESTRING ((10 10, 20 20, 10 40),
    (40 40, 30 30, 40 20, 30 10))"""
    geom = wellknowntext.convert_well_known_text(wkt)
    assert abs(LineString(geom[0]).length - 36.5) < 0.1

    wkt = """LINESTRING (30 10, 10 30, 40 40)"""
    geom = wellknowntext.convert_well_known_text(wkt)
    assert abs(LineString(geom).length - 59.9) < 0.1

    wkt = """POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"""
    geom = wellknowntext.convert_well_known_text(wkt)
    assert abs(Polygon(geom[0]).area - 550.) < 0.1

    wkt = """POLYGON q((30 10, 40 40, 20 40, 10 20, 30 10))q"""
    with pytest.raises(ValueError):
        wellknowntext.convert_well_known_text(wkt)

    wkt = """RARRR q((30 10, 40 40, 20 40, 10 20, 30 10))q"""
    with pytest.raises(ValueError):
        wellknowntext.convert_well_known_text(wkt)

    with pytest.raises(ValueError):
        wellknowntext.convert_well_known_text('')
예제 #14
0
def real_parser(txn, raw):
    """Actually do the heavy lifting of parsing this product

    Args:
      txn (cursor): psycopg2 database transaction
      raw (str): the raw text that needs parsing
    """
    # Load up dictionary of Possible Road Conditions
    if len(ROADS) == 0:
        log.msg("Initializing ROADS and CONDITIONS dicts...")
        init_dicts(txn)

    tp = TextProduct(raw)
    log.msg("PROCESSING STOIA: %s" % (tp.valid,))

    # Lets start our processing by looking for the first * and then
    # processing after finding it
    lines = re.split("\n", raw[raw.find("*") :])
    for line in lines:
        if len(line) < 40 or line[0] == "*" or line[30:40].strip() == "":
            continue
        data = line[7:]
        # Find the right most ) and chomp everything up until it
        pos = data.rfind(")")
        meat = data[: pos + 1].upper()
        condition = data[(pos + 1) :].upper().strip()
        if meat.strip() == "":
            continue
        if meat not in ROADS:
            log.msg("Unknown road: %s\n" % (meat,))
            continue

        road_code = figureCondition(txn, condition)
        towingProhibited = condition.find("TOWING PROHIBITED") > -1
        limitedVis = condition.find("LIMITED VIS.") > -1
        segid = ROADS[meat]["segid"]

        txn.execute(
            """
            UPDATE roads_current SET cond_code = %s, valid = %s,
            towing_prohibited = %s, limited_vis = %s, raw = %s
            WHERE segid = %s
            """,
            (road_code, tp.valid, towingProhibited, limitedVis, condition, segid),
        )

    # Copy the currents table over to the log... HARD CODED
    if tp.valid.month < 7:
        logtable = "roads_%s_%s_log" % (tp.valid.year - 1, tp.valid.year)
    else:
        logtable = "roads_%s_%s_log" % (tp.valid.year, tp.valid.year + 1)
    txn.execute(
        """
        INSERT into """
        + logtable
        + """
        SELECT * from roads_current WHERE valid = %s
        """,
        (tp.valid,),
    )
    log.msg("Copied %s rows into %s table" % (txn.rowcount, logtable))

    # Now we generate a shapefile....
    dbf = dbflib.create("iaroad_cond")
    dbf.add_field("SEGID", dbflib.FTInteger, 4, 0)
    dbf.add_field("MAJOR", dbflib.FTString, 10, 0)
    dbf.add_field("MINOR", dbflib.FTString, 128, 0)
    dbf.add_field("US1", dbflib.FTInteger, 4, 0)
    dbf.add_field("ST1", dbflib.FTInteger, 4, 0)
    dbf.add_field("INT1", dbflib.FTInteger, 4, 0)
    dbf.add_field("TYPE", dbflib.FTInteger, 4, 0)
    dbf.add_field("VALID", dbflib.FTString, 12, 0)
    dbf.add_field("COND_CODE", dbflib.FTInteger, 4, 0)
    dbf.add_field("COND_TXT", dbflib.FTString, 120, 0)
    dbf.add_field("BAN_TOW", dbflib.FTString, 1, 0)
    dbf.add_field("LIM_VIS", dbflib.FTString, 1, 0)

    shp = shapelib.create("iaroad_cond", shapelib.SHPT_ARC)

    txn.execute(
        """select b.*, c.*, ST_astext(b.geom) as bgeom from
         roads_base b, roads_current c WHERE b.segid = c.segid
         and valid is not null and b.geom is not null"""
    )
    i = 0
    for row in txn:
        s = row["bgeom"]
        f = wellknowntext.convert_well_known_text(s)
        valid = row["valid"]
        d = {}
        d["SEGID"] = row["segid"]
        d["MAJOR"] = row["major"]
        d["MINOR"] = row["minor"]
        d["US1"] = row["us1"]
        d["ST1"] = row["st1"]
        d["INT1"] = row["int1"]
        d["TYPE"] = row["type"]
        d["VALID"] = valid.strftime("%Y%m%d%H%M")
        d["COND_CODE"] = row["cond_code"]
        d["COND_TXT"] = row["raw"]
        d["BAN_TOW"] = str(row["towing_prohibited"])[0]
        d["LIM_VIS"] = str(row["limited_vis"])[0]

        obj = shapelib.SHPObject(shapelib.SHPT_ARC, 1, f)
        shp.write_object(-1, obj)
        dbf.write_record(i, d)

        del (obj)
        i += 1

    del (shp)
    del (dbf)
    z = zipfile.ZipFile("iaroad_cond.zip", "w")
    z.write("iaroad_cond.shp")
    z.write("iaroad_cond.shx")
    z.write("iaroad_cond.dbf")
    o = open("iaroad_cond.prj", "w")
    o.write(EPSG26915)
    o.close()
    z.write("iaroad_cond.prj")
    z.close()

    utc = tp.valid.astimezone(pytz.timezone("UTC"))
    subprocess.call(
        (
            "/home/ldm/bin/pqinsert -p 'zip ac %s "
            "gis/shape/26915/ia/iaroad_cond.zip "
            "GIS/iaroad_cond_%s.zip zip' iaroad_cond.zip"
            ""
        )
        % (utc.strftime("%Y%m%d%H%M"), utc.strftime("%Y%m%d%H%M")),
        shell=True,
    )

    for suffix in ["shp", "shx", "dbf", "prj", "zip"]:
        os.unlink("iaroad_cond.%s" % (suffix,))
예제 #15
0
def export_shapefile(txn, tp):
    """Export a Shapefile of Road Conditions"""
    os.chdir("/tmp")
    dbf = dbflib.create("iaroad_cond")
    dbf.add_field("SEGID", dbflib.FTInteger, 6, 0)
    dbf.add_field("MAJOR", dbflib.FTString, 10, 0)
    dbf.add_field("MINOR", dbflib.FTString, 128, 0)
    dbf.add_field("US1", dbflib.FTInteger, 4, 0)
    dbf.add_field("ST1", dbflib.FTInteger, 4, 0)
    dbf.add_field("INT1", dbflib.FTInteger, 4, 0)
    dbf.add_field("TYPE", dbflib.FTInteger, 4, 0)
    dbf.add_field("VALID", dbflib.FTString, 12, 0)
    dbf.add_field("COND_CODE", dbflib.FTInteger, 4, 0)
    dbf.add_field("COND_TXT", dbflib.FTString, 120, 0)
    dbf.add_field("BAN_TOW", dbflib.FTString, 1, 0)
    dbf.add_field("LIM_VIS", dbflib.FTString, 1, 0)

    shp = shapelib.create("iaroad_cond", shapelib.SHPT_ARC)

    txn.execute("""select b.*, c.*, ST_astext(b.geom) as bgeom from
         roads_base b, roads_current c WHERE b.segid = c.segid
         and valid is not null and b.geom is not null""")
    i = 0
    for row in txn:
        s = row["bgeom"]
        f = wellknowntext.convert_well_known_text(s)
        valid = row["valid"]
        d = {}
        d["SEGID"] = row["segid"]
        d["MAJOR"] = row["major"]
        d["MINOR"] = row["minor"]
        d["US1"] = row["us1"]
        d["ST1"] = row["st1"]
        d["INT1"] = row["int1"]
        d["TYPE"] = row["type"]
        d["VALID"] = valid.strftime("%Y%m%d%H%M")
        d["COND_CODE"] = row["cond_code"]
        d["COND_TXT"] = row["raw"]
        d["BAN_TOW"] = str(row["towing_prohibited"])[0]
        d["LIM_VIS"] = str(row["limited_vis"])[0]

        obj = shapelib.SHPObject(shapelib.SHPT_ARC, 1, f)
        shp.write_object(-1, obj)
        dbf.write_record(i, d)

        del (obj)
        i += 1

    del (shp)
    del (dbf)
    z = zipfile.ZipFile("iaroad_cond.zip", 'w')
    z.write("iaroad_cond.shp")
    z.write("iaroad_cond.shx")
    z.write("iaroad_cond.dbf")
    o = open('iaroad_cond.prj', 'w')
    o.write(EPSG26915)
    o.close()
    z.write("iaroad_cond.prj")
    z.close()

    utc = tp + datetime.timedelta(hours=6)
    subprocess.call(
        ("/home/ldm/bin/pqinsert -p 'zip ac %s "
         "gis/shape/26915/ia/iaroad_cond.zip "
         "GIS/iaroad_cond_%s.zip zip' iaroad_cond.zip"
         "") % (utc.strftime("%Y%m%d%H%M"), utc.strftime("%Y%m%d%H%M")),
        shell=True)

    for suffix in ['shp', 'shx', 'dbf', 'prj', 'zip']:
        os.unlink("iaroad_cond.%s" % (suffix, ))
예제 #16
0
파일: 24h_lsr.py 프로젝트: nbackas/iem
dbf.add_field("COUNTY", dbflib.FTString, 40, 0)
dbf.add_field("SOURCE", dbflib.FTString, 40, 0)
dbf.add_field("REMARK", dbflib.FTString, 200, 0)

#sql = "SELECT *, astext(geom) as tgeom from warnings WHERE issue < '%s' and \
sql = """SELECT distinct *, ST_astext(geom) as tgeom from lsrs_%s WHERE 
	valid > (now() -'1 day'::interval) """ % (eTS.year, )
pcursor.execute(sql)

cnt = 0
for row in pcursor:

    s = row["tgeom"]
    if s == None or s == "":
        continue
    f = wellknowntext.convert_well_known_text(s)

    d = {}
    d["VALID"] = row['valid'].strftime("%Y%m%d%H%M")
    d["MAG"] = float(row['magnitude'] or 0)
    d["TYPECODE"] = row['type']
    d["WFO"] = row['wfo']
    d["TYPETEXT"] = row['typetext']
    d["CITY"] = row['city']
    d["COUNTY"] = row['county']
    d["SOURCE"] = row['source']
    d["REMARK"] = row['remark'][:200]
    obj = shapelib.SHPObject(shapelib.SHPT_POINT, 1, [[f]])
    shp.write_object(-1, obj)
    dbf.write_record(cnt, d)
    del (obj)
예제 #17
0
def generate_shapefile(ts):
    """ Generate a shapefile of this data """
    # Now we generate a shapefile....
    dbf = dbflib.create("iaroad_cond")
    dbf.add_field("SEGID", dbflib.FTInteger, 4, 0)
    dbf.add_field("MAJOR", dbflib.FTString, 10, 0)
    dbf.add_field("MINOR", dbflib.FTString, 40, 0)
    dbf.add_field("US1", dbflib.FTInteger, 4, 0)
    dbf.add_field("ST1", dbflib.FTInteger, 4, 0)
    dbf.add_field("INT1", dbflib.FTInteger, 4, 0)
    dbf.add_field("TYPE", dbflib.FTInteger, 4, 0)
    dbf.add_field("VALID", dbflib.FTString, 12, 0)
    dbf.add_field("COND_CODE", dbflib.FTInteger, 4, 0)
    dbf.add_field("COND_TXT", dbflib.FTString, 120, 0)
    dbf.add_field("BAN_TOW", dbflib.FTString, 1, 0)
    dbf.add_field("LIM_VIS", dbflib.FTString, 1, 0)

    shp = shapelib.create("iaroad_cond", shapelib.SHPT_ARC)

    pcursor.execute("""select b.*, c.*, astext(b.geom) as bgeom from 
         roads_base b, roads_current c WHERE b.segid = c.segid""")
    i = 0
    for row in pcursor:
        s = row["bgeom"]
        f = wellknowntext.convert_well_known_text(s)
        valid = row["valid"]
        d = {}
        d["SEGID"] = row["segid"]
        d["MAJOR"] = row["major"]
        d["MINOR"] = row["minor"]
        d["US1"] = row["us1"]
        d["ST1"] = row["st1"]
        d["INT1"] = row["int1"]
        d["TYPE"] = row["type"]
        d["VALID"] = valid.strftime("%Y%m%d%H%M")
        d["COND_CODE"] = row["cond_code"]
        d["COND_TXT"] = row["raw"]
        d["BAN_TOW"] = str(row["towing_prohibited"])[0]
        d["LIM_VIS"] = str(row["limited_vis"])[0]

        obj = shapelib.SHPObject(shapelib.SHPT_ARC, 1, f )
        shp.write_object(-1, obj)
        dbf.write_record(i, d)

        del(obj)
        i += 1

    del(shp)
    del(dbf)
    z = zipfile.ZipFile("iaroad_cond.zip", 'w')
    z.write("iaroad_cond.shp")
    z.write("iaroad_cond.shx")
    z.write("iaroad_cond.dbf")
    shutil.copyfile("/mesonet/data/gis/meta/26915.prj", "iaroad_cond.prj")
    z.write("iaroad_cond.prj")
    z.close()

    utc = ts.astimezone( pytz.timezone("UTC") )
    subprocess.call("/home/ldm/bin/pqinsert -p 'zip ac %s gis/shape/26915/ia/iaroad_cond.zip GIS/iaroad_cond_%s.zip zip' iaroad_cond.zip" % (
                                            utc.strftime("%Y%m%d%H%M"), 
                                            utc.strftime("%Y%m%d%H%M")), 
                    shell=True )

    for suffix in ['shp', 'shx', 'dbf', 'prj', 'zip']:
        os.unlink("iaroad_cond.%s" % (suffix,))