Some words about the binary driver, used to convert OSM maps to a format usable for navigation :
The map coordinates of binfile are in meters, measured from equator and null meridian, with a merkator projection. The limit of this world is about 20000 km in earch direction from this null point. This doesn't cover polar regions, but it's ok for now.
So, the world gives a 40000x40000km rectangle. This rectangle is divided into 4 equally-sized sub-rectangles called a,b,c and d...
- a is top left
- b top right
- c bottom left
- d bottom right
Each of this sub-rectangles is then further subdivided, so a rectlangle "aa" is top left in "a"
This is continued further up to 14 levels (but the number of levels might be variable). So in the end effect you get many tiles which are containing objects which are within this tile... And the tiles are also containing references to tiles which are within them.
Each tile is a member file in a zip file. So to extract an area of this file, you just read the zip file sequentially.
How to extract a specific area from this map format
You can calculate the bounding box of the current tile.
Then there are two possibilities:
- The tile overlaps with the area you are interested in : Then simply copy the whole file data, including its header to the output, and add an entry to the directory which will be written later
- The tile doesn't overlap : Then don't drop that file, but instead write a file with size 0 and the same name to the output (I will explain later why this is needed), and add an entry to the directory
At some point you will have reached the end of the zip files, then you have to write the zip directory and the "end of directory" marker
This will be very fast (you don't have to look into the zip files, which would mean decompressing and compressing it again) but has some disadvantages:
- You will have many empty files in it which are not really necessary. This is needed because the reference to sub-tiles are by number, and not by name (would be slow), and so the position of a tile within the zip file is not allowed to change
- You get some data you didn't want to have : this is because a tile which overlaps with your area of course doesn't contain only data from your wanted area, but from the area where it is located