OpenStreetMap-logo OpenStreetMap

Artikkel When Comment
RoboSat ❤️ Tanzania

So after having a bunch of challenges with titling, here is what I did:

1- convert the image to tiles using gdal2tiles.py gdal

gdal_translate -ot byte -of vrt wms.tif filename.vrt
gdal2tiles.py -z 20 --processes=8 filename.vrt masks-512/

Note: Modify the gdal2tiles.py to generate 512*512 tiles

2- Convert the OSM GeoJson file to a binary Geotif using the following code:

from osgeo import gdal

vector_layer = r"C:\Users\Mehran\Desktop\test4\buildings.geojson"
raster_layer = r"C:\Users\Mehran\Desktop\test4\wms.tif"
target_layer = r"C:\Users\Mehran\Desktop\test4\wms_binary.tif"

# open the raster layer and get its relevant properties
raster_ds = gdal.Open(raster_layer, gdal.GA_ReadOnly)
xSize = raster_ds.RasterXSize
ySize = raster_ds.RasterYSize
geotransform = raster_ds.GetGeoTransform()
projection = raster_ds.GetProjection()

# create the target layer (1 band)
driver = gdal.GetDriverByName('GTiff')
target_ds = driver.Create(target_layer, xSize, ySize, bands = 1, eType = gdal.GDT_Byte, options = ["COMPRESS=DEFLATE"])
target_ds.SetGeoTransform(geotransform)
target_ds.SetProjection(projection)

# rasterize the vector layer into the target one
ds = gdal.Rasterize(target_ds, vector_layer, burnValues = [1])

target_ds = None

3- Convert the binary image to tiles using gdal2tiles.py (the same as step 1)

RoboSat ❤️ Tanzania

sorry for the confusions. So my problem is that the labels assigned to the image tiles are different from the labels of mask images. For example:

for image: C:\Users\Mehran\Desktop\test\tiles\20\606678\478661.png for mask: C:\Users\Mehran\Desktop\test\masks\20\600678\570023.png

I used the following code to generate the image tiles: gdal_translate -ot byte -of vrt image.tif filename.vrt gdal2tiles.py -z 20 –processes=8 filename.vrt tiles/

When I visualize the image and geojson, they are on the right place. They both are in the WGS84 coordinate system. I cannot understand that why that get different labels in the slippy map tiles.

RoboSat ❤️ Tanzania

I got another clue. In the dataset-building.toml I have: dataset = ‘C:/Users/Mehran/Desktop/test/tiles’

But it seems that this address is ignored. Even when I delete this line, the code runs fine and generates the masks.

RoboSat ❤️ Tanzania

I clipped the GeoJson to only includes the polygons overlapping with the imagery. Now the folders in the tiles and masks folder are identical, but the numbers assigned for the tile images are different from numbers assigned for the mask images. For example, 478657 and 569904 are for two corresponding images. Also 1967 mask images are generated for 3080 tile images.

RoboSat ❤️ Tanzania

I generate the masks using the following command: rs rasterize –dataset dataset-building.toml –zoom 20 –size 256 buildings.geojson buildings.tiles masks

and I get this: 18529/18529 [00:13<00:00, 1349.35feature/s]100% 10645/10645 [02:00<00:00, 88.30tile/s]

a mask folder is generated with 10645 images that is equal to the number of rows in the buildings.tiles. My tile folder includes 3,080 images. My original image only covers a subset of buildings in the GeoJson file. All of the images in the mask folder are fine with images classified into building and background. The problem is that I cannot find any of the tile images in the mask folder. For example I have:

C:\Users\Mehran\Desktop\test\tiles\20\606678\478661.png

But, I cannot find the corresponding image in the mask folder.

Here is the only image in the masks\20\600678 folder: C:\Users\Mehran\Desktop\test\masks\20\600678\570023.png

My other question is that what are the numbers in the buildings.tiles file: 606705,569916,20 606526,569974,20 606514,569911,20 600314,569998,20

Is the projection and coordinate system of the files matters. My tile images are EPSG:3857 (I used gdal2tiles to make the tiles) and my GeoJson is EPSG:4326.

RoboSat ❤️ Tanzania

When I run the extract using docker, 16 geojson files with names like “buildings-4ef7bde13d8e4528b48c577a2d269e1c.geojson” are generated. I am running on the dataset used in this post. It seems that the area is devided into smaller area. Each geojson has a size of about 20 MB.