# [OSM-dev] Osmosis Bug Report: Node TileIDs Incorrect

Brett Henderson brett at bretth.com
Sun Apr 27 11:38:21 BST 2008

```If somebody knows how the current quad tile implementation is supposed
to work, please let me know.

I've had a quick look at the error but I don't know how the quad tile
algorithm should work.  When I implemented this I just copied the C
implementation, however I found it a little confusing which code I
should be looking at because the source files didn't all appear to be in
a single location.  As a result, I may have implemented it wrongly.

This is my java implementation:

/**
* Calculates a tile index based upon the supplied coordinates.
*
* @param latitude
*            The coordinate latitude.
* @param longitude
*            The coordinate longitude.
* @return The tile index value.
*/
public long calculateTile(double latitude, double longitude) {
int x;
int y;
long tile;

//x = (int) Math.round((longitude + 180) * 65536 / 360);
//y = (int) Math.round((latitude + 90) * 65536 / 180);
x = (int) Math.floor((longitude + 180) * 65536 / 360);
y = (int) Math.floor((latitude + 90) * 65536 / 180);

tile = 0;

for (int i = 15; i >= 0; i--) {
tile = (tile << 1) | ((x >> i) & 1);
tile = (tile << 1) | ((y >> i) & 1);
}

return tile;
}

Note the commented out lines, they are the original code giving the
I have replaced them with different rounding code which now gives the
correct answer for the example given by cschmidt below.

For lat="51.4781325" and lon="-0.1474929" I now get the correct answer
of 2062265654 but I'm not sure if this is a fluke or whether this is now
the correct implementation.

Christopher Schmidt wrote:
> On Wed, Apr 23, 2008 at 09:07:50AM +1000, Brett Henderson wrote:
>
>> Oh crap, apparently I didn't test that bit.  I never did get the mysql
>> extensions installed to compare against.
>>
>> I doubt if I'll get to this for a few days, bit run off my feet at the
>> moment.  I don't know what the problem is but perhaps Java is doing some
>> unexpected rounding along the way.
>>
>
> That's essentially what I figured. For now, anyone using osmosis to
> populate a mysql database should use the update statement at the end of
> this post if they want to get accurate results back from API map/bbox
> queries.
>
>
>> Christopher Schmidt wrote:
>>
>>> Osmosis creates incorrect tile IDs for some nodes when reading from XML
>>> and inserting into MySQL.
>>>
>>> For the node:
>>>
>>> <node id="21619242" lat="51.4781325" lon="-0.1474929" user="Osmosis
>>>     System User" visible="true" timestamp="2007-11-11T09:11:14-05:00">
>>>
>>> Osmosis generates:
>>>
>>> 2062265655
>>>
>>> tile_for_point generates:
>>>
>>> 16:35:51 < crschmidt> select tile_for_point(514781325,-01474929);
>>> 16:35:56 < crschmidt> | tile_for_point(514781325,-01474929) |
>>> 16:35:56 < crschmidt> +-------------------------------------+
>>> 16:35:56 < crschmidt> |                          2062265654 |
>>>
>>> Updating a table with 115000 nodes in it says:
>>>
>>> mysql> update current_nodes set tile=tile_for_point(latitude,
>>>                                                     longitude);
>>> Query OK, 103333 rows affected (11.41 sec)
>>> Rows matched: 115692  Changed: 103333  Warnings: 0
>>>
>>> Which means, I guess, that 12000 (~10%) ended up okay, and everything
>>> else was (presumably) off by one.
>>>
>>> Regards,
>>>
>>>
>> _______________________________________________
>> dev mailing list
>> dev at openstreetmap.org
>> http://lists.openstreetmap.org/cgi-bin/mailman/listinfo/dev
>>
>
>

```