예제 #1
0
 def writetile(self,fname,xstart,xend,ystart,yend):
     #fname=os.path.join(self.info.dirname,self.tilename(xstart,xend,ystart,yend))
     verbprint("creating tile file:"+fname) 
     f=open(fname,'wb')
     a=self.info.sourcedata.gettile(xstart, xend, ystart, yend,
                                    float(self.info.missing)/
                                    float(self.info.scale))
     u="u"
     bb=int(self.info.wordsize)*8
     if self.info.signed:
         u=""
     type="%sint%i" % (u,bb)
     verbprint("scaling data by: "+str(self.info.scale))
     a=a/self.info.scale
     verbprint("converting data to: "+type)
     b=a.astype(type)
     if sys.byteorder not in ["big","little"]:
         raise Exception('strange, sys.byteorder is not "big" or "little"')
     if sys.byteorder != self.info.endian:
         verbprint("swapping byte order to: "+self.info.endian)
         b.byteswap(True)
     if not self.info.continuous:
         b=np.clip(b,1,self.info.maxcategories)
     
     verbprint("writing to file:"+fname)
     b.tofile(f)
     f.close()
예제 #2
0
    def gettile(self, xstart, xend, ystart, yend, missing):
        # need to fix this!!!!
        # gdal doesn't give this information for some reason
        # this is for NED data
        sourcemissing = -9999

        a = np.empty((yend - ystart + 1, xend - xstart + 1))
        a.fill(float(missing))
        for i in range(len(self.files)):
            f = self.source_tiles[i]
            ff = self.files[i]
            if ( f[0][0] <= xend or f[0][1] >= xstart ) and \
               ( f[1][0] <= yend or f[1][1] >= ystart ):
                txstart = max(f[0][0] + 1,
                              xstart) - 1  # convert to zero based index
                sxstart = txstart - f[0][0]
                txstart = txstart - xstart + 1
                txend = min(f[0][1] + 1, xend)
                sxend = txend - f[0][0]
                txend = txend - xstart + 1
                tystart = max(f[1][0] + 1,
                              ystart) - 1  # convert to zero based index
                systart = tystart - f[1][0]
                tystart = tystart - ystart + 1
                tyend = min(f[1][1] + 1, yend)
                syend = tyend - f[1][0]
                tyend = tyend - ystart + 1
                fd = gdal.Open(ff.filename)
                verbprint(
                    "fetching from %s: x: %i-%i, y: %i-%i" %
                    (ff.filename, sxstart + 1, sxend, systart + 1, syend))
                verbprint("to tile: x: %i-%i, y: %i-%i" %
                          (txstart + 1, txend, tystart + 1, tyend))
                xsize = sxend - sxstart
                ysize = syend - systart
                #print fd.ReadAsArray(sxstart,systart,xsize,ysize).shape
                a[tystart:tyend,txstart:txend]=\
                fd.ReadAsArray(sxstart,systart,xsize,ysize)#.transpose()
        # don't know if this actually works
        a = np.choose(np.equal(a, sourcemissing), (a, missing))
        return a
예제 #3
0
 def gettile(self,xstart,xend,ystart,yend,missing):
     # need to fix this!!!!
     # gdal doesn't give this information for some reason
     # this is for NED data
     sourcemissing=-9999
     
     a=np.empty((yend-ystart+1,xend-xstart+1))
     a.fill(float(missing))
     for i in range(len(self.files)):
         f=self.source_tiles[i]
         ff=self.files[i]
         if ( f[0][0] <= xend or f[0][1] >= xstart ) and \
            ( f[1][0] <= yend or f[1][1] >= ystart ):
             txstart=max(f[0][0]+1,xstart)-1  # convert to zero based index
             sxstart=txstart-f[0][0]
             txstart=txstart-xstart+1
             txend=min(f[0][1]+1,xend)
             sxend=txend-f[0][0]
             txend=txend-xstart+1
             tystart=max(f[1][0]+1,ystart)-1  # convert to zero based index
             systart=tystart-f[1][0]
             tystart=tystart-ystart+1
             tyend=min(f[1][1]+1,yend)
             syend=tyend-f[1][0]
             tyend=tyend-ystart+1
             fd=gdal.Open(ff.filename)
             verbprint("fetching from %s: x: %i-%i, y: %i-%i" %
                       (ff.filename,sxstart+1,sxend,systart+1,syend))
             verbprint("to tile: x: %i-%i, y: %i-%i" % 
                       (txstart+1,txend,tystart+1,tyend))
             xsize=sxend-sxstart
             ysize=syend-systart
             #print fd.ReadAsArray(sxstart,systart,xsize,ysize).shape
             a[tystart:tyend,txstart:txend]=\
             fd.ReadAsArray(sxstart,systart,xsize,ysize)#.transpose()
     # don't know if this actually works
     a=np.choose(np.equal(a,sourcemissing),(a,missing))
     return a
