summaryrefslogtreecommitdiff
path: root/src/search_road.c
blob: 4b6b10ea607e0f50eea01124f3d0f6a9cd62924c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/***************************************************************************
 *            search_road.c
 *
 *  Copyright  2005  Ian McIntosh
 *  ian_mcintosh@linuxadvocate.org
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <gtk/gtk.h>
#include <math.h>
#include <stdlib.h>

#include "../include/db.h"
#include "../include/util.h"
#include "../include/geometryset.h"
#include "../include/searchwindow.h"
#include "../include/search.h"

typedef struct {
	gint m_nNumber;			// house number	eg. 51
	gchar* m_pszRoadName;	// road name eg. Washington

	gint m_nSuffixID;		// a number representing eg. Ave
} roadsearch_t;

#define ROADSEARCH_NUMBER_NONE		(-1)
#define SEARCH_RESULT_COUNT_LIMIT	(200)		// how many rows to get from DB
#define MAX_QUERY 	(4000)

#if 0 		// glib < 2.6
gint g_strv_length(const gchar** a)
{
	gint nCount=0;
	const gchar** pp = a;
	while(*pp != NULL) {
		nCount++;
		pp++;
	}
	return nCount;
}
#endif


// prototypes

void search_road_on_cleaned_sentence(const gchar* pszCleanedSentence);
void search_road_on_words(const gchar** aWords, gint nWordCount);
void search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch);
void search_road_filter_result(const gchar* pszRoadName, gint nRoadNumber, gint nRoadSuffixID, gint nAddressLeftStart, gint nAddressLeftEnd, gint nAddressRightStart, gint nAddressRightEnd, pointstring_t* pPointString);


// functions

void search_road_execute(const gchar* pszSentence)
{
	if(pszSentence[0] == 0) {
		return;	// ignore empty searches ?
	}

	TIMER_BEGIN(search, "SEARCH BEGIN");

	// copy sentence and clean it
	gchar* pszCleanedSentence = g_strdup(pszSentence);
	search_clean_string(pszCleanedSentence);
	search_road_on_cleaned_sentence(pszCleanedSentence);
	g_free(pszCleanedSentence);

	TIMER_END(search, "SEARCH END");
}

void search_road_on_cleaned_sentence(const gchar* pszCleanedSentence)
{
	// Create an array of the words
	gchar** aWords = g_strsplit(pszCleanedSentence," ", 0);	// " " = delimeters, 0 = no max #

	gint nWordCount = g_strv_length(aWords);

	if(nWordCount > 0) {
		search_road_on_words(aWords, nWordCount);
	}

	// cleanup
	g_strfreev(aWords);	// free the array of strings	
}

void search_road_on_words(const gchar** aWords, gint nWordCount)
{
	g_assert(nWordCount > 0);
	roadsearch_t roadsearch = {0};

	/*  Match these:
	First
	First St.
	101 First
	101 First St.
	101 First Street
	*/

	// index of first and last words of road name (the bit in the middle)
	gint iFirst = 0;
	gint iLast = nWordCount-1;

	// Start stripping off words as we identify roadsearch_t structure members
	gint nRemainingWordCount = nWordCount;

	// house number
	roadsearch.m_nNumber = ROADSEARCH_NUMBER_NONE;
	if(nRemainingWordCount >= 2) {
		if(search_address_number_atoi(aWords[iFirst], &roadsearch.m_nNumber)) {
			iFirst++;	// first word is TAKEN :)
			nRemainingWordCount--;
		}
	}

	// road name suffix (eg. "ave")
	if(nRemainingWordCount >= 2) {
		gint nSuffixID;
		if(map_road_suffix_atoi(aWords[iLast], &nSuffixID)) {
			roadsearch.m_nSuffixID = nSuffixID;
			iLast--;
			nRemainingWordCount--;
		}
	}

	// finally, take remaining words and consider that the street name
	// TODO: concat the words from iFirst to iLast together.. do NOT just use iLast!!!!
	roadsearch.m_pszRoadName = g_strdup(aWords[iLast]);
	search_road_on_roadsearch_struct(&roadsearch);

	g_free(roadsearch.m_pszRoadName);
}

