Ejemplo n.º 1
0
def make_shapefile(filename):
    # Create a shapefile with polygons
    outfile = shapelib.create(filename, shapelib.SHPT_POLYGON)

    # Create one very simple polygon and write it to the shapefile.  The
    # vertices should be given in clockwise order to comply with the
    # shapefile specification.
    obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
                             [[(10, 10), (10, 20), (20, 20), (10, 10)]])
    print obj.extents()
    print obj.vertices()
    outfile.write_object(-1, obj)

    # Create a polygon with a hole.  Note that according to the
    # shapefile specification, the vertices of the outer ring have to be
    # in clockwise order and the inner rings have to be in counter
    # clockwise order.
    #
    # There's an optional fourth parameter which when given must be a
    # list of part types, one for each part of the shape.  For polygons,
    # the part type is always shapelib.SHPP_RING, though.  The part
    # types are only relevant for SHPT_MULTIPATCH shapefiles.
    obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
                             [[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],
                              [(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)],
                              ])
    print obj.extents()
    print obj.vertices()
    outfile.write_object(-1, obj)

    # close the file.
    outfile.close()
Ejemplo n.º 2
0
def writeShp(geoList, outFilePath, geoType):
    if not (geoType in ["Point", "Ptgon", "Line"]):
        print "The output geometry type should be Point, Ptgon, or Line"
        return

    if geoType == "Point":
        geoT = shapelib.SHPT_POINT
    elif geoType == "Ptgon":
        geoT = shapelib.SHPT_POLYGON
    else:
        geoT = shapelib.SHPT_ARC

    outShp = shapelib.create(outFilePath, geoT)
    outDbf = dbflib.create(outFilePath.split('.')[0] + '.dbf')
    outDbf.add_field("ID", dbflib.FTInteger, 100, 0)

    for j in range(len(geoList)):
        recordDict = {"ID": j}
        if geoType == "Polygon":
            if geoList[j].geom_type == 'Polygon':
                vert = [list(geoList[j].exterior.coords)]
                for interi in geoList[j].interiors:
                    vert.append(list(interi.coords))
            #print vert
        else:
            vert = [list(geoList[j].coords)]
        obj = shapelib.SHPObject(geoT, -1, vert)
        outShp.write_object(-1, obj)
        outDbf.write_record(j, recordDict)

    print "%d records, %d fields" % (outDbf.record_count(),
                                     outDbf.field_count())
Ejemplo n.º 3
0
def make_shapefile(filename):
    obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, [[(10, 10), (20, 10),
                                                         (20, 20), (10, 10)]])
    logging.info(obj.extents())
    logging.info(obj.vertices())
    outfile = shapelib.create(filename, shapelib.SHPT_POLYGON)
    outfile.write_object(-1, obj)
    del outfile
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
    def write_shp(self, newShape):
        tempshp = shapelib.create('temp', self.shpType)
        for b in range(self.shpNum):
            print('\t merging -> %d' % b)
            shpobj = self.shp.read_object(b)  #(5, i, [[(), (),()], []])
            if len(shpobj.vertices()) > 1:
                shpobj = shapelib.SHPObject(self.shpType, b,
                                            [shpobj.vertices()[0]])
            tempshp.write_object(b, shpobj)
        tempshp.close()
        shutil.copy(self.buildingSHPFile + '.dbf', 'temp' + '.dbf')
        tempshp.close()

        self.clear_small_polygons('temp', newShape, 20.)
Ejemplo n.º 6
0
 def clear_small_polygons(self, shapeFile, newShapeFile, lenLimit):
     shp = shapelib.ShapeFile(shapeFile)
     dbf = dbflib.open(shapeFile)
     newSHP = shapelib.create(newShapeFile, self.shpType)
     newDBF = dbflib.create(newShapeFile)
     for f in range(dbf.field_count()):
         fi = dbf.field_info(f)
         newDBF.add_field(fi[1], fi[0], fi[2], fi[3])
     bb = 0
     for b in range(shp.info()[0]):
         sobj = shp.read_object(b)
         rec = dbf.read_record(b)
         if self.length_of_polygon(sobj.vertices()) > lenLimit:
             shpobj = shapelib.SHPObject(self.shpType, bb,
                                         [sobj.vertices()[0]])
             newSHP.write_object(bb, shpobj)
             newDBF.write_record(bb, rec)
             bb += 1
     shp.close()
     dbf.close()
     newSHP.close()
     newDBF.close()
Ejemplo n.º 7
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, ))
Ejemplo n.º 8
0
dbf.add_field("YYYYMMDD", dbflib.FTString, 8, 0)
dbf.add_field("HHMM", dbflib.FTString, 4, 0)
dbf.add_field("HI_T_F", dbflib.FTInteger, 10, 0)
dbf.add_field("LO_T_F", dbflib.FTInteger, 10, 0)
dbf.add_field("PREC", dbflib.FTDouble, 10, 2)
dbf.add_field("SNOW", dbflib.FTDouble, 10, 2)
dbf.add_field("SDEPTH", dbflib.FTDouble, 10, 2)
dbf.add_field("PMONTH", dbflib.FTDouble, 10, 2)
dbf.add_field("SMONTH", dbflib.FTDouble, 10, 2)

