I have a set of satellite image tiles from Bing Maps (info here). The tiles' names specify tile coordinates within the tile system and zoom, e.g. 156608-142775-18.jpg.

An example set of tiles:

['156608-142775-18.jpg', '156608-142776-18.jpg', '156607-142777-18.jpg', '156608-142772-18.jpg', '156607-142776-18.jpg', '156608-142777-18.jpg']

Here are the tiles visualized:

Visualized tiles

What I need to do is to merge all neighboring images into one image. So, in my example, I wish to merge the lower five tiles into one and output two images - the big merged image and the upper single tile which has no neighbors.

I am not sure how to achieve this. It seems to me there should be a simple solution to this that I cannot figure out.

I will use the same code for merging a large number of tiles so I want to avoid solutions that use a lot of memory or take long time. I work in Python. I don't need the actual code, just an idea how to do this.

  • Which python GIS libraries are you using? – BERA Jul 16 '20 at 19:30
  • geopandas, shapely, geojson, but i am free to use others, too – Jan Pisl Jul 16 '20 at 19:58
  • 1
    Then a similar workflow as my answer should work. Just list files instead of raster layers. – BERA Jul 16 '20 at 20:01
  • 1
    Yes, I think I get the idea. Thanks a lot! – Jan Pisl Jul 16 '20 at 20:17

This could give you some ideas, I use QGIS and networkx python module to check if raster extents intersects:

import networkx as nx

layerlist = [layer for layer in QgsProject.instance().mapLayers().values()]

rasterlist = []
for l in layerlist:
    for l2 in layerlist:
        if l.name()!=l2.name() and l.extent().intersects(l2.extent()):
            if l.extent().intersect(l2.extent()).perimeter()>0: #Excludes rasters connected only in corners
                rasterlist.append([l.source(), l2.source()])

adjacent_rasters  = list(nx.connected_components(G))

single_rasters = [r.source() for r in layerlist if not any(r.source() in i for i in rasterlist)]

print('single_rasters: ',single_rasters)
for e, rastergroup in enumerate(adjacent_rasters,1):
    print('Group {} has {} rasters: {}'.format(e,len(rastergroup),rastergroup))

enter image description here

Then merge them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.