[Python] Problem trying to crop image using GeoJson, Python and rasterio

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to crop a piece of image from a satellite picture using rasterio, json, GeoJson and Python. But I'm facing the following issue. Does anyone has an idea how to solve it?

    CODE

    import rasterio
    from rasterio.mask import mask
    import json

    #opens json file
    myGeoms_file = open('floripa.json')
    myGeoms_json = json.load(myGeoms_file)
    myGeoms_file.close()

    #parsing my json to a format appropriate for rasterio
    coords = myGeoms_json['coordinates'][0]
    parsedJson = [{
    'type': 'Polygon',
    'coordinates': [[]]
    }]
    for i in coords:
    newCoord = (i[0],i[1])
    parsedJson[0]['coordinates'][0].append(newCoord)

    #starting to image cropping process
    with rasterio.open("CB04.tif") as src:
    out_image, out_transform = mask(src, parsedJson, crop=True)
    out_meta = src.meta.copy()


    Issue:

    [{'type': 'Polygon', 'coordinates': [[(-48.52309226989746, -27.596847566108902), (-48.52386474609375, -27.59707576282468), (-48.526482582092285, -27.60110715979902), (-48.52652549743652, -27.601943846249203), (-48.52382183074951, -27.604453867271626), (-48.52249145507812, -27.604263716176668), (-48.52184772491455, -27.604453867271626), (-48.51961612701416, -27.607001860107363), (-48.516783714294434, -27.60441583707903), (-48.51738452911376, -27.602248094283674), (-48.51674079895019, -27.599890149923038), (-48.51515293121338, -27.594717707186042), (-48.515281677246094, -27.594033100886566), (-48.51575374603271, -27.593995067077895), (-48.51644039154053, -27.59418523598927), (-48.52038860321045, -27.59791248005127), (-48.52163314819336, -27.597532154830994), (-48.52309226989746, -27.596847566108902)]]}]


    WindowError Traceback (most recent call last)
    /usr/local/lib/python3.6/dist-packages/rasterio/mask.py in raster_geometry_mask(dataset, shapes, all_touched, invert, crop, pad, pad_width)
    83 window = geometry_window(dataset, shapes, north_up=north_up, rotated=rotated,
    ---> 84 pad_x=pad_x, pad_y=pad_y)
    85

    6 frames
    WindowError: windows do not intersect


    During handling of the above exception, another exception occurred:

    ValueError Traceback (most recent call last)
    /usr/local/lib/python3.6/dist-packages/rasterio/mask.py in raster_geometry_mask(dataset, shapes, all_touched, invert, crop, pad, pad_width)
    88 # depending on value of crop
    89 if crop:
    ---> 90 raise ValueError('Input shapes do not overlap raster.')
    91 else:
    92 warnings.warn('shapes are outside bounds of raster. '

    ValueError: Input shapes do not overlap raster.

    Continue reading...

Compartilhe esta Página