null+****@clear*****
null+****@clear*****
2011年 9月 12日 (月) 23:55:36 JST
Kouhei Sutou 2011-09-12 14:55:36 +0000 (Mon, 12 Sep 2011)
New Revision: 066d3919f79b94707b2576034fdd742954568d65
Log:
[geo] support geo_distance(text, geo_point) and geo_distance(text, text);
Modified files:
lib/geo.c
test/unit/core/test-geo.c
Modified: lib/geo.c (+31 -9)
===================================================================
--- lib/geo.c 2011-09-12 14:46:31 +0000 (977c92b)
+++ lib/geo.c 2011-09-12 14:55:36 +0000 (5646aac)
@@ -975,23 +975,45 @@ double
grn_geo_distance(grn_ctx *ctx, grn_obj *point1, grn_obj *point2)
{
double d = 0;
+ grn_bool point1_initialized = GRN_FALSE;
grn_bool point2_initialized = GRN_FALSE;
- grn_obj point2_;
- grn_id domain = point1->header.domain;
- if (domain == GRN_DB_TOKYO_GEO_POINT || domain == GRN_DB_WGS84_GEO_POINT) {
- if (point2->header.domain != domain) {
- GRN_OBJ_INIT(&point2_, GRN_BULK, 0, domain);
+ grn_obj point1_, point2_;
+ grn_id domain1 = point1->header.domain;
+ grn_id domain2 = point2->header.domain;
+ if (domain1 == GRN_DB_TOKYO_GEO_POINT || domain1 == GRN_DB_WGS84_GEO_POINT) {
+ if (domain1 != domain2) {
+ GRN_OBJ_INIT(&point2_, GRN_BULK, 0, domain1);
point2_initialized = GRN_TRUE;
if (grn_obj_cast(ctx, point2, &point2_, 0)) { goto exit; }
point2 = &point2_;
}
- d = grn_geo_distance_raw(ctx,
- GRN_GEO_POINT_VALUE_RAW(point1),
- GRN_GEO_POINT_VALUE_RAW(point2));
+ } else if (domain2 == GRN_DB_TOKYO_GEO_POINT ||
+ domain2 == GRN_DB_WGS84_GEO_POINT) {
+ GRN_OBJ_INIT(&point1_, GRN_BULK, 0, domain2);
+ point1_initialized = GRN_TRUE;
+ if (grn_obj_cast(ctx, point1, &point1_, 0)) { goto exit; }
+ point1 = &point1_;
+ } else if ((GRN_DB_SHORT_TEXT <= domain1 && domain1 <= GRN_DB_LONG_TEXT) &&
+ (GRN_DB_SHORT_TEXT <= domain2 && domain2 <= GRN_DB_LONG_TEXT)) {
+ GRN_OBJ_INIT(&point1_, GRN_BULK, 0, GRN_DB_WGS84_GEO_POINT);
+ point1_initialized = GRN_TRUE;
+ if (grn_obj_cast(ctx, point1, &point1_, 0)) { goto exit; }
+ point1 = &point1_;
+
+ GRN_OBJ_INIT(&point2_, GRN_BULK, 0, GRN_DB_WGS84_GEO_POINT);
+ point2_initialized = GRN_TRUE;
+ if (grn_obj_cast(ctx, point2, &point2_, 0)) { goto exit; }
+ point2 = &point2_;
} else {
- /* todo */
+ goto exit;
}
+ d = grn_geo_distance_raw(ctx,
+ GRN_GEO_POINT_VALUE_RAW(point1),
+ GRN_GEO_POINT_VALUE_RAW(point2));
exit :
+ if (point1_initialized) {
+ GRN_OBJ_FIN(ctx, &point1_);
+ }
if (point2_initialized) {
GRN_OBJ_FIN(ctx, &point2_);
}
Modified: test/unit/core/test-geo.c (+1 -1)
===================================================================
--- test/unit/core/test-geo.c 2011-09-12 14:46:31 +0000 (1296c66)
+++ test/unit/core/test-geo.c 2011-09-12 14:55:36 +0000 (405a1b7)
@@ -252,11 +252,11 @@ data_distance(void)
/*
ADD_DATUM("text - tokyo",
GRN_DB_SHORT_TEXT, GRN_DB_TOKYO_GEO_POINT);
+*/
ADD_DATUM("text - wgs84",
GRN_DB_SHORT_TEXT, GRN_DB_WGS84_GEO_POINT);
ADD_DATUM("text - text",
GRN_DB_SHORT_TEXT, GRN_DB_SHORT_TEXT);
-*/
#undef ADD_DATUM
}