コード例 #1
0
    def test_px2lonlat_gt(self):
        lon_array = np.array([self.col])
        lat_array = np.array([self.row])
        lon, lat = gt.px2lonlat_gt(self.img, lon_array, lat_array,
                                   self.lon_min, self.lat_min, self.lon_max,
                                   self.lat_max)

        assert self.lat == lat[0]
        assert self.lon == lon[0]
コード例 #2
0
ファイル: test_geotiff.py プロジェクト: TUW-GEO/poets
    def test_px2lonlat_gt(self):
        lon_array = np.array([self.col])
        lat_array = np.array([self.row])
        lon, lat = gt.px2lonlat_gt(self.img, lon_array, lat_array,
                                         self.lon_min, self.lat_min,
                                         self.lon_max, self.lat_max)

        assert self.lat == lat[0]
        assert self.lon == lon[0]
コード例 #3
0
def bbox_img(source_file, region, fileExtension, shapefile=None):
    """
    Clips bounding box out of image file and returns data as numpy.ndarray

    Parameters
    ----------
    source_file : str
        Path to source file.
    region : str
        Identifier of the region in the shapefile. If the default shapefile is
        used, this would be the FIPS country code.
    fileExtension : str
        Filetype (e.g. png, tif).
    shapefile : str, optional
        Path to shape file, uses "world country admin boundary shapefile" by
        default.

    Returns
    -------
    data : dict of numpy.arrays
        Clipped image (grey values).
    lon_new : numpy.array
        Longitudes of the clipped image.
    lat_new : numpy.array
        Latitudes of the clipped image.
    timestamp : datetime.date
        Timestamp of the image.
    metadata : dict of strings
        Metadata from source netCDF file.
    """

    orig_img = Image.open(source_file)

    lon_min_src, lat_min_src, lon_max_src, lat_max_src = \
        get_layer_extent(source_file)

    if region == 'global':
        lon_min = -180
        lon_max = 180
        lat_min = -90
        lat_max = 90
    else:
        shp = Shape(region, shapefile)
        lon_min = shp.bbox[0]
        lon_max = shp.bbox[2]
        lat_min = shp.bbox[1]
        lat_max = shp.bbox[3]

    d = lon_max - lon_min

    # countries that cross the international dateline (maybe more!)
    if region in ['NZ', 'RS', 'US']:
        lon_min, lon_max = dateline_country(region)

    # get 2 pairs of points (upper left, lower right of bbox)
    if d > 350 and region not in ['AY', 'global']:
        if fileExtension in ['.tif', '.tiff', '.TIF', '.TIFF']:
            if (round(lon_max_src - lon_min_src) == 360.0
                    and round(lat_max_src - lat_min_src) == 180.0):
                orig_img = rearrange_img(orig_img)
                row_min, col_min = lonlat2px_rearr(orig_img, lon_min, lat_max)
                row_max, col_max = lonlat2px_rearr(orig_img, lon_max, lat_min)

                img = orig_img.crop(
                    (int(math.floor(col_min)), int(math.floor(row_min)),
                     int(math.ceil(col_max)), int(math.ceil(row_max))))
            else:
                print 'Rearranging is only possible for global imagefiles.'
                return
        else:
            orig_img = rearrange_img(orig_img)
            row_min, col_min = lonlat2px_rearr(orig_img, lon_min, lat_max)
            row_max, col_max = lonlat2px_rearr(orig_img, lon_max, lat_min)

            img = orig_img.crop(
                (int(math.floor(col_min)), int(math.floor(row_min)),
                 int(math.ceil(col_max)), int(math.ceil(row_max))))

    elif fileExtension in ['.tif', '.tiff', '.TIF', '.TIFF']:
        row_min, col_min = lonlat2px_gt(orig_img, lon_min, lat_max,
                                        lon_min_src, lat_min_src, lon_max_src,
                                        lat_max_src)
        row_max, col_max = lonlat2px_gt(orig_img, lon_max, lat_min,
                                        lon_min_src, lat_min_src, lon_max_src,
                                        lat_max_src)

        # crop image
        img = orig_img.crop(
            (int(math.floor(col_min)), int(math.floor(row_min)),
             int(math.ceil(col_max)), int(math.ceil(row_max))))
    else:
        row_min, col_min = lonlat2px(orig_img, lon_min, lat_max)
        row_max, col_max = lonlat2px(orig_img, lon_max, lat_min)

        # crop image
        img = orig_img.crop(
            (int(math.floor(col_min)), int(math.floor(row_min)),
             int(math.ceil(col_max)), int(math.ceil(row_max))))

    # get data values from image
    data = {'dataset': np.array(img)}

    # lon_new, lat_new
    lon_px = np.arange(int(math.floor(col_min)), int(math.ceil(col_max)))
    lat_px = np.arange(int(math.floor(row_min)), int(math.ceil(row_max)))

    if region in ['NZ', 'RS', 'US']:
        lon_new, lat_new = px2lonlat_rearr(orig_img, lon_px, lat_px)
    elif fileExtension in ['.tif', '.tiff', '.TIF', '.TIFF']:
        lon_new, lat_new = px2lonlat_gt(orig_img, lon_px, lat_px, lon_min_src,
                                        lat_min_src, lon_max_src, lat_max_src)
    else:
        lon_new, lat_new = px2lonlat(orig_img, lon_px, lat_px)

    # timestamp
    timestamp = None

    # metadata
    metadata = None

    # move coordinates to pixel center
    if lon_new.size > 2:
        lon_new += (lon_new[1] - lon_new[0]) / 2
    if lat_new.size > 2:
        lat_new += (lat_new[1] - lat_new[0]) / 2

    return data, lon_new, lat_new, timestamp, metadata
コード例 #4
0
ファイル: imagefile.py プロジェクト: TUW-GEO/poets
def bbox_img(source_file, region, fileExtension, shapefile=None):
    """
    Clips bounding box out of image file and returns data as numpy.ndarray

    Parameters
    ----------
    source_file : str
        Path to source file.
    region : str
        Identifier of the region in the shapefile. If the default shapefile is
        used, this would be the FIPS country code.
    fileExtension : str
        Filetype (e.g. png, tif).
    shapefile : str, optional
        Path to shape file, uses "world country admin boundary shapefile" by
        default.

    Returns
    -------
    data : dict of numpy.arrays
        Clipped image (grey values).
    lon_new : numpy.array
        Longitudes of the clipped image.
    lat_new : numpy.array
        Latitudes of the clipped image.
    timestamp : datetime.date
        Timestamp of the image.
    metadata : dict of strings
        Metadata from source netCDF file.
    """

    orig_img = Image.open(source_file)

    lon_min_src, lat_min_src, lon_max_src, lat_max_src = \
        get_layer_extent(source_file)

    if region == 'global':
        lon_min = -180
        lon_max = 180
        lat_min = -90
        lat_max = 90
    else:
        shp = Shape(region, shapefile)
        lon_min = shp.bbox[0]
        lon_max = shp.bbox[2]
        lat_min = shp.bbox[1]
        lat_max = shp.bbox[3]

    d = lon_max - lon_min

    # countries that cross the international dateline (maybe more!)
    if region in ['NZ', 'RS', 'US']:
        lon_min, lon_max = dateline_country(region)

    # get 2 pairs of points (upper left, lower right of bbox)
    if d > 350 and region not in ['AY', 'global']:
        if fileExtension in ['.tif', '.tiff', '.TIF', '.TIFF']:
            if (round(lon_max_src - lon_min_src) == 360.0 and
                round(lat_max_src - lat_min_src) == 180.0):
                orig_img = rearrange_img(orig_img)
                row_min, col_min = lonlat2px_rearr(orig_img, lon_min, lat_max)
                row_max, col_max = lonlat2px_rearr(orig_img, lon_max, lat_min)

                img = orig_img.crop((int(math.floor(col_min)),
                                     int(math.floor(row_min)),
                                     int(math.ceil(col_max)),
                                     int(math.ceil(row_max))))
            else:
                print 'Rearranging is only possible for global imagefiles.'
                return
        else:
            orig_img = rearrange_img(orig_img)
            row_min, col_min = lonlat2px_rearr(orig_img, lon_min, lat_max)
            row_max, col_max = lonlat2px_rearr(orig_img, lon_max, lat_min)

            img = orig_img.crop((int(math.floor(col_min)),
                                 int(math.floor(row_min)),
                                 int(math.ceil(col_max)),
                                 int(math.ceil(row_max))))

    elif fileExtension in ['.tif', '.tiff', '.TIF', '.TIFF']:
        row_min, col_min = lonlat2px_gt(orig_img, lon_min, lat_max,
                                        lon_min_src, lat_min_src, lon_max_src,
                                        lat_max_src)
        row_max, col_max = lonlat2px_gt(orig_img, lon_max, lat_min,
                                        lon_min_src, lat_min_src, lon_max_src,
                                        lat_max_src)

        # crop image
        img = orig_img.crop((int(math.floor(col_min)),
                             int(math.floor(row_min)),
                             int(math.ceil(col_max)),
                             int(math.ceil(row_max))))
    else:
        row_min, col_min = lonlat2px(orig_img, lon_min, lat_max)
        row_max, col_max = lonlat2px(orig_img, lon_max, lat_min)

        # crop image
        img = orig_img.crop((int(math.floor(col_min)),
                             int(math.floor(row_min)),
                             int(math.ceil(col_max)),
                             int(math.ceil(row_max))))

    # get data values from image
    data = {'dataset': np.array(img)}

    # lon_new, lat_new
    lon_px = np.arange(int(math.floor(col_min)), int(math.ceil(col_max)))
    lat_px = np.arange(int(math.floor(row_min)), int(math.ceil(row_max)))

    if region in ['NZ', 'RS', 'US']:
        lon_new, lat_new = px2lonlat_rearr(orig_img, lon_px, lat_px)
    elif fileExtension in ['.tif', '.tiff', '.TIF', '.TIFF']:
        lon_new, lat_new = px2lonlat_gt(orig_img, lon_px, lat_px, lon_min_src,
                                        lat_min_src, lon_max_src, lat_max_src)
    else:
        lon_new, lat_new = px2lonlat(orig_img, lon_px, lat_px)

    # timestamp
    timestamp = None

    # metadata
    metadata = None

    # move coordinates to pixel center
    if lon_new.size > 2:
        lon_new += (lon_new[1] - lon_new[0]) / 2
    if lat_new.size > 2:
        lat_new += (lat_new[1] - lat_new[0]) / 2

    return data, lon_new, lat_new, timestamp, metadata