I have a MySQL table with area and lat/lon location columns. Every area has many locations, say 20.000. Is there a way to pick just a few, say 100, that look somewhat evenly distributed on the map?
The distribution doesn't have to be perfect, query speed is more important. If that is not possible directly with MySQL a very fast algorithm that somehow picks evenly distributed locations might also work.
Thanks in advance.
Edit: answering some requests in comments. The data doesn't have something that can be used, it's just area and coordinates of locations, example:
+-------+--------------+----------+-----------+------------+--------+--------+
| id | area | postcode | lat | lon | colour | size |
+-------+--------------+----------+-----------+------------+--------+--------+
| 16895 | Athens | 10431 | 37.983917 | 23.7293599 | red | big |
| 16995 | Athens | 11523 | 37.883917 | 23.8293599 | green | medium |
| 16996 | Athens | 10432 | 37.783917 | 23.7293599 | yellow | small |
| 17000 | Thessaloniki | 54453 | 40.783917 | 22.7293599 | green | small |
+-------+--------------+----------+-----------+------------+--------+--------+
There are some more columns with characteristics but those are just used for filtering.
I did try getting the nth row in the meantime, it seems to work although a bit slow
SET @a = 0;
select * from `locations` where (@a := @a + 1) % 200 = 0
using random() also works but a bit slow too.
Edit2: Turns out it was easy to add postal codes on the table. Having that, getting grouped by postal code seems to give a nice to the eye result. Only issue is that there are very large areas with around 3000 distinct postcodes and getting just 100 may end up many of them show in one place, so will probably need to further process in PHP.
Edit3, answering @RickJames questions in comments so they are in one place:
- Please define "evenly distributed" -- evenly spaced in latitude? no two are "close" to each other? etc.
- "Evenly distributed" was a bad choice of words. We just want to show some locations on the area that are not all in one place
- Are the "areas" rectangles? Hexagons? Or gerrymandered congressional districts?
- They can be thought roughly as rectangles but it shouldn't matter. Important thing I missed, we also need to show locations from multiple areas. Areas may be far apart from each other or neighboring (but not overlap). In that case we'd want the sample of 100 to be split between the areas.
- Is the "100 per area" fixed? Or can it be "about 100"
- It's not fixed, it's around 100 but we can change this if it doesn't look nice
- Is there an AUTO_INCREMENT id on the table? Are there gaps in the numbers?
- Yes there is an
AUTO_INCREMENTid and can have gaps
- Yes there is an
- Has the problem changed from "100 per area" to "1 per postal code"?
- Nope the problem is still the same, "show 100 per area in a way that not all of them are in the same place", how this is done it doesn't matter
- What are the total row count and desired number of rows in output?
- Total row count depends on area and criteria, it can be up to 40k in an area. If total is more than
1000we want to fall back showing just a random of100. If1000or less we can just show all of them
- Total row count depends on area and criteria, it can be up to 40k in an area. If total is more than
- Do you need a different sample each time you run the query?
- Same sample or different sample even with the same criteria is fine
- Are you willing to add a column to the table?
- It's not up to me but if I have good argument then most probably we can add a new column