Skip to content

Commit

Permalink
This commit :
Browse files Browse the repository at this point in the history
1- Creates a csv file of tile id and coordinate in both systems when generate is called. This is very handy for the user to refer back to if they need to know where certain tiles are.
2- Add an optional dest_path argument to save_csv and save_shapefile to be saved even if generate is not called and no source tile exists.
  • Loading branch information
Mary-h86 committed Aug 6, 2024
1 parent 60abaab commit 222a983
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/tile2net/raster/grid.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import warnings
import os
import numpy as np
Expand Down Expand Up @@ -506,26 +508,28 @@ def create_grid_gdf(self):
gdf_grid = gdf_grid.reset_index(drop=True)
return gdf_grid

def save_shapefile(self):
def save_shapefile(self, dst_path: str = None) -> None:
"""
Saves the grid data in shapefile format
"""
dst_path = self.project.tiles.path
if not os.path.exists(dst_path):
os.makedirs(dst_path)
if not dst_path is None:
dst_path = self.project.tiles.path
if not os.path.exists(dst_path):
os.makedirs(dst_path)
poly = self._create_pseudo_tiles()
tileinfo_df = self._create_info_dict(df=True)
gdf_grid = gpd.GeoDataFrame(tileinfo_df, geometry=poly, crs=self.crs)
gdf_grid.to_file(
os.path.join(dst_path, f'{self.name}_{self.tile_size}_pseudo_tiles'))

def save_csv(self):
def save_csv(self, dst_path: str = None):
"""
Saves the grid data in CSV format without geometry info
"""
dst_path = self.project.tiles.path
if not os.path.exists(dst_path):
os.makedirs(dst_path)
if not dst_path:
dst_path = self.project.tiles.path
if not os.path.exists(dst_path):
os.makedirs(dst_path)
tileinfo_df = self._create_info_dict(df=True)
tileinfo_df.to_csv(os.path.join(dst_path, f'{self.name}_{self.tile_size}_info.csv'))

Expand Down
3 changes: 2 additions & 1 deletion src/tile2net/raster/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def __init__(
)

def __repr__(self):
if self.boundary_path != -1:
if self.boundary_path != "":
tiles_within = f"{(self.num_inside / self.num_tiles) * 100:.1f}"
return (
f"{self.name} Data Constructor. \nCoordinate reference system (CRS): {self.crs} \n"
Expand Down Expand Up @@ -813,6 +813,7 @@ def generate(self, step):
"""
self.stitch(step)
self.save_info_json(new_tstep=step)
self.save_csv()
logger.info(f"Dumping to {self.project.tiles.info}")
json.dump(
dict(self.project.structure),
Expand Down

0 comments on commit 222a983

Please sign in to comment.