Esempio n. 1
0
    def __init__(self,
                 projparams,
                 llcrnrlon,
                 llcrnrlat,
                 urcrnrlon,
                 urcrnrlat,
                 urcrnrislatlon=True):
        """
 initialize a Proj class instance.

 Input 'projparams' is a dictionary containing proj map
 projection control parameter key/value pairs.
 See the proj documentation (http://www.remotesensing.org/proj/)
 for details.

 llcrnrlon,llcrnrlat are lon and lat (in degrees) of lower left hand corner
 of projection region.

 urcrnrlon,urcrnrlat are lon and lat (in degrees) of upper right hand corner
 of projection region if urcrnrislatlon=True (default). Otherwise, 
 urcrnrlon,urcrnrlat are x,y in projection coordinates (units meters), 
 assuming the lower left corner is x=0,y=0.
        """
        # set units to meters.
        if not projparams.has_key('units'):
            projparams['units'] = 'm'
        elif projparams['units'] != 'm':
            print 'resetting units to meters ...'
            projparams['units'] = 'm'
        self.projparams = projparams
        # make sure proj parameter specified.
        # (no other checking done in proj parameters)
        if 'proj' not in projparams.keys():
            raise KeyError, "need to specify proj parameter"
        if 'R' not in projparams.keys() and 'a' and 'b' not in projparams.keys(
        ):
            raise KeyError, "need to specify R (perfect sphere radius), or a and b (major and minor sphere radii)"
        # build proj string.
        self.proj4cmd = []
        for key, value in map(None, projparams.keys(), projparams.values()):
            if key == 'x_0' or key == 'y_0':
                # x_0 and y_0 are set based on llcrnrlon,llcrnrlat.
                pass
            else:
                self.proj4cmd.append('+' + key + "=" + str(value) + ' ')
        self.projection = projparams['proj']
        # rmajor is the semi-major axis.
        # rminor is the semi-minor axis.
        # esq is eccentricity squared.
        try:
            self.rmajor = projparams['a']
            self.rminor = projparams['b']
        except:
            self.rmajor = projparams['R']
            self.rminor = self.rmajor
        if self.rmajor == self.rminor:
            self.ellipsoid = False
        else:
            self.ellipsoid = True
        self.flattening = (self.rmajor - self.rminor) / self.rmajor
        self.esq = (self.rmajor**2 - self.rminor**2) / self.rmajor**2
        self.llcrnrlon = llcrnrlon
        self.llcrnrlat = llcrnrlat
        if self.projection not in ['cyl', 'ortho', 'moll', 'robin']:
            self._proj4 = proj4.Proj(''.join(self.proj4cmd))
            llcrnrx, llcrnry = self._fwd(llcrnrlon, llcrnrlat)
        elif self.projection == 'cyl':
            llcrnrx = llcrnrlon
            llcrnry = llcrnrlat
        elif self.projection == 'ortho':
            self._proj4 = proj4.Proj(''.join(self.proj4cmd))
            llcrnrx = -self.rmajor
            llcrnry = -self.rmajor
            urcrnrx = -llcrnrx
            urcrnry = -llcrnry
        elif self.projection == 'moll' or self.projection == 'robin':
            self._proj4 = proj4.Proj(''.join(self.proj4cmd))
            xtmp, urcrnry = self._fwd(projparams['lon_0'], 90.)
            urcrnrx, xtmp = self._fwd(projparams['lon_0'] + 180., 0)
            llcrnrx = -urcrnrx
            llcrnry = -urcrnry
        # compute x_0, y_0 so ll corner of domain is x=0,y=0.
        # note that for 'cyl' x,y == lon,lat
        self.proj4cmd.append("+x_0=" + str(-llcrnrx) + ' ')
        self.projparams['x_0'] = -llcrnrx
        self.proj4cmd.append("+y_0=" + str(-llcrnry) + ' ')
        self.projparams['y_0'] = -llcrnry
        # reset with x_0, y_0.
        if self.projection != 'cyl':
            self._proj4 = proj4.Proj(''.join(self.proj4cmd))
            llcrnry = 0.
            llcrnrx = 0.
        else:
            llcrnrx = llcrnrlon
            llcrnry = llcrnrlat
        if urcrnrislatlon:
            self.urcrnrlon = urcrnrlon
            self.urcrnrlat = urcrnrlat
            if self.projection not in ['ortho', 'moll', 'robin']:
                urcrnrx, urcrnry = self._fwd(urcrnrlon, urcrnrlat)
            elif self.projection == 'ortho':
                urcrnrx = 2. * self.rmajor
                urcrnry = 2. * self.rmajor
            elif self.projection == 'moll' or self.projection == 'robin':
                xtmp, urcrnry = self._fwd(projparams['lon_0'], 90.)
                urcrnrx, xtmp = self._fwd(projparams['lon_0'] + 180., 0)
        else:
            urcrnrx = urcrnrlon
            urcrnry = urcrnrlat
            urcrnrlon, urcrnrlat = self._inv(urcrnrx, urcrnry)
            self.urcrnrlon = urcrnrlon
            self.urcrnrlat = urcrnrlat
        # corners of domain.
        self.llcrnrx = llcrnrx
        self.llcrnry = llcrnry
        self.urcrnrx = urcrnrx
        self.urcrnry = urcrnry
        if urcrnrx > llcrnrx:
            self.xmin = llcrnrx
            self.xmax = urcrnrx
        else:
            self.xmax = llcrnrx
            self.xmin = urcrnrx
        if urcrnry > llcrnry:
            self.ymin = llcrnry
            self.ymax = urcrnry
        else:
            self.ymax = llcrnry
            self.ymin = urcrnry
