File: llsearch.c

package info (click to toggle)
drawmap 1.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 472 kB
  • ctags: 396
  • sloc: ansic: 4,194; makefile: 61
file content (141 lines) | stat: -rw-r--r-- 4,695 bytes parent folder | download
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
/*
 * =========================================================================
 * llsearch - A program that extracts a subset of the data in a GNIS file.
 * Copyright (c) 1997  Fred M. Erickson
 *
 * 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, 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 * =========================================================================
 *
 *
 * This is a filter to search a GNIS file for any entries within
 * the specified latitude/longitude box.
 *
 * Each GNIS file is for a separate state, but some of the larger
 * states span many degrees of latitude and longitude.
 * It is convenient to be able to pull out just the data you need
 * for a given map.
 *
 * The program reads from stdin and writes to stdout.
 */
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

ssize_t buf_read(int, void *, size_t);
ssize_t buf_write(int, const void *, size_t);
ssize_t get_a_line(int, void *, size_t);
double lat_conv(unsigned char *);
double lon_conv(unsigned char *);



main(int argc, char *argv[])
{
	unsigned char buf[16384];
	unsigned char *tok_ptr;
	double latitude_low;
	double latitude_high;
	double longitude_low;
	double longitude_high;
	double latitude;
	double longitude;
	ssize_t ret_val;
	int i;

	if (argc != 5)  {
		fprintf(stderr, "Usage:  %s latitude_low longitude_low latitude_high longitude_high\n", argv[0]);
		fprintf(stderr, "        (The latitude/longitude values are in decimal degrees.)\n");
		fprintf(stderr, "        (West longitude is negative and south latitude is negative.)\n");
		fprintf(stderr, "        (%s reads from stdin and writes to stdout.)\n", argv[0]);
		exit(0);
	}
	latitude_low = strtod(argv[1], (char **)0);	/* We assume that these are in decimal degrees */
	longitude_low = strtod(argv[2], (char **)0);	/* We assume that these are in decimal degrees */
	latitude_high = strtod(argv[3], (char **)0);	/* We assume that these are in decimal degrees */
	longitude_high = strtod(argv[4], (char **)0);	/* We assume that these are in decimal degrees */

	if (latitude_low > latitude_high)  {
		latitude = latitude_high;
		latitude_high = latitude_low;
		latitude_low = latitude;
	}
	if (longitude_low > longitude_high)  {
		longitude = longitude_high;
		longitude_high = longitude_low;
		longitude_low = longitude;
	}

	if ((latitude_high < -90.0) || (latitude_high > 90.0) ||
	    (latitude_low < -90.0) || (latitude_low > 90.0) ||
	    (longitude_high < -180.0) || (longitude_high > 180.0) ||
	    (longitude_low < -180.0) || (longitude_low > 180.0))  {
		fprintf(stderr, "Error:  Parameters appear incorrect\n");
		exit(0);
	}
	if ((latitude_high < latitude_low) || (longitude_high < longitude_low))  {
		fprintf(stderr, "Error:  Parameters appear incorrect\n");
		exit(0);
	}

	while ((ret_val = get_a_line(0, buf, 16383)) > 0)  {
		buf[ret_val] == '\0';

		/*
		 * We need to figure out whether it is an old-style or new-style record.
		 */
		if ((tok_ptr = strstr(buf, "','")) != (unsigned char *)0)  {
			/* New-style record. */
			tok_ptr += 3;
			for (i = 0; i < 6; i++)  {
				if ((tok_ptr = strstr(tok_ptr, "','")) != (unsigned char *)0)  {
					tok_ptr += 3;
				}
				else  {
					break;
				}
			}
			if ((tok_ptr != (unsigned char *)0) && (*tok_ptr != '\0'))  {
				latitude = atof(tok_ptr);
				if (((tok_ptr = strstr(tok_ptr, "','")) != (unsigned char *)0) && (*(tok_ptr + 3) != '\0'))  {
					tok_ptr += 3;
					longitude = atof(tok_ptr);
				}
			}
			else  {
				latitude = 0.0;
				longitude = 0.0;
			}
		}
		else  {
			/* Old-style record. */
			/*
			 * Note:  We assume latitude_low, longitude_low, latitude_high, and longitude_high
			 * were entered in decimal degrees.
			 * latitude and longitude from the old-style GNIS files, however are in DDDMMSS format, and
			 * require special conversion functions.
			 */
			latitude = lat_conv(&buf[149]);
			longitude = lon_conv(&buf[156]);
		}

		if ((latitude >= latitude_low) &&
		    (latitude <= latitude_high) &&
		    (longitude >= longitude_low) &&
		    (longitude <= longitude_high))  {
			write(1, buf, ret_val);
		}
	}
}