void search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch)
{
	gchar* pszAddressClause;
	if(pRoadSearch->m_nNumber != ROADSEARCH_NUMBER_NONE) {
		pszAddressClause = g_strdup_printf(
			"AND ("
			"(%d BETWEEN Road.AddressLeftStart AND Road.AddressLeftEnd)"
			" OR (%d BETWEEN Road.AddressLeftEnd AND Road.AddressLeftStart)"
			" OR (%d BETWEEN Road.AddressRightStart AND Road.AddressRightEnd)"
			" OR (%d BETWEEN Road.AddressRightEnd AND Road.AddressRightStart)"
			")", pRoadSearch->m_nNumber, pRoadSearch->m_nNumber, 
				 pRoadSearch->m_nNumber, pRoadSearch->m_nNumber);
	}
	else {
		pszAddressClause = g_strdup("");
	}

	gchar* pszSuffixClause;
	if(pRoadSearch->m_nSuffixID != ROAD_SUFFIX_NONE) {
		pszSuffixClause = g_strdup_printf(
			"AND (RoadName.SuffixID = %d)",
			pRoadSearch->m_nSuffixID);
	}
	else {
		pszSuffixClause = g_strdup("");
	}

	// if doing a road search, only show 1 hit per road
	//~ gchar* pszGroupClause;
	//~ if(pRoadSearch->m_nSuffixID == ROAD_SUFFIX_NONE) {
		//~ pszGroupClause = g_strdup("GROUP BY (RoadName.ID, RoadName.SuffixID)");
	//~ }
	//~ else {
		//~ pszGroupClause = g_strdup("");
	//~ }

	gchar azQuery[MAX_QUERY];
	gchar* pszSafeRoadName = db_make_escaped_string(pRoadSearch->m_pszRoadName);
	g_print("pRoadSearch->m_pszRoadName = %s, pszSafeRoadName = %s\n", pRoadSearch->m_pszRoadName, pszSafeRoadName);

	g_snprintf(azQuery, MAX_QUERY,
		"SELECT Road.ID, RoadName.Name, RoadName.SuffixID, AsText(Road.Coordinates), Road.AddressLeftStart, Road.AddressLeftEnd, Road.AddressRightStart, Road.AddressRightEnd"
		" FROM RoadName"
		" LEFT JOIN Road_RoadName ON (RoadName.ID=Road_RoadName.RoadNameID)"
		" LEFT JOIN Road ON (Road_RoadName.RoadID=Road.ID %s)"	// address # clause
		" WHERE RoadName.Name LIKE '%s%%'"
//		" WHERE RoadName.Name='%s'"
		" AND Road.ID IS NOT NULL"	// don't include rows where the Road didn't match
		" %s"
//		" %s"
		" ORDER BY RoadName.Name"
		" LIMIT %d;", pszAddressClause, pszSafeRoadName, pszSuffixClause, 
			// pszGroupClause, 
			SEARCH_RESULT_COUNT_LIMIT + 1);
	
	// free strings
	db_free_escaped_string(pszSafeRoadName);
	g_free(pszAddressClause);
	g_free(pszSuffixClause);
//	g_free(pszGroupClause);

//	g_strlcpy(azQuery, , MAX_QUERY);
	g_print("SQL: %s\n", azQuery);

	db_resultset_t* pResultSet;
	if(db_query(azQuery, &pResultSet)) {
		db_row_t aRow;

		// get result rows!
		gint nCount = 0;		
		while((aRow = mysql_fetch_row(pResultSet))) {
			nCount++;
			if(nCount <= SEARCH_RESULT_COUNT_LIMIT) {
				pointstring_t* pPointString = NULL;
				geometryset_util_new_pointstring(&pPointString);
				db_parse_pointstring(aRow[3], pPointString, geometryset_util_new_point);

//	g_print("raw: %s\n", aRow[3]);
				search_road_filter_result(aRow[1], pRoadSearch->m_nNumber, atoi(aRow[2]), atoi(aRow[4]), atoi(aRow[5]), atoi(aRow[6]), atoi(aRow[7]), pPointString);
//	g_print("%03d: Road.ID='%s' RoadName.Name='%s', Suffix=%s, L:%s-%s, R:%s-%s\n", nCount, aRow[0], aRow[1], aRow[3], aRow[4], aRow[5], aRow[6], aRow[7]);
				geometryset_util_free_pointstring(pPointString);
			}
		}
		db_free_result(pResultSet);

		if(nCount == 0) {
			g_print("no address search results\n");
		}
	}
	else {
		g_print("search failed\n");
	}
}


gfloat point_calc_distance(mappoint_t* pA, mappoint_t* pB)
{
	// determine slope of the line
	gdouble fRise = pB->m_fLatitude - pA->m_fLatitude;
	gdouble fRun = pB->m_fLongitude - pA->m_fLongitude;

	return sqrt((fRun*fRun) + (fRise*fRise));
}

typedef enum {
	ROADSIDE_CENTER,
	ROADSIDE_LEFT,
	ROADSIDE_RIGHT,
} ERoadSide;

#define HIGHLIGHT_DISTANCE_FROM_ROAD (0.00017)		// this seems like a good amount...

