| 4 | |
| 5 | [[Image(illustration1.png, 300)]] |
| 6 | |
| 7 | In postgis 1.4 this would cause 178137 * 12167 = 2 167 392 879 iterations. |
| 8 | With the faster algorithm described in [wiki:NewDistCalcGeom2Geom How the distance calculations is done] the iterations will be very much reduced and then takes about 7 seconds. But even now there is a lot of unnecessary work done when calculating the exact distance to each and every of the sub geometries. This is how we now instead uses bounding boxes to only calculate a selection of geometries. |
| 9 | |
| 10 | [[Image(illustration2.png, 300)]] |
| 11 | |
| 12 | Here is the bounding boxes of the two states. The first thing we do is to iterate through all combinations of bounding boxes to find the “smallest max distance” between the boxes. What we know from this value is that the distance we get here is longer or the same (if two inputed points) than the min distance we are looking for |
| 13 | The result is the distance along a line like this: |
| 14 | |
| 15 | [[Image(illustration3.png, 300)]] |
| 16 | |
| 17 | Now we iterate through all the combinations again and store all combinations with smaller min distance than the earlier found in a list. We also order the list so we get the smallest min distance first. |
| 18 | |
| 19 | Now we are ready to calculate real distances beginning with the bounding boxes closest to each other. We continue the process until the next “min distance between bounding boxes” is longer than the min distance between real geometries we have found. |
| 20 | |
| 21 | The result is then the distance along a line like this returned in about 600ms. |
| 22 | |
| 23 | [[Image(illustration4.png, 300)]] |