Esempio n. 2
0
    def __init__(self,
                 projparams,
                 llcrnrlon,
                 llcrnrlat,
                 urcrnrlon,
                 urcrnrlat,
                 urcrnrislatlon=True):
        """
 initialize a Proj class instance.

 Input 'projparams' is a dictionary containing proj map
 projection control parameter key/value pairs.
 See the proj documentation (http://www.remotesensing.org/proj/)
 for details.

 llcrnrlon,llcrnrlat are lon and lat (in degrees) of lower left hand corner
 of projection region.

 urcrnrlon,urcrnrlat are lon and lat (in degrees) of upper right hand corner
 of projection region if urcrnrislatlon=True (default). Otherwise, 
 urcrnrlon,urcrnrlat are x,y in projection coordinates (units meters), 
 assuming the lower left corner is x=0,y=0.
        """
        # set units to meters.
        if not projparams.has_key('units'):
            projparams['units'] = 'm'
        elif projparams['units'] != 'm':
            print 'resetting units to meters ...'
            projparams['units'] = 'm'
        self.projparams = projparams
        # make sure proj parameter specified.
        # (no other checking done in proj parameters)
        if 'proj' not in projparams.keys():
            raise KeyError, "need to specify proj parameter"
        # build proj string.
        self.proj4md = []
        for key, value in map(None, projparams.keys(), projparams.values()):
            if key == 'x_0' or key == 'y_0':
                # x_0 and y_0 are set based on llcrnrlon,llcrnrlat.
                pass
            else:
                self.proj4md.append('+' + key + "=" + str(value) + ' ')
        self.projection = projparams['proj']
        self.llcrnrlon = llcrnrlon
        self.llcrnrlat = llcrnrlat
        if self.projection != 'cyl':
            self._proj4 = proj4.Proj(''.join(self.proj4md))
        llcrnrx, llcrnry = self._fwd(llcrnrlon, llcrnrlat)
        # compute x_0, y_0 so ll corner of domain is x=0,y=0.
        # note that for 'cyl' x,y == lon,lat
        # and for 'merc' x,y == lon,y
        self.proj4md.append("+x_0=" + str(-llcrnrx) + ' ')
        self.projparams['x_0'] = -llcrnrx
        self.proj4md.append("+y_0=" + str(-llcrnry) + ' ')
        self.projparams['y_0'] = -llcrnry
        # reset with x_0, y_0.
        if self.projection != 'cyl':
            self._proj4 = proj4.Proj(''.join(self.proj4md))
            llcrnry = 0.
            if self.projection == 'merc':
                llcrnrx = llcrnrlon
            else:
                llcrnrx = 0.
        else:
            llcrnrx = llcrnrlon
            llcrnry = llcrnrlat
        if urcrnrislatlon:
            self.urcrnrlon = urcrnrlon
            self.urcrnrlat = urcrnrlat
            urcrnrx, urcrnry = self._fwd(urcrnrlon, urcrnrlat)
        else:
            urcrnrx = urcrnrlon
            urcrnry = urcrnrlat
            urcrnrlon, urcrnrlat = self._inv(urcrnrx, urcrnry)
            self.urcrnrlon = urcrnrlon
            self.urcrnrlat = urcrnrlat
        # corners of domain.
        self.llcrnrx = llcrnrx
        self.llcrnry = llcrnry
        self.urcrnrx = urcrnrx
        self.urcrnry = urcrnry
        if urcrnrx > llcrnrx:
            self.xmin = llcrnrx
            self.xmax = urcrnrx
        else:
            self.xmax = llcrnrx
            self.xmin = urcrnrx
        if urcrnry > llcrnry:
            self.ymin = llcrnry
            self.ymax = urcrnry
        else:
            self.ymax = llcrnry
            self.ymin = urcrnry