void pointstring_walk_percentage(pointstring_t* pPointString, gdouble fPercent, ERoadSide eRoadSide, mappoint_t* pReturnPoint)
{
	gint i;
	if(pPointString->m_pPointsArray->len < 2) {
		g_assert_not_reached();
	}

	//
	// count total distance
	//
	gfloat fTotalDistance = 0.0;
	mappoint_t* pPointA = g_ptr_array_index(pPointString->m_pPointsArray, 0);
	mappoint_t* pPointB;
	for(i=1 ; i<pPointString->m_pPointsArray->len ; i++) {
		pPointB = g_ptr_array_index(pPointString->m_pPointsArray, 1);

		fTotalDistance += point_calc_distance(pPointA, pPointB);
		
		pPointA = pPointB;
	}

	gfloat fTargetDistance = (fTotalDistance * fPercent);
	gfloat fRemainingDistance = fTargetDistance;

	pPointA = g_ptr_array_index(pPointString->m_pPointsArray, 0);
	for(i=1 ; i<pPointString->m_pPointsArray->len ; i++) {
		pPointB = g_ptr_array_index(pPointString->m_pPointsArray, 1);

		gfloat fLineSegmentDistance = point_calc_distance(pPointA, pPointB);
		if(fRemainingDistance <= fLineSegmentDistance || (i == pPointString->m_pPointsArray->len-1)) {
			// this is the line segment we are looking for.

			gfloat fPercentOfLine = (fRemainingDistance / fLineSegmentDistance);
			if(fPercentOfLine > 1.0) fPercentOfLine = 1.0;

			gfloat fRise = (pPointB->m_fLatitude - pPointA->m_fLatitude);
			gfloat fRun = (pPointB->m_fLongitude - pPointA->m_fLongitude);

			// Calculate the a point on the center of the line segment
			// the right percent along the road
			pReturnPoint->m_fLongitude = pPointA->m_fLongitude + (fRun * fPercentOfLine);
			pReturnPoint->m_fLatitude = pPointA->m_fLatitude + (fRise * fPercentOfLine);

			gdouble fPerpendicularNormalizedX = -fRise / fLineSegmentDistance;
			gdouble fPerpendicularNormalizedY = fRun / fLineSegmentDistance;

			if(eRoadSide == ROADSIDE_CENTER) {
				// do nothing, we're already at line center
			}
			else if(eRoadSide == ROADSIDE_LEFT) {
// g_print("MOVING LEFT\n");
				pReturnPoint->m_fLongitude += fPerpendicularNormalizedX * HIGHLIGHT_DISTANCE_FROM_ROAD;
				pReturnPoint->m_fLatitude += fPerpendicularNormalizedY * HIGHLIGHT_DISTANCE_FROM_ROAD;
			}
			else {
// g_print("MOVING RIGHT\n");
				pReturnPoint->m_fLongitude -= fPerpendicularNormalizedX * HIGHLIGHT_DISTANCE_FROM_ROAD;
				pReturnPoint->m_fLatitude -= fPerpendicularNormalizedY * HIGHLIGHT_DISTANCE_FROM_ROAD;
			}
			return;
		}
		fRemainingDistance -= fLineSegmentDistance;
		pPointA = pPointB;
	}
	g_assert_not_reached();
}


