1
0
Fork 0

Merge branch 'geoindex' into devel

This adds Richards new cursor implementation.
This commit is contained in:
Richard Parker 2015-12-18 08:56:20 +00:00
commit aa9115886d
3 changed files with 295 additions and 7 deletions

View File

@ -1,4 +1,4 @@
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// @brief geo index
///
/// @file
@ -71,6 +71,7 @@ int hs4[4]={33972992,9770664,11661062,28398735};
int hs5[4]={79685116,67516870,19274248,35037618};
static GeoIndex * gi;
static GeoCursor * gcr;
static GeoCoordinate gcp;
static GeoCoordinate gcp1, gcp2, gcp3, gcp4;
static GeoCoordinates * list1;
@ -122,6 +123,17 @@ int GCMASS (GeoCoordinates * gc, int ct, int hash) {
j=j*7;
j=j%123456791;
}
if(hash!=j)
{
for(i=0;i<(int) gc->length;i++)
{
printf("long %14.9f",gc->coordinates[i].longitude);
printf(" lat %14.9f",gc->coordinates[i].latitude);
printf(" dat %4d",(int)((char *)gc->coordinates[i].data-ix));
printf(" dist %12.1f \n",gc->distances[i]);
}
printf("Correct hash is %d\n",j);
}
GeoIndex_CoordinatesFree(gc);
return hash == j ? 1 : 0;
@ -264,6 +276,51 @@ BOOST_FIXTURE_TEST_SUITE(GeoIndexTest, GeoIndexSetup)
/* 3 is Auckland -36.916667 +174.783333 */
/* 4 is Jo'burg -26.166667 +28.033333 */
/***********************************/
/* 1000-1100 first tests on cursor */
BOOST_AUTO_TEST_CASE (tst_geo1000) {
gi=GeoIndex_new();
la=41.23456789;
lo=39.87654321;
for(j=1;j<50;j++)
{
gcp.latitude = la;
gcp.longitude= lo;
gcp.data = &ix[j];
r = GeoIndex_insert(gi,&gcp);
icheck(1000,0,r);
la+=19.5396157761;
if(la>90.0) la-=180.0;
lo+=17.2329155421;
if(lo>180) lo-=360.0;
}
gcp.latitude = 0.0;
gcp.longitude= 25.5;
gcr = GeoIndex_NewCursor(gi,&gcp);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1001,list1,5, 87399654);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1002,list1,5, 97583446);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1003,list1,5, 30565728);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1004,list1,5, 77530526);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1005,list1,5, 38005425);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1006,list1,5, 106050772);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1007,list1,5, 51101201);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1008,list1,5, 83277910);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1009,list1,5, 53245966);
list1 = GeoIndex_ReadCursor(gcr,5);
gcmass(1010,list1,4, 86589238);
MyFree(gi);
}
/***************************************/
BOOST_AUTO_TEST_CASE (tst_geo1) {
gcp1.latitude = 51.5;
gcp1.longitude = -0.166666;
@ -293,6 +350,32 @@ BOOST_AUTO_TEST_CASE (tst_geo1) {
/* first some easily recognizable GeoStrings */
/* mainly for debugging rather than regression */
BOOST_AUTO_TEST_CASE (tst_geo15) {
gi=GeoIndex_new();
GeoIndex_hint(gi,10); /* set it to "robust" mode */
for(i=0;i<50;i++)
{
gcp.latitude = 90.0;
gcp.longitude = 180.0;
gcp.data = ix + i;
r = GeoIndex_insert(gi,&gcp);
icheck(10,0,r);
}
gcp.latitude = 0.0;
gcp.longitude= 25.5;
gcr = GeoIndex_NewCursor(gi,&gcp);
list1 = GeoIndex_ReadCursor(gcr,1);
icheck(11,1,list1->length);
gcp.latitude = 89.9;
gcp.longitude = -180.0;
gcp.data = ix + 64;
GeoIndex_insert(gi,&gcp);
list1 = GeoIndex_NearestCountPoints(gi,&gcp,1);
gccheck(13,list1, 1,"AAAAAAAAAAAAAAAABAAAAAAAA");
gicheck(14,gi);
/*GeoIndex_INDEXDUMP(gi,stdout);*/
MyFree(gi);
}
BOOST_AUTO_TEST_CASE (tst_geo10) {
gi=GeoIndex_new();
GeoIndex_hint(gi,10); /* set it to "robust" mode */
@ -1260,6 +1343,7 @@ BOOST_AUTO_TEST_CASE (tst_geo900) {
MyFree(gi);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief generate tests
////////////////////////////////////////////////////////////////////////////////

View File

@ -1,4 +1,4 @@
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// @brief geo index
///
/// @file
@ -35,7 +35,7 @@
#include "GeoIndex.h"
/* Radius of the earth used for distances */
#define EARTHRADIUS 6371000.0
#define EARTHRADIAN 6371000.0
#define GEOSLOTSTART 50
#define GEOPOTSTART 100
@ -292,7 +292,7 @@ double GeoIndex_distance(GeoCoordinate * c1, GeoCoordinate * c2)
y2=cos(c2->latitude*M_PI/180.0)*sin(c2->longitude*M_PI/180.0);
mole=sqrt((xx1-x2)*(xx1-x2) + (yy1-y2)*(yy1-y2) + (z1-z2)*(z1-z2));
if(mole > 2.0) mole = 2.0; /* make sure arcsin succeeds! */
return 2.0 * EARTHRADIUS * asin(mole/2.0);
return 2.0 * EARTHRADIAN * asin(mole/2.0);
}
/* =================================================== */
/* GeoIndexFreePot */
@ -740,11 +740,22 @@ void GeoMkDetail(GeoIx * gix, GeoDetailedPoint * gd, GeoCoordinate * c)
double GeoMetersToSNMD(double meters)
{
double angle,hnmd;
angle=0.5*meters/EARTHRADIUS;
angle=0.5*meters/EARTHRADIAN;
hnmd=sin(angle); /* half normalized mole distance */
if(angle>=M_PI/2.0) return 4.0;
else return hnmd*hnmd*4.0;
}
double GeoFixtoSNMD(GeoFix gf)
{
double x;
x=gf;
x=x/ARCSINFIX;
x=sin(x);
x=x*x;
x=x*4.0;
return x;
}
/* =================================================== */
/* GeoSetDistance */
/* During a search (of either type), the target point */
@ -1033,7 +1044,7 @@ GeoCoordinates * GeoAnswers (GeoIx * gix, GeoResults * gr)
(gix->gc)[slot].data;
mole=sqrt(gr->snmd[i]);
if(mole > 2.0) mole = 2.0; /* make sure arcsin succeeds! */
gr->snmd[j]= 2.0 * EARTHRADIUS * asin(mole/2.0);
gr->snmd[j]= 2.0 * EARTHRADIAN * asin(mole/2.0);
j++;
}
ans->distances = gr->snmd;
@ -2131,6 +2142,195 @@ int GeoIndex_hint(GeoIndex * gi, int hint)
return 0;
}
/* =================================================== */
/* GeoCr structure */
/* This is the REAL GeoCursor structure - the one in */
/* the GeoIndex.h file is just a sham (it says it is */
/* a char!) to keep the structure private so that the */
/* GeoIndex.h is short and contains only data of */
/* interest to the user. */
/* =================================================== */
typedef struct {
int pot;
GeoFix dist;
} hpot; // pot for putting on the heap
bool hpotcompare(hpot a, hpot b)
{
if(a.dist>b.dist) return true;
else return false;
}
typedef struct {
int slot;
double snmd;
} hslot; // pot for putting on the heap
bool hslotcompare(hslot a, hslot b)
{
if(a.snmd>b.snmd) return true;
else return false;
}
typedef struct {
GeoIx * Ix; /* GeoIndex */
GeoDetailedPoint gd;
double potsnmd;
double slotsnmd;
std::vector<hpot> potheap;
std::vector<hslot> slotheap;
} GeoCr;
GeoFix makedist(GeoPot * pot, GeoDetailedPoint * gd)
{
GeoFix dist,d1;
int i;
dist=0;
for(i=0;i<GeoIndexFIXEDPOINTS;i++)
{
if(gd->fixdist[i]>pot->maxdist[i])
d1=gd->fixdist[i]-pot->maxdist[i];
else d1=0;
if(d1>dist) dist=d1;
}
return dist;
}
GeoCursor * GeoIndex_NewCursor(GeoIndex * gi, GeoCoordinate * c)
{
GeoIx * gix;
GeoCr * gcr;
hpot hp;
gix = (GeoIx *) gi;
gcr = static_cast<GeoCr*>(TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(GeoCr), false));
if (gcr == NULL) {
return (GeoCursor *) gcr;
}
gcr->Ix=gix;
std::vector<hpot>* p = new (&gcr->potheap) std::vector<hpot>();
std::vector<hslot>* q = new (&gcr->slotheap) std::vector<hslot>();
(void) p; // avoid compiler warnings - I just want to call
(void) q; // the constructors and have no use for p,q.
GeoMkDetail(gix,&(gcr->gd),c);
hp.pot=1;
hp.dist = makedist(gix->pots+1,&(gcr->gd));
gcr->potsnmd=GeoFixtoSNMD(hp.dist);
gcr->slotsnmd=20.0;
gcr->potheap.push_back(hp);
std::push_heap(gcr->potheap.begin(), gcr->potheap.end(),hpotcompare);
return (GeoCursor *) gcr;
}
GeoCoordinates * GeoIndex_ReadCursor(GeoCursor * gc, int count)
{
int i,j,r;
GeoCoordinate * ct;
GeoResults * gr;
GeoCoordinates * gcts;
GeoCr * gcr;
GeoPot pot;
int slox;
double tsnmd;
hslot hs;
hpot hp;
gcr=(GeoCr *) gc;
gr=GeoResultsCons(count);
if(gr==NULL) return NULL;
while(gr->pointsct<count)
{
if(gcr->potsnmd < gcr->slotsnmd*1.000001)
{
// smash top pot - if there is one
if(gcr->potheap.size()==0) break; // that's all there is
pot=*((gcr->Ix)->pots+(gcr->potheap.front().pot));
// anyway remove top from heap
std::pop_heap(gcr->potheap.begin(),
gcr->potheap.end(),hpotcompare);
gcr->potheap.pop_back();
if(pot.LorLeaf==0)
{
// leaf pot - put all the points into the points heap
for(i=0;i<pot.RorPoints;i++)
{
j=pot.points[i];
ct=((gcr->Ix)->gc+j);
hs.slot=j;
hs.snmd=GeoSNMD(&(gcr->gd),ct);
gcr->slotheap.push_back(hs);
std::push_heap(gcr->slotheap.begin(),
gcr->slotheap.end(),hslotcompare);
}
if(gcr->slotheap.size()!=0)
{
slox=gcr->slotheap.front().slot;
gcr->slotsnmd=GeoSNMD(&gcr->gd,(gcr->Ix)->gc+slox);
}
}
else
{
hp.pot=pot.LorLeaf;
hp.dist = makedist((gcr->Ix)->pots+pot.LorLeaf,&(gcr->gd));
gcr->potheap.push_back(hp);
std::push_heap(gcr->potheap.begin(),
gcr->potheap.end(),hpotcompare);
hp.pot=pot.RorPoints;
hp.dist = makedist((gcr->Ix)->pots+pot.RorPoints,&(gcr->gd));
gcr->potheap.push_back(hp);
std::push_heap(gcr->potheap.begin(),
gcr->potheap.end(),hpotcompare);
}
gcr->potsnmd=10.0;
if(gcr->potheap.size()!=0)
{
pot=*((gcr->Ix)->pots+(gcr->potheap.front().pot));
gcr->potsnmd=GeoFixtoSNMD(makedist(&pot,&(gcr->gd)));
}
}
else
{
if(gcr->slotheap.size()==0) break; // that's all there is
slox=gcr->slotheap.front().slot;
tsnmd=GeoSNMD(&gcr->gd,(gcr->Ix)->gc+slox);
r = GeoResultsGrow(gr);
if(r==-1)
{
TRI_Free(TRI_UNKNOWN_MEM_ZONE, gr->snmd);
TRI_Free(TRI_UNKNOWN_MEM_ZONE, gr->slot);
TRI_Free(TRI_UNKNOWN_MEM_ZONE, gr);
return NULL;
}
gr->slot[gr->pointsct]=slox;
gr->snmd[gr->pointsct]=tsnmd;
gr->pointsct++;
gcr->slotsnmd=5.0;
std::pop_heap(gcr->slotheap.begin(),
gcr->slotheap.end(),hslotcompare);
gcr->slotheap.pop_back();
if(gcr->slotheap.size()!=0)
{
slox=gcr->slotheap.front().slot;
gcr->slotsnmd=GeoSNMD(&gcr->gd,(gcr->Ix)->gc+slox);
}
}
}
gcts=GeoAnswers(gcr->Ix,gr);
return gcts;
}
void GeoIndex_CursorFree(GeoCursor * gc)
{
GeoCr * cr;
if(gc == NULL) {
return;
}
cr = (GeoCr *) gc;
TRI_Free(TRI_UNKNOWN_MEM_ZONE, cr);
return;
}
/* =================================================== */
/* The remaining routines are usually */
/* only compiled in for debugging purposes. They allow*/