예제 #4
0
        def __init__(self, filename):
            """
            Initialize file_info from filename

            """

            verbprint("Opening file: " + filename)
            fg = gdal.Open(filename)
            fh = fg
            if fg is None:
                raise IOError("file: " + filename +
                              " cannot be found or opened by GDAL")

            self.filename = filename
            self.driver = fg.GetDriver().LongName
            self.sdriver = fg.GetDriver().ShortName
            self.bands = fh.RasterCount
            self.xsize = fh.RasterXSize
            self.ysize = fh.RasterYSize
            self.band_type = fh.GetRasterBand(1).DataType
            self.projection = fh.GetProjection()
            self.geotransform = fh.GetGeoTransform()
            self.spatialreference = osr.SpatialReference(self.projection)
            sr = self.spatialreference
            self.ct = osr.CoordinateTransformation(sr, sr.CloneGeogCS())
            #print self.ct.TransformPoint(self.geotransform[0],self.geotransform[3])
            ul = self.ct.TransformPoint(self.geotransform[0],
                                        self.geotransform[3])
            (self.ulx, self.uly) = (ul[0], ul[1])
            self.xres = abs(self.geotransform[1])
            self.yres = abs(self.geotransform[5])
            lr = self.ct.TransformPoint(
                self.ulx + self.geotransform[1] * self.xsize,
                self.uly + self.geotransform[5] * self.ysize)
            (self.lrx, self.lry) = (lr[0], lr[1])
            self.topbottom = (self.geotransform[5] < 0)
            self.description = fg.GetDescription()

            ct = fh.GetRasterBand(1).GetRasterColorTable()
            if ct is not None:
                self.ct = ct.Clone()
            else:
                self.ct = None

            self.check()
            verbprint(filename + " opened with driver: ")
            verbprint(" " * 4 + self.driver + " (" + self.sdriver + ")")
            if verblog():
                self.report()
예제 #5
0
 def createoutput(self):
     verbprint("checking if data directory %s exists" % self.info.dirname)
     if os.path.lexists(self.info.dirname):
         if self.info.force:
             if os.path.isdir(self.info.dirname):
                 verbprint("deleting directory: "+self.info.dirname)
                 shutil.rmtree(self.info.dirname)
             else:
                 verprint("deleting file: "+self.info.dirname)
                 os.remove(self.info.dirname)
         else:
             raise Exception("A file or directory exists at "+
                             self.info.dirname+".  Use -f to overwrite.")
     verbprint("creating output directory: "+self.info.dirname)
     os.mkdir(self.info.dirname)
     self.info.write(self.info.dirname)