#define BUFFER_SIZE 200
void search_road_filter_result(const gchar* pszRoadName, gint nRoadNumber, gint nRoadSuffixID, gint nAddressLeftStart, gint nAddressLeftEnd, gint nAddressRightStart, gint nAddressRightEnd, pointstring_t* pPointString)
{
	gint nRoadID = 0;
	gchar azBuffer[BUFFER_SIZE];

	mappoint_t ptAddress = {0};
	//~ gchar* pszCity = "City";
	//~ gchar* pszState = "State";
	//~ gchar* pszZIP = "ZIP";

	if(nRoadNumber == ROADSEARCH_NUMBER_NONE) {
		pointstring_walk_percentage(pPointString, 0.5, ROADSIDE_CENTER, &ptAddress);

		//~ g_snprintf(azBuffer, BUFFER_SIZE, "(%d-%d,%d-%d) %s %s", 
			//~ nAddressLeftStart, nAddressLeftEnd,
			//~ nAddressRightStart, nAddressRightEnd, 
			//~ pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));

		if(nAddressRightStart == 0 && nAddressRightEnd == 0) {
			// show no numbers if they're both 0
			g_snprintf(azBuffer, BUFFER_SIZE, "%s %s", pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
		}
		else if(nAddressRightStart < nAddressRightEnd) {
			g_snprintf(azBuffer, BUFFER_SIZE, "(%d-%d) %s %s", nAddressRightStart, nAddressRightEnd, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
		}
		else {
			// reverse start/end for the dear user :)
			g_snprintf(azBuffer, BUFFER_SIZE, "(%d-%d) %s %s", nAddressRightEnd, nAddressRightStart, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
		}
		searchwindow_add_result(nRoadID, azBuffer, &ptAddress);

		// do left side, same as right side (see above)
		if(nAddressLeftStart == 0 && nAddressLeftEnd == 0) {
			g_snprintf(azBuffer, BUFFER_SIZE, "%s %s", pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
		}
		else if(nAddressLeftStart < nAddressLeftEnd) {
			g_snprintf(azBuffer, BUFFER_SIZE, "(%d-%d) %s %s", nAddressLeftStart, nAddressLeftEnd, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
		}
		else {
			// swap address to keep smaller number to the left
			g_snprintf(azBuffer, BUFFER_SIZE, "(%d-%d) %s %s", nAddressLeftEnd, nAddressLeftStart, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
		}
		searchwindow_add_result(nRoadID, azBuffer, &ptAddress);		
	}
	else {	// else the search had a road number
		// NOTE: we have to filter out results like "97-157" when searching for "124" because it's
		// on the wrong side of the road.
//g_snprintf(azBuffer, BUFFER_SIZE, "%d %s %s", nRoadNumber, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));

		// check left side of street
		// NOTE: if search was for an even, at least one (and hopefully both) of the range should be even
		if((is_even(nRoadNumber) && (is_even(nAddressLeftStart) || is_even(nAddressLeftEnd))) ||
		   (is_odd(nRoadNumber) && (is_odd(nAddressLeftStart) || is_odd(nAddressLeftEnd))))
		{
			// check range, and the range reversed
			if(nRoadNumber >= nAddressLeftStart && nRoadNumber <= nAddressLeftEnd) {
				// MATCH: left side forward
				gint nRange = (nAddressLeftEnd - nAddressLeftStart);
				if(nRange == 0) {
					// just use road center...?
					pointstring_walk_percentage(pPointString, 0.5, ROADSIDE_LEFT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressLeftStart) / (gfloat)nRange;
					pointstring_walk_percentage(pPointString, fPercent, ROADSIDE_LEFT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, "%d %s %s", nRoadNumber, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
				searchwindow_add_result(nRoadID, azBuffer, &ptAddress);				
			}
			else if(nRoadNumber >= nAddressLeftEnd && nRoadNumber <= nAddressLeftStart) {
				// MATCH: left side backwards
				gint nRange = (nAddressLeftStart - nAddressLeftEnd);
				if(nRange == 0) {
					// just use road center...?
					pointstring_walk_percentage(pPointString, 0.5, ROADSIDE_RIGHT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressLeftEnd) / (gfloat)nRange;

					// flip percent (23 becomes 77, etc.)
					pointstring_walk_percentage(pPointString, (100.0 - fPercent), ROADSIDE_RIGHT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, "%d %s %s", nRoadNumber, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
				searchwindow_add_result(nRoadID, azBuffer, &ptAddress);
			}
		}

		// check right side of street
		if((is_even(nRoadNumber) && (is_even(nAddressRightStart) || is_even(nAddressRightEnd))) ||
		   (is_odd(nRoadNumber) && (is_odd(nAddressRightStart) || is_odd(nAddressRightEnd))))
		{
			// check range, and the range reversed
			if(nRoadNumber >= nAddressRightStart && nRoadNumber <= nAddressRightEnd) {
				// MATCH: right side forward
				gint nRange = (nAddressRightEnd - nAddressRightStart);
				if(nRange == 0) {
					// just use road center...?
					pointstring_walk_percentage(pPointString, 0.5, ROADSIDE_RIGHT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressRightStart) / (gfloat)nRange;
					pointstring_walk_percentage(pPointString, fPercent, ROADSIDE_RIGHT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, "%d %s %s", nRoadNumber, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
				searchwindow_add_result(nRoadID, azBuffer, &ptAddress);				
			}
			else if(nRoadNumber >= nAddressRightEnd && nRoadNumber <= nAddressRightStart) {
				// MATCH: right side backwards
				gint nRange = (nAddressRightStart - nAddressRightEnd);
				if(nRange == 0) {
					// just use road center...?
					pointstring_walk_percentage(pPointString, 0.5, ROADSIDE_LEFT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressRightEnd) / (gfloat)nRange;

					// flip percent (23 becomes 77, etc.)
					pointstring_walk_percentage(pPointString, (100.0 - fPercent), ROADSIDE_LEFT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, "%d %s %s", nRoadNumber, pszRoadName, map_road_suffix_itoa(nRoadSuffixID, SUFFIX_TYPE_LONG));
				searchwindow_add_result(nRoadID, azBuffer, &ptAddress);				
			}
		}
	}
}