shp = shapelib.create("coop_"+ts, shapelib.SHPT_POINT)

j = 0
for sid in cob.keys():

    obj = shapelib.SHPObject(shapelib.SHPT_POINT, j,
                             [[(cob[sid]["LON"], cob[sid]["LAT"])]])
    shp.write_object(-1, obj)
    # print id, cob[sid]
    if cob[sid]["TMPX"] < 0:
        cob[sid]["TMPX"] = -99.
    if cob[sid]["TMPN"] < 0:
        cob[sid]["TMPN"] = -99.
    if cob[sid]["P24I"] < 0:
        cob[sid]["P24I"] = -99.
    if cob[sid]["SNOW"] < 0:
        cob[sid]["SNOW"] = -99.
    if cob[sid]["SNOD"] < 0:
        cob[sid]["SNOD"] = -99.
    if cob[sid]["PMOI"] < 0:
        cob[sid]["PMOI"] = -99.
    if cob[sid]["SMOI"] < 0:
Ejemplo n.º 9
0
    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)
    cnt += 1

del (shp)
del (dbf)
z = zipfile.ZipFile("lsr_24hour.zip", 'w', zipfile.ZIP_DEFLATED)
z.write("lsr_24hour.shp")
z.write("lsr_24hour.shx")
z.write("lsr_24hour.dbf")
shutil.copy('/mesonet/www/apps/iemwebsite/data/gis/meta/4326.prj',
            'lsr_24hour.prj')
z.write("lsr_24hour.prj")
z.close()
Ejemplo n.º 10
0
	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'])
	d["TOP"] = float(row['top'])
	d["DRCT"] = float(row['drct'])
	d["SKNT"] = float(row['sknt'])
	d["LAT"] = float(row['lat'])
	d["LON"] = float(row['lon'])
	#print d
	obj = shapelib.SHPObject(shapelib.SHPT_POINT, 1, [[f,]] )
	shp.write_object(-1, obj)
	dbf.write_record(cnt, d)
	del(obj)
	cnt += 1

if cnt == 0:
	if now.minute == 1:
		print 'No NEXRAD attributes found, this may be bad!'
	obj = shapelib.SHPObject(shapelib.SHPT_POINT, 1, [[(0.1, 0.1),]] )
	d = {}
	d["VALID"] = "200000000000"
	d["NEXRAD"] = "XXX"
	d["STORM_ID"] = "XX"
	d["AZIMUTH"] = 0
	d["RANGE"] = 0
Ejemplo n.º 11
0
                'precip': "%.1f" % (myPrecip,),
                'latitude': "%.4f" % (lat,),
                'longitude': "%.4f" % (lon,)
       })
    sys.stdout.write('Content-type: application/json\n\n')
    sys.stdout.write(json.dumps(res))

if format == 'shp':
    # Time to create the shapefiles
    fp = "iemre_%s_%s" % (ts0.strftime("%Y%m%d"), ts1.strftime("%Y%m"))
    shp = shapelib.create("%s.shp" % (fp,), shapelib.SHPT_POLYGON)

    for x in iemre.XAXIS:
        for y in iemre.YAXIS:
            obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
                                     [[(x, y), (x, y+iemre.DY),
                                       (x+iemre.DX, y+iemre.DY),
                                       (x+iemre.DX, y), (x, y)]])
            shp.write_object(-1, obj)

    del(shp)
    dbf = dbflib.create(fp)
    dbf.add_field("GDD", dbflib.FTDouble, 10, 2)
    dbf.add_field("PREC_IN", dbflib.FTDouble, 10, 2)

    cnt = 0
    for i in range(len(iemre.XAXIS)):
        for j in range(len(iemre.YAXIS)):
            dbf.write_record(cnt, {'PREC_IN': precip[j, i],
                                   'GDD': gdd[j, i]})
            cnt += 1
Ejemplo n.º 12
0
        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)

d = {}
d["SIG"] = 'A'
d["ETN"] = etn

obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, f)
shp.write_object(-1, obj)
dbf.write_record(0, d)
del(obj)

del(shp)
del(dbf)

# Create zip file, send it back to the clients
shutil.copyfile("/mesonet/www/apps/iemwebsite/data/gis/meta/4326.prj",
                fp+".prj")
z = zipfile.ZipFile(fp+".zip", 'w', zipfile.ZIP_DEFLATED)
z.write(fp+".shp")
z.write(fp+".shx")
z.write(fp+".dbf")
z.write(fp+".prj")