View File

@ -28,7 +28,7 @@
////////////////////////////////////////////////////////////////////////////////
/* GeoIndex.h - header file for GeoIndex algorithms */
/* Version 2.1 8.1.2012 R. A. Parker */
/* Version 2.2 25.11.2015 R. A. Parker */
#ifndef ARANGODB_GEO_INDEX_GEO_INDEX_H
#define ARANGODB_GEO_INDEX_GEO_INDEX_H 1
@ -94,6 +94,7 @@ typedef struct {
GeoCoordinates;
typedef char GeoIndex; /* to keep the structure private */
typedef char GeoCursor; /* to keep the structure private */
size_t GeoIndex_MemoryUsage (void*);
@ -108,6 +109,9 @@ GeoCoordinates * GeoIndex_PointsWithinRadius(GeoIndex * gi,
GeoCoordinate * c, double d);
GeoCoordinates * GeoIndex_NearestCountPoints(GeoIndex * gi,
GeoCoordinate * c, int count);
GeoCursor * GeoIndex_NewCursor(GeoIndex * gi, GeoCoordinate * c);
GeoCoordinates * GeoIndex_ReadCursor(GeoCursor * gc, int count);
void GeoIndex_CursorFree(GeoCursor * gc);
void GeoIndex_CoordinatesFree(GeoCoordinates * clist);
#ifdef TRI_GEO_DEBUG
void GeoIndex_INDEXDUMP(GeoIndex * gi, FILE * f);