I've problem. I've database with 500k records. Each record store latitude, longitude, specie of animal,date of observation. I have to draw power grid(15x10) above mapkit view, that demonstrate the power of specie within this power grid cell. Each cell is 32x32 box.

Basically calculate in run-time it's very slow. Have somebody idea how you can cache it?In memory or perhaps in database.

Data structure:

Observation:

  • Latitude
  • Longitude
  • Date
  • Specie
  • another trivial data

Screen sample:

alt text

Each red-colored box opocasity show count of species in this area.

Code which i use now: data -> choose from database, it's all observation in map region

for (int row = 0; row < rows; row++)
 { 
  for (int column = 0; column < columns; column++)
  {
   speciesPerBox=0;
   minG=boxes[row][column].longitude;
   if (column!=columns-1) {
    maxG=boxes[row][column+1].longitude;
   } else {
    maxG=buttomRight.longitude;
   }

   maxL=boxes[row][column].latitude;
   if (row!=rows-1) {
    minL=boxes[row+1][column].latitude;
   } else {
    minL=buttomRight.latitude;
   }

   for (int i=0; i<sightingCount; i++) {
    l=data[i].latitude;
    g=data[i].longitude;

    if (l>=minL&&l<maxL&&g>=minG&&g<maxG) {
     for (int j=0; j<speciesPerBox; j++) {
       if (speciesCountArray[j]==data[i].specie) {
        hasSpecie=YES;
       }
      }

      if (hasSpecie==NO) {
       speciesCountArray[speciesPerBox]=data[i].specie;
       speciesPerBox++;
      }

      hasSpecie=NO;


     }
    }
   }


   mapData[row][column].count = speciesPerBox;
  }
 }

Because you information is static, you are able to pre-compute each species for every power grid and store it within the database rather than all of the location coordinates.

Because you have 15 x 10 = 150 cells, you'll finish track of 150 * [num of species] records within the database, that ought to be considered a much more compact number.

Also, make certain you've indexes around the proper posts. Otherwise, your queries will need to scan each and every record again and again again.