예제 #6
0
        def __init__(self, filename):
            """
            Initialize file_info from filename

            """

            verbprint("Opening file: "+filename)
            fg=gdal.Open(filename)
            fh=fg
            if fg is None:
                raise IOError("file: "+filename+" cannot be found or opened by GDAL")
    
            self.filename = filename
            self.driver = fg.GetDriver().LongName
            self.sdriver = fg.GetDriver().ShortName
            self.bands = fh.RasterCount
            self.xsize = fh.RasterXSize
            self.ysize = fh.RasterYSize
            self.band_type = fh.GetRasterBand(1).DataType
            self.projection = fh.GetProjection()
            self.geotransform = fh.GetGeoTransform()
            self.spatialreference=osr.SpatialReference(self.projection)
            sr=self.spatialreference
            self.ct=osr.CoordinateTransformation(sr,sr.CloneGeogCS())
            #print self.ct.TransformPoint(self.geotransform[0],self.geotransform[3])
            ul = self.ct.TransformPoint(self.geotransform[0],self.geotransform[3])
            (self.ulx,self.uly) = (ul[0],ul[1])
            self.xres = abs(self.geotransform[1])
            self.yres = abs(self.geotransform[5])
            lr = self.ct.TransformPoint(self.ulx + self.geotransform[1] * self.xsize,
                                        self.uly + self.geotransform[5] * self.ysize)
            (self.lrx,self.lry) = (lr[0],lr[1]) 
            self.topbottom=(self.geotransform[5]<0)
            self.description=fg.GetDescription()
    
            ct = fh.GetRasterBand(1).GetRasterColorTable()
            if ct is not None:
                self.ct = ct.Clone()
            else:
                self.ct = None

            self.check()
            verbprint(filename+" opened with driver: ")
            verbprint(" "*4+self.driver+" ("+self.sdriver+")")
            if verblog():
                self.report()
예제 #7
0
 def write(self,d):
     verbprint("creating geogrid header file: "+os.path.join(d,"index"))
     self.check()
     n=self.sourcedata.getsize()
     if n[0] > 99999 or n[1] > 99999:
         raise Exception("Too many input data points to fit into "+\
                         "one geogrid data structure, and "+\
                         "splitting them is not yet implemented")
     f=open(os.path.join(d,"index"),"w")
     f.write("projection="+self.projection+"\n")
     if self.projection != "regular_ll":
         f.write("truelat1=%f\n" % self.truelat1)
         f.write("truelat2=%f\n" % self.truelat2)
         f.write("stdlon=%f\n" % self.stdlon)
     if self.continuous:
         s="continuous"
         f.write("type="+s+"\n")
     else:
         s="categorical"
         f.write("type="+s+"\n")
         catmin=1
         f.write("category_min=%i\n" % catmin)
         catmax=self.maxcategories
         f.write("category_max=%i\n" % catmax)
     
     if self.units is None:
         if self.continuous:
             if not self.script:
                 print textwrap.fill("WARNING: You didn't specify any units, and I can't find "+ \
                                     "it in the data set.  Substituting \"meters\"",80)
             s="meters"
         else:
             s="category"
     else:
         s=self.units
     f.write('units="'+s+'"\n')
     if self.desc is None:
         self.desc=self.sourcedata.getdescription()
     f.write('description="'+self.desc+'"\n')
     r=self.sourcedata.getresolution()
     f.write("dx="+str(r[0])+"\n")
     f.write("dy="+str(r[1])+"\n")
     if self.sourcedata.gettopbottom():
         n=n[1]
         s="top_bottom"
     else:
         n=1
         s="bottom_top"
         
     r=self.sourcedata.getfirstcoord()
     f.write("known_x=1\n")
     f.write("known_y=%i\n" % n)
     f.write("known_lon=%f\n" % r[0])
     f.write("known_lat=%f\n" % r[1])
     f.write("wordsize=%i\n" % int(self.wordsize))
     f.write("tile_bdr=%i\n" % self.halo)
     f.write("missing_value=%i\n" % self.missing)
     f.write("scale_factor=%f\n" % self.scale)
     f.write("row_order=%s\n" % s)
     f.write("endian=%s\n" % self.endian)
     f.write("tile_x=%i\n" % self.nxtile)
     f.write("tile_y=%i\n" % self.nytile)
     f.write("tile_z=%i\n" % self.nztile)
     if self.signed:
         s="yes"
     else:
         s="no"
     f.write("signed=%s\n" % s)
     f.close()