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

[Feature] Write Geotiff Files #25

Open
KipCrossing opened this issue Jul 4, 2021 · 7 comments
Open

[Feature] Write Geotiff Files #25

KipCrossing opened this issue Jul 4, 2021 · 7 comments

Comments

@KipCrossing
Copy link
Owner

Writing data to a tiff file should be easy via the tifffile package

And writing the tags should also be straightforward using the tifftools package

The hard part is mapping the chosen CRS to the Geotiff tags correctly.

@KipCrossing KipCrossing mentioned this issue Jul 15, 2021
2 tasks
@HowcanoeWang
Copy link

HowcanoeWang commented Dec 14, 2022

Please refer to our package, it provide an alternative solution for making geotiff tags.
https://github.com/UTokyo-FieldPhenomics-Lab/EasyIDP/blob/ff687a6d3e27a0155d137019a15ba8461ce2222e/easyidp/geotiff.py#L351

But it only suitable for cutting a region from given geotiff (only modify the left-top corner & size for cropped region to save), unable to make a new geotiff and save.

@KipCrossing
Copy link
Owner Author

Please refer to our package, it provide an alternative solution for making geotiff tags. https://github.com/UTokyo-FieldPhenomics-Lab/EasyIDP/blob/ff687a6d3e27a0155d137019a15ba8461ce2222e/easyidp/geotiff.py#L351

But it only suitable for cutting a region from given geotiff (only modify the left-top corner & size for cropped region to save), unable to make a new geotiff and save.

Very nice! Thanks for the reference!

PS, I also work in agriculture. Would love to know more about the applications for your package. Send me an email if you can.

@HowcanoeWang
Copy link

Very nice! Thanks for the reference!

PS, I also work in agriculture. Would love to know more about the applications for your package. Send me an email if you can.

Please kindly check the documentation for more details.
https://easyidp.readthedocs.io/en/latest/index.html

@KipCrossing
Copy link
Owner Author

Sooooo... I just got a solution from ChatGTP:

import xarray as xr
import tifffile
import tifftools

# Load the data and create a tifffile object
data = xr.open_dataset('data.nc')
tiff = tifffile.TiffFile('output.tiff', mode='w')

# Extract the data and latitude and longitude coordinates from the xarray
data_array = data['data'].values
lats = data['lat'].values
lons = data['lon'].values

# Set the CRS
crs = 'EPSG:4326'

# Create the GeoTiff tags
tags = tifftools.create_geotiff_tags(data_array, crs, lats, lons)

# Add the tags to the tifffile object
tiff.append(data_array, tags)

# Close the tifffile object
tiff.close()

This script assumes that you have an xarray with data, latitude, and longitude coordinates stored in a file called 'data.nc' and that you want to save it as a geotiff with the CRS 'EPSG:4326'. It loads the xarray and extracts the data, latitude, and longitude coordinates. It then creates a tifffile object and uses the tifftools.create_geotiff_tags() function to create the GeoTiff tags for the data. The tags are then added to the tifffile object using the append() method, and the tifffile object is closed to save the geotiff.

Note that this script assumes that the latitude and longitude coordinates are stored as 1D arrays, but if they are stored as 2D arrays, you can use the create_geotiff_tags_from_grid() function instead of the create_geotiff_tags() function.

I haven't tested it out yet, But it's good to know. Obv I don't need the xarray stuff.

@HowcanoeWang
Copy link

I have digged the source code of tifftools, no api or function named create_geotiff_tags, and the only close one is write_tag_data, but seems not expected.

def write_tag_data(dest, src, offsets, lengths, srclen):
    """
    Copy data from a source tiff to a destination tiff, return a list of
    offsets where data was written.
    :param dest: the destination file, opened to the location to write.
    :param src: the source file.
    :param offsets: an array of offsets where data will be copied from.
    :param lengths: an array of lengths to copy from each offset.
    :param srclen: the length of the source file.
    :return: the offsets in the destination file corresponding to the data
        copied.
    """

@KipCrossing
Copy link
Owner Author

Looks like this might be a major problem with chatGTP. That is; when the solution is not obvious, it will make up a solution based on features that don't exist.

@waleedgeo
Copy link

Hi, previously, I have been using rasterio-based implementation of writing geotiffs (check https://github.com/rasterio/rasterio/blob/main/examples/reproject.py#L44-L55). If that's what you are looking for, maybe I can help in implementing the write geotiff files feature.

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

3 participants