From 350d8674d3090f1fc93ce16cf638b327e684b04c Mon Sep 17 00:00:00 2001 From: Carles Tena Date: Tue, 23 Jul 2019 16:15:17 +0200 Subject: [PATCH 1/2] -IoRaster: drop old function --- hermesv3_bu/io_server/io_raster.py | 78 ------------------------------ 1 file changed, 78 deletions(-) diff --git a/hermesv3_bu/io_server/io_raster.py b/hermesv3_bu/io_server/io_raster.py index d004468..73dc937 100755 --- a/hermesv3_bu/io_server/io_raster.py +++ b/hermesv3_bu/io_server/io_raster.py @@ -198,84 +198,6 @@ class IoRaster(IoServer): return gdf - def to_shapefile_serie_old(self, raster_path, out_path=None, write=False, crs=None, nodata=0): - """ - - :param raster_path: - :param out_path: - :param write: - :param crs: - :param rank: - :param nodata: - :return: - """ - - if out_path is None or not os.path.exists(out_path): - ds = rasterio.open(raster_path) - print 1 - grid_info = ds.transform - # TODO remove when new version will be installed - if not rasterio.__version__ == '1.0.21': - lons = np.arange(ds.width) * grid_info[1] + grid_info[0] - lats = np.arange(ds.height) * grid_info[5] + grid_info[3] - else: - lons = np.arange(ds.width) * grid_info[0] + grid_info[2] - lats = np.arange(ds.height) * grid_info[4] + grid_info[5] - print 2 - # 1D to 2D - c_lats = np.array([lats] * len(lons)).T.flatten() - c_lons = np.array([lons] * len(lats)).flatten() - print 3 - del lons, lats - if not rasterio.__version__ == '1.0.21': - b_lons = self.create_bounds(c_lons, grid_info[1], number_vertices=4) + grid_info[1] / 2 - b_lats = self.create_bounds(c_lats, grid_info[1], number_vertices=4, inverse=True) + grid_info[5] / 2 - else: - b_lons = self.create_bounds(c_lons, grid_info[0], number_vertices=4) + grid_info[0]/2 - b_lats = self.create_bounds(c_lats, grid_info[4], number_vertices=4, inverse=True) + grid_info[4]/2 - print 4 - df_lats = pd.DataFrame(b_lats[0], columns=['b_lat_1', 'b_lat_2', 'b_lat_3', 'b_lat_4']) - df_lons = pd.DataFrame(b_lons[0], columns=['b_lon_1', 'b_lon_2', 'b_lon_3', 'b_lon_4']) - print 5 - df = pd.concat([df_lats, df_lons], axis=1) - print 6 - print df - exit() - del df_lats, df_lons, b_lats, b_lons - - df['p1'] = zip(df.b_lon_1, df.b_lat_1) - del df['b_lat_1'], df['b_lon_1'] - df['p2'] = zip(df.b_lon_2, df.b_lat_2) - del df['b_lat_2'], df['b_lon_2'] - df['p3'] = zip(df.b_lon_3, df.b_lat_3) - del df['b_lat_3'], df['b_lon_3'] - df['p4'] = zip(df.b_lon_4, df.b_lat_4) - del df['b_lat_4'], df['b_lon_4'] - print 7 - - list_points = df.values - print 8 - - del df['p1'], df['p2'], df['p3'], df['p4'] - data = ds.read(1).flatten() - geometry = [Polygon(list(points)) for points in list_points] - gdf = gpd.GeoDataFrame(data, columns=['data'], crs=ds.crs, geometry=geometry) - gdf.loc[:, 'CELL_ID'] = xrange(len(gdf)) - gdf = gdf[gdf['data'] != nodata] - - if crs is not None: - gdf = gdf.to_crs(crs) - - if write: - if not os.path.exists(os.path.dirname(out_path)): - os.makedirs(os.path.dirname(out_path)) - gdf.to_file(out_path) - - else: - gdf = gpd.read_file(out_path) - - return gdf - def to_shapefile_serie(self, raster_path, out_path=None, write=False, crs=None, nodata=0): """ -- GitLab From 7331bf50c5d291458c710c4369aeb443cf1233e2 Mon Sep 17 00:00:00 2001 From: Carles Tena Date: Tue, 23 Jul 2019 16:22:27 +0200 Subject: [PATCH 2/2] -Grids: Changed numpy.arange by numpy.linspace --- hermesv3_bu/grids/grid_lcc.py | 10 ++++++---- hermesv3_bu/grids/grid_mercator.py | 10 ++++++---- hermesv3_bu/grids/grid_rotated.py | 12 ++++++------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/hermesv3_bu/grids/grid_lcc.py b/hermesv3_bu/grids/grid_lcc.py index c1a3cfe..c5ae6a0 100755 --- a/hermesv3_bu/grids/grid_lcc.py +++ b/hermesv3_bu/grids/grid_lcc.py @@ -109,10 +109,12 @@ class LccGrid(Grid): """ spent_time = timeit.default_timer() # Create a regular grid in metres (Two 1D arrays) - self.x = np.arange(self.attributes['x_0'], self.attributes['x_0'] + self.attributes['inc_x'] * - self.attributes['nx'], self.attributes['inc_x'], dtype=np.float) - self.y = np.arange(self.attributes['y_0'], self.attributes['y_0'] + self.attributes['inc_y'] * - self.attributes['ny'], self.attributes['inc_y'], dtype=np.float) + self.x = np.linspace(self.attributes['x_0'], self.attributes['x_0'] + + (self.attributes['inc_x'] * (self.attributes['nx'] - 1)), self.attributes['nx'], + dtype=np.float) + self.y = np.linspace(self.attributes['y_0'], self.attributes['y_0'] + + (self.attributes['inc_y'] * (self.attributes['ny'] - 1)), self.attributes['ny'], + dtype=np.float) # 1D to 2D x = np.array([self.x] * len(self.y)) diff --git a/hermesv3_bu/grids/grid_mercator.py b/hermesv3_bu/grids/grid_mercator.py index daae000..2c57d65 100755 --- a/hermesv3_bu/grids/grid_mercator.py +++ b/hermesv3_bu/grids/grid_mercator.py @@ -98,10 +98,12 @@ class MercatorGrid(Grid): """ spent_time = timeit.default_timer() # Create a regular grid in metres (Two 1D arrays) - self.x = np.arange(self.attributes['x_0'], self.attributes['x_0'] + self.attributes['inc_x'] * - self.attributes['nx'], self.attributes['inc_x'], dtype=np.float) - self.y = np.arange(self.attributes['y_0'], self.attributes['y_0'] + self.attributes['inc_y'] * - self.attributes['ny'], self.attributes['inc_y'], dtype=np.float) + self.x = np.linspace(self.attributes['x_0'], self.attributes['x_0'] + + (self.attributes['inc_x'] * (self.attributes['nx'] - 1)), self.attributes['nx'], + dtype=np.float) + self.y = np.arange(self.attributes['y_0'], self.attributes['y_0'] + + (self.attributes['inc_y'] * (self.attributes['ny'] - 1)), self.attributes['ny'], + dtype=np.float) # 1D to 2D x = np.array([self.x] * len(self.y)) diff --git a/hermesv3_bu/grids/grid_rotated.py b/hermesv3_bu/grids/grid_rotated.py index af16e8e..3ddf526 100755 --- a/hermesv3_bu/grids/grid_rotated.py +++ b/hermesv3_bu/grids/grid_rotated.py @@ -55,12 +55,12 @@ class RotatedGrid(Grid): """ spent_time = timeit.default_timer() - center_latitudes = np.arange(self.attributes['south_boundary'], self.attributes['south_boundary'] + - (self.attributes['n_lat'] * self.attributes['inc_rlat']), - self.attributes['inc_rlat'], dtype=np.float) - center_longitudes = np.arange(self.attributes['west_boundary'], self.attributes['west_boundary'] + - (self.attributes['n_lon'] * self.attributes['inc_rlon']), - self.attributes['inc_rlon'], dtype=np.float) + center_latitudes = np.linspace(self.attributes['south_boundary'], self.attributes['south_boundary'] + + (self.attributes['inc_rlat'] * (self.attributes['n_lat'] - 1)), + self.attributes['n_lat'], dtype=np.float) + center_longitudes = np.linspace(self.attributes['west_boundary'], self.attributes['west_boundary'] + + (self.attributes['inc_rlon'] * (self.attributes['n_lon'] - 1)), + self.attributes['n_lon'], dtype=np.float) corner_latitudes = self.create_bounds(center_latitudes, self.attributes['inc_rlat'], number_vertices=4, inverse=True) -- GitLab