Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example - writing a GIS Tiff file to DSS. #54

Open
LukeWebbJacobs opened this issue Nov 10, 2023 · 4 comments
Open

Example - writing a GIS Tiff file to DSS. #54

LukeWebbJacobs opened this issue Nov 10, 2023 · 4 comments

Comments

@LukeWebbJacobs
Copy link

LukeWebbJacobs commented Nov 10, 2023

Hello, I have struggled to write a valid GIS tiff, to a dss file.

I had to leave disabled the 'raise_profile_error', because the values calculated by the pydsstools, appear to be calculated using the top left of the grid, instead of the bottom left.

The comment on the 25th of november here mentions this as well:
#31

The end result is that the raster, ends up shifted up, by the distance of the height of the raster If i provide values that pass your validation.

@LukeWebbJacobs
Copy link
Author

LukeWebbJacobs commented Nov 10, 2023

Is there a specific example how this should be done?

@LukeWebbJacobs
Copy link
Author

LukeWebbJacobs commented Nov 10, 2023

Working Example - Conversion from TIFF to DSS
Here is an mostly working example to convert a tiff to dss. Similar to your current example 10, writing spatial grid record.

But I have since noticed small projection inconsistancies so any specific example / help would be appreciated!.

Notice this line of code, Which i believe mostly works around the bug. (and causes the profile error to be raised) :
('opt_lower_left_y', ll_y - data.shape[0]),

    from pydsstools.heclib.dss.HecDss import Open
    from pydsstools.heclib.utils import gridInfo
    from pydsstools.core import (lower_left_xy_from_transform)
    import rasterio
    
    
    in_tiff = 'in_file.tif'
    out_dss_file = 'a_file.dss'
    
    with (Open(out_dss_file) as fid):

        # Build output path within dss
        pathname_out = 'a/b/c/d/e/f'

        # Load the raster
        with rasterio.open(in_tiff, 'r') as in_raster:
            # Get data as numpy array
            data = in_raster.read(1)

            # Create dss metadata dictionary
            ll_x, ll_y = lower_left_xy_from_transform(in_raster.transform, data.shape)
            crs_name = in_raster.crs.to_wkt().split('"')[1]

            grid_info = gridInfo()
            grid_info.update([('grid_type', 'specified-time'),
                              ('grid_crs', in_raster.crs.to_wkt()),
                              ('opt_crs_name', crs_name),
                              ('grid_transform', in_raster.transform),
                              ('data_type', 'per-cum'),
                              ('data_units', 'MM'),
                              ('opt_time_stamped', True),
                              ('opt_is_interval', True),

                              ('opt_lower_left_x', ll_x),
                              ('opt_lower_left_y', ll_y - data.shape[0]),
                              ],
                             )

            # Add file to output dss
            fid.put_grid(pathname_out, data, grid_info)

@LukeWebbJacobs LukeWebbJacobs changed the title Error writing a GIS Tiff file. Error writing a GIS Tiff file to DSS. Nov 10, 2023
@LukeWebbJacobs LukeWebbJacobs changed the title Error writing a GIS Tiff file to DSS. Example - writing a GIS Tiff file to DSS. Nov 10, 2023
@KmBase
Copy link

KmBase commented Dec 16, 2024

in grid.pyx , the function lower_left_xy_from_transform is :

def lower_left_xy_from_transform(transform,shape,cell_zero_xcoord=0,cell_zero_ycoord=0):
    cdef:
        float xmin,ymax
        float ymin # verbose to make calculation more clear
        int lower_left_x,lower_left_y
    cellsize = transform[0]
    cellsize_y = transform[4]
    rows,cols = shape
    xmin = transform[2] - cell_zero_xcoord
    ymax = transform[5] - cell_zero_ycoord
    ymin = ymax + rows * cellsize_y
    lower_left_x = <int>(floor(xmin/cellsize))
    lower_left_y = <int>(floor(ymax/cellsize))
    return (lower_left_x,lower_left_y)

why the value of lower_left_y is y_max? There is a source about the coordinate-reference-systems

For identification, each cell in the grid has a pair of integer indices (i, j) indicating the position, by cell count, of its southwest (or minimum-x, minimum-y) corner, relative to the UTM zone coordinate origin. To find the indices of the cell in which a point is located, find the point’s easting and northing in the projected coordinate system defined above, and calculates the indices with the following formulas.
i = floor( easting / cellsize )
j = floor( northing / cellsize )
where floor(x) is the largest integer less than or equal to x.

We know that transform[2] and transform[5] is the upper_left location. So the xmin and ymin are absolutely correct in the lower_left_xy_from_transform function.But why ymin is unused.

@KmBase
Copy link

KmBase commented Dec 16, 2024

By the way, I find that when x_cellsize is not equal to y_cellsize, the result will be far away from the result generated by votex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants