summaryrefslogtreecommitdiff
path: root/src/search_road.c
blob: a46f16d10b502584ef963f39e402aa60098917b3 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/***************************************************************************
 *            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 "main.h"
#include "db.h"
#include "util.h"
#include "search.h"
#include "search_road.h"
#include "road.h"
#include "glyph.h"
#include "searchwindow.h"		// for defines about glyph size

#define ROAD_RESULT_SUGGESTED_ZOOMLEVEL		(37)

#define FORMAT_ROAD_RESULT_WITHOUT_NUMBER 	("%s %s\n%s")
#define FORMAT_ROAD_RESULT_WITH_NUMBER 		("%d %s %s\n%s")

typedef struct {
	gint nNumber;			// house number	eg. 51
	gchar* pszRoadName;	// road name eg. "Washington"
	gint nCityID;			//
	gint nStateID;
	gchar* pszZIPCode;
	gint nSuffixID;		// a number representing eg. Ave
} roadsearch_t;

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


static glyph_t* g_SearchResultTypeRoadGlyph = NULL;

void search_road_init()
{
	g_SearchResultTypeRoadGlyph = glyph_load_at_size("search-result-type-road", SEARCHWINDOW_SEARCH_RESULT_GLYPH_WIDTH, SEARCHWINDOW_SEARCH_RESULT_GLYPH_HEIGHT);
}


//#define ROAD_MIN_LENGTH_FOR_WILDCARD_SEARCH	(4)	  wildcard search no longer used

gboolean search_address_match_zipcode(const gchar* pszWord)
{
	// very US-centric right now
	gint nLen = strlen(pszWord);
	if(nLen < 4 || nLen > 5) return FALSE;

	gint i;
	for(i=0 ; i<nLen ; i++) {
		if(!g_ascii_isdigit(pszWord[i])) {
			return FALSE;
		}
	}
	return TRUE;
}

// prototypes

void search_road_on_words(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, const gchar* pszCityNameLeft, const gchar* pszCityNameRight, const gchar* pszStateNameLeft, const gchar* pszStateNameRight, const gchar* pszZIPLeft, const gchar* pszZIPRight, GArray* pMapPointsArray);

// functions

void search_road_execute(const gchar* pszSentence)
{
	// Create an array of the words
	gchar** aWords = g_strsplit(pszSentence," ", 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(gchar** aWords, gint nWordCount)
{
	g_assert(nWordCount > 0);
	roadsearch_t roadsearch = {0};

	// 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;

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

	// Claim zip code, if present
	if(search_address_match_zipcode(aWords[iLast])) {
		//g_print("matched ZIP %s\n", aWords[iLast]);
		roadsearch.pszZIPCode = aWords[iLast];

		iLast--;	// last word taken
		nRemainingWordCount--;
	}

	if(nRemainingWordCount == 0) {
		// do a zip code search and return
		g_print("TODO: zip code search\n");
		return;
	}

	// See if we can match a state name   XXX: We need to match multi-word state names
	gboolean bGotStateName = FALSE;
	if(nRemainingWordCount >= 3) {
		// try two-word state name
		gchar* pszStateName = util_g_strjoinv_limit(" ", aWords, iLast-1, iLast);
		//g_print("trying two-word state name '%s'\n", pszStateName);

		if(db_state_get_id(pszStateName, &roadsearch.nStateID)) {
			//g_print("matched state name!\n");
			iLast -= 2;	// last TWO words taken
			nRemainingWordCount -= 2;
		}
		g_free(pszStateName);
	}
	// try a one-word state name
	if(bGotStateName == FALSE && nRemainingWordCount >= 2) {
		//g_print("trying one-word state name '%s'\n", aWords[iLast]);
		if(db_state_get_id(aWords[iLast], &roadsearch.nStateID)) {
			//g_print("matched state name!\n");
			iLast--;	// last word taken
			nRemainingWordCount--;
		}
	}

	// try to match city name
	gint nCityNameLength;
	for(nCityNameLength = 5 ; nCityNameLength >= 1 ; nCityNameLength--) {
		if(nRemainingWordCount > nCityNameLength) {
			gchar* pszCityName = util_g_strjoinv_limit(" ", aWords, iLast - (nCityNameLength-1), iLast);

			if(db_city_get_id(pszCityName, roadsearch.nStateID, &roadsearch.nCityID)) {
				iLast -= nCityNameLength;	// several words taken :)
				nRemainingWordCount -= nCityNameLength;

				// success
				g_free(pszCityName);
				break;
			}
			else {
				// failure
				g_free(pszCityName);
				continue;
			}
		}
	}

	// road name suffix (eg. "ave")
	if(nRemainingWordCount >= 2) {
		gint nSuffixID;

		if(road_suffix_atoi(aWords[iLast], &nSuffixID)) {
			// matched
			roadsearch.nSuffixID = nSuffixID;
			iLast--;
			nRemainingWordCount--;
		}
	}

	if(nRemainingWordCount > 0) {
		roadsearch.pszRoadName = util_g_strjoinv_limit(" ", aWords, iFirst, iLast);
		search_road_on_roadsearch_struct(&roadsearch);
	}
	else {
		// oops, no street name
		//g_print("no street name found in search\n");
	}
	g_free(roadsearch.pszRoadName);
}

void search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch)
{
	//
	// Assemble the various optional clauses for the SQL statement
	//
	gchar* pszAddressClause;
	if(pRoadSearch->nNumber != ROADSEARCH_NUMBER_NONE) {
		pszAddressClause = g_strdup_printf(
			" AND ("
			"(%d BETWEEN Road0.AddressLeftStart AND Road0.AddressLeftEnd)"
			" OR (%d BETWEEN Road0.AddressLeftEnd AND Road0.AddressLeftStart)"
			" OR (%d BETWEEN Road0.AddressRightStart AND Road0.AddressRightEnd)"
			" OR (%d BETWEEN Road0.AddressRightEnd AND Road0.AddressRightStart)"
			")", pRoadSearch->nNumber, pRoadSearch->nNumber,
				 pRoadSearch->nNumber, pRoadSearch->nNumber);
	}
	else {
		pszAddressClause = g_strdup("");
	}

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

	gchar* pszZIPClause;
	if(pRoadSearch->pszZIPCode != NULL) {
		gchar* pszSafeZIP = db_make_escaped_string(pRoadSearch->pszZIPCode);
		pszZIPClause = g_strdup_printf(" AND (Road0.ZIPCodeLeft='%s' OR Road0.ZIPCodeRight='%s')", pszSafeZIP, pszSafeZIP);
		db_free_escaped_string(pszSafeZIP);
	}
	else {
		pszZIPClause = g_strdup("");
	}

	gchar* pszCityClause;
	if(pRoadSearch->nCityID != 0) {
		pszCityClause = g_strdup_printf(" AND (Road0.CityLeftID=%d OR Road0.CityRightID=%d)", pRoadSearch->nCityID, pRoadSearch->nCityID);
	}
	else {
		pszCityClause = g_strdup("");
	}

	gchar* pszStateClause;
	if(pRoadSearch->nStateID != 0) {
		pszStateClause = g_strdup_printf(" AND (CityLeft.StateID=%d OR CityRight.StateID=%d)", pRoadSearch->nStateID, pRoadSearch->nStateID);
	}
	else {
		pszStateClause = g_strdup("");
	}

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

	gchar* pszSafeRoadName = db_make_escaped_string(pRoadSearch->pszRoadName);
	//g_print("pRoadSearch->pszRoadName = %s, pszSafeRoadName = %s\n", pRoadSearch->pszRoadName, pszSafeRoadName);

	// XXX: Should we use soundex()? (http://en.wikipedia.org/wiki/Soundex)
	gchar* pszRoadNameCondition = g_strdup_printf("RoadName.Name='%s'", pszSafeRoadName);

	// Now we use only Soundex
	//pszRoadNameCondition = g_strdup_printf("RoadName.NameSoundex = SUBSTRING(SOUNDEX('%s') FROM 1 FOR 10)", pszSafeRoadName);

	gchar* pszQuery = g_strdup_printf(
		"SELECT 0 AS ID, RoadName.Name, RoadName.SuffixID, AsBinary(Road0.Coordinates), Road0.AddressLeftStart, Road0.AddressLeftEnd, Road0.AddressRightStart, Road0.AddressRightEnd, CityLeft.Name, CityRight.Name"
		", StateLeft.Code, StateRight.Code, Road0.ZIPCodeLeft, Road0.ZIPCodeRight"
		" FROM RoadName"
		" LEFT JOIN Road0 ON (RoadName.ID=Road0.RoadNameID%s)"					// address # clause
		// left side
	    " LEFT JOIN City AS CityLeft ON (Road0.CityLeftID=CityLeft.ID)"
		" LEFT JOIN State AS StateLeft ON (CityLeft.StateID=StateLeft.ID)"
		// right side
		" LEFT JOIN City AS CityRight ON (Road0.CityRightID=CityRight.ID)"
		" LEFT JOIN State AS StateRight ON (CityRight.StateID=StateRight.ID)"
		" WHERE %s"
//		" WHERE RoadName.Name='%s'"
		" AND Road0.TypeID IS NOT NULL"	// (any field will do) don't include rows where the Road didn't match
		// begin clauses
		"%s"
		"%s"
		"%s"
		"%s"
		" LIMIT %d;",
			   pszAddressClause,
			
			   pszRoadNameCondition,

			   // clauses
			   pszSuffixClause,
			   pszZIPClause,
			   pszCityClause,
			   pszStateClause,
			SEARCH_RESULT_COUNT_LIMIT + 1);

	// free intermediate strings
	db_free_escaped_string(pszSafeRoadName);
	g_free(pszAddressClause);
	g_free(pszRoadNameCondition);
	g_free(pszSuffixClause);
	g_free(pszZIPClause);
	g_free(pszCityClause);
	g_free(pszStateClause);
	
	//g_print("SQL: %s\n", azQuery);

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

		// get result rows!
		gint nCount = 0;		
		while((aRow = db_fetch_row(pResultSet))) {
			// [0] Road.ID
			// [1] RoadName.Name
			// [2] RoadName.SuffixID
			// [3] AsBinary(Road.Coordinates),
			// [4] Road.AddressLeftStart,
			// [5] Road.AddressLeftEnd
			// [6] Road.AddressRightStart
			// [7] Road.AddressRightEnd,
			// [8] CityLeft.Name,
			// [9] CityRight.Name

			// [10] StateLeft.Name,
			// [11] StateRight.Name

			// [12] ZIPLeft
			// [13] ZIPRight

			nCount++;
			if(nCount <= SEARCH_RESULT_COUNT_LIMIT) {

				GArray* pMapPointsArray = g_array_new(FALSE, FALSE, sizeof(mappoint_t));
				maprect_t r;
				db_parse_wkb_linestring(aRow[3], pMapPointsArray, &r);
				search_road_filter_result(aRow[1], pRoadSearch->nNumber, atoi(aRow[2]), atoi(aRow[4]), atoi(aRow[5]), atoi(aRow[6]), atoi(aRow[7]), aRow[8], aRow[9], aRow[10], aRow[11], aRow[12], aRow[13], pMapPointsArray);
				//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]);

				g_array_free(pMapPointsArray, TRUE);
			}
		}
		db_free_result(pResultSet);
	}
	else {
		g_print("road search failed\n");
	}
	g_free(pszQuery);
}

// XXX: doesn't map.c have something like this? :)
static gfloat point_calc_distance(mappoint_t* pA, mappoint_t* pB)
{
	gdouble fRise = pB->fLatitude - pA->fLatitude;
	gdouble fRun = pB->fLongitude - pA->fLongitude;
	return sqrt((fRun*fRun) + (fRise*fRise));
}

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

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

static void mappoint_array_walk_percentage(GArray* pMapPointsArray, gdouble fPercent, ERoadSide eRoadSide, mappoint_t* pReturnPoint)
{
	gint i;
	if(pMapPointsArray->len < 2) {
		g_assert_not_reached();
	}

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

		fTotalDistance += point_calc_distance(pPointA, pPointB);

		pPointA = pPointB;
	}

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

	pPointA = &g_array_index(pMapPointsArray, mappoint_t, 0);
	for(i=1 ; i<pMapPointsArray->len ; i++) {
		pPointB = &g_array_index(pMapPointsArray, mappoint_t, 1);

		gfloat fLineSegmentDistance = point_calc_distance(pPointA, pPointB);
		if(fRemainingDistance <= fLineSegmentDistance || (i == pMapPointsArray->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->fLatitude - pPointA->fLatitude);
			gfloat fRun = (pPointB->fLongitude - pPointA->fLongitude);

			// Calculate the a point on the center of the line segment
			// the right percent along the road
			pReturnPoint->fLongitude = pPointA->fLongitude + (fRun * fPercentOfLine);
			pReturnPoint->fLatitude = pPointA->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->fLongitude += fPerpendicularNormalizedX * HIGHLIGHT_DISTANCE_FROM_ROAD;
				pReturnPoint->fLatitude += fPerpendicularNormalizedY * HIGHLIGHT_DISTANCE_FROM_ROAD;
			}
			else {
// g_print("MOVING RIGHT\n");
				pReturnPoint->fLongitude -= fPerpendicularNormalizedX * HIGHLIGHT_DISTANCE_FROM_ROAD;
				pReturnPoint->fLatitude -= fPerpendicularNormalizedY * HIGHLIGHT_DISTANCE_FROM_ROAD;
			}
			return;
		}
		fRemainingDistance -= fLineSegmentDistance;
		pPointA = pPointB;
	}
	g_assert_not_reached();
}

#if ROADSTER_DEAD_CODE
static gint min4(gint a, gint b, gint c, gint d)
{
	gint x = min(a,b);
	gint y = min(c,d);
	return min(x,y);
}

static gint max4(gint a, gint b, gint c, gint d)
{
	gint x = max(a,b);
	gint y = max(c,d);
	return max(x,y);
}
#endif /* ROADSTER_DEAD_CODE */

//
// XXX: the SQL doesn't require all fields be set for THE SAME SIDE
// 		do we need to filter out records where each side matches some of the criteria but not all?
//
#define BUFFER_SIZE 200
void search_road_filter_result(
		const gchar* pszRoadName, gint nRoadNumber, gint nRoadSuffixID,
		gint nAddressLeftStart, gint nAddressLeftEnd,
		gint nAddressRightStart, gint nAddressRightEnd,
		const gchar* pszCityNameLeft, const gchar* pszCityNameRight,
		const gchar* pszStateNameLeft, const gchar* pszStateNameRight,
		const gchar* pszZIPLeft, const gchar* pszZIPRight,
		GArray* pMapPointsArray)
{
	gint nRoadID = 0;
	gchar azBuffer[BUFFER_SIZE];

	mappoint_t ptAddress = {0};

//     pszStateNameLeft = "(st)";
//     pszStateNameRight = "(st)";

	// set City, State, Zip text to be used for each side of the road "City, State, ZIP"
	gchar* pszCSZLeft = g_strdup_printf("%s%s%s%s%s",
										(pszCityNameLeft != NULL) ? pszCityNameLeft : "",
										(pszStateNameLeft != NULL) ? ", " : "",
										(pszStateNameLeft != NULL) ? pszStateNameLeft : "",
										(strcmp(pszZIPLeft, "00000") == 0) ? "" : ", ",
										(strcmp(pszZIPLeft, "00000") == 0) ? "" : pszZIPLeft);
	gchar* pszCSZRight = g_strdup_printf("%s%s%s%s%s",
										 (pszCityNameRight != NULL) ? pszCityNameRight : "",
										 (pszStateNameRight != NULL) ? ", " : "",
										 (pszStateNameRight != NULL) ? pszStateNameRight : "",
										 (strcmp(pszZIPRight, "00000") == 0) ? "" : ", ",
										 (strcmp(pszZIPRight, "00000") == 0) ? "" : pszZIPRight);
	
	// consider the longer of the two to be better-- (for joined results)
//	gchar* pszCSZBetter = (strlen(pszCSZLeft) > strlen(pszCSZRight)) ? pszCSZLeft : pszCSZRight;

	if(nRoadNumber == ROADSEARCH_NUMBER_NONE) {
		// Right in the center
		mappoint_array_walk_percentage(pMapPointsArray, 0.5, ROADSIDE_CENTER, &ptAddress);

//         gint nStart = min4(nAddressLeftStart, nAddressLeftEnd, nAddressRightStart, nAddressRigtEnd);
//         gint nEnd = min4(nAddressLeftStart, nAddressLeftEnd, nAddressRightStart, nAddressRigtEnd);

/*
		if(nAddressRightStart == 0 && nAddressRightEnd == 0) {
*/
			// show no numbers if they're both 0
			g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITHOUT_NUMBER,
					   pszRoadName,
					   road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG),
					   pszCSZRight);
/*
		}
		else if(nAddressRightStart < nAddressRightEnd) {
			g_snprintf(azBuffer, BUFFER_SIZE, "%d-%d %s %s\n%s", nAddressRightStart, nAddressRightEnd, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZRight);
		}
		else {
			// reverse start/end for the dear user :)
			g_snprintf(azBuffer, BUFFER_SIZE, "%d-%d %s %s\n%s", nAddressRightEnd, nAddressRightStart, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZRight);
		}
		searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL);

		// do left side, same as right side (see above)
		if(nAddressLeftStart == 0 && nAddressLeftEnd == 0) {
			g_snprintf(azBuffer, BUFFER_SIZE, "%s %s\n%s", pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft);
		}
		else if(nAddressLeftStart < nAddressLeftEnd) {
			g_snprintf(azBuffer, BUFFER_SIZE, "%d-%d %s %s\n%s", nAddressLeftStart, nAddressLeftEnd, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft);
		}
		else {
			// swap address to keep smaller number to the left
			g_snprintf(azBuffer, BUFFER_SIZE, "%d-%d %s %s\n%s", nAddressLeftEnd, nAddressLeftStart, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft);
		}
*/		searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL);		
	}
	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, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_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...?
					mappoint_array_walk_percentage(pMapPointsArray, 0.5, ROADSIDE_LEFT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressLeftStart) / (gfloat)nRange;
					mappoint_array_walk_percentage(pMapPointsArray, fPercent, ROADSIDE_LEFT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft);
				searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL);				
			}
			else if(nRoadNumber >= nAddressLeftEnd && nRoadNumber <= nAddressLeftStart) {
				// MATCH: left side backwards
				gint nRange = (nAddressLeftStart - nAddressLeftEnd);
				if(nRange == 0) {
					// just use road center...?
					mappoint_array_walk_percentage(pMapPointsArray, 0.5, ROADSIDE_RIGHT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressLeftEnd) / (gfloat)nRange;

					// flip percent (23 becomes 77, etc.)
					mappoint_array_walk_percentage(pMapPointsArray, (100.0 - fPercent), ROADSIDE_RIGHT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft);
				searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL);
			}
		}

		// 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...?
					mappoint_array_walk_percentage(pMapPointsArray, 0.5, ROADSIDE_RIGHT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressRightStart) / (gfloat)nRange;
					mappoint_array_walk_percentage(pMapPointsArray, fPercent, ROADSIDE_RIGHT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZRight);
				searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL);
			}
			else if(nRoadNumber >= nAddressRightEnd && nRoadNumber <= nAddressRightStart) {
				// MATCH: right side backwards
				gint nRange = (nAddressRightStart - nAddressRightEnd);
				if(nRange == 0) {
					// just use road center...?
					mappoint_array_walk_percentage(pMapPointsArray, 0.5, ROADSIDE_LEFT, &ptAddress);
				}
				else {
					gfloat fPercent = (gfloat)(nRoadNumber - nAddressRightEnd) / (gfloat)nRange;

					// flip percent (23 becomes 77, etc.)
					mappoint_array_walk_percentage(pMapPointsArray, (100.0 - fPercent), ROADSIDE_LEFT, &ptAddress);
				}
				g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZRight);
				searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL);
			}
		}
	}
	g_free(pszCSZLeft);
	g_free(pszCSZRight);
}