2
/****************************************************************************
5
* AUTHOR(S): Michael Shapiro, CERL (original contributor)
6
* Roberto Flor <flor itc.it>, Markus Neteler <neteler itc.it>,
7
* Glynn Clements <glynn gclements.plus.com>, Jachym Cepicky <jachym les-ejk.cz>,
8
* Jan-Oliver Wagner <jan intevation.de>, Radim Blazek <radim.blazek gmail.com>
10
* COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
12
* This program is free software under the GNU General Public
13
* License (>=v2). Read the file COPYING that comes with GRASS
16
*****************************************************************************/
19
#include <grass/gis.h>
20
#include "local_proto.h"
21
#include <grass/glocale.h>
23
int search_points = 12;
26
int npoints_alloc = 0;
35
struct Point *points = NULL;
39
int main(int argc, char *argv[])
43
struct Cell_head window;
50
struct GModule *module;
51
struct History history;
54
struct Option *input, *npoints, *output;
59
module = G_define_module();
60
module->keywords = _("raster");
61
module->description = _("Surface generation program.");
63
parm.input = G_define_standard_option(G_OPT_R_INPUT);
65
parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
67
parm.npoints = G_define_option();
68
parm.npoints->key = "npoints";
69
parm.npoints->key_desc = "count";
70
parm.npoints->type = TYPE_INTEGER;
71
parm.npoints->required = NO;
72
parm.npoints->description = _("Number of interpolation points");
73
parm.npoints->answer = "12";
75
if (G_parser(argc, argv))
78
/* Make sure that the current projection is not lat/long */
79
if ((G_projection() == PROJECTION_LL))
80
G_fatal_error(_("Lat/long databases not supported by r.surf.idw2. Use r.surf.idw instead!"));
82
if (G_legal_filename(parm.output->answer) < 0)
83
G_fatal_error(_("<%s> is an illegal file name"), parm.output->answer);
85
if (sscanf(parm.npoints->answer, "%d", &search_points) != 1 ||
87
G_fatal_error(_("%s=%s - illegal number of interpolation points"),
88
parm.npoints->key, parm.npoints->answer);
90
list = (struct Point *)G_calloc(search_points, sizeof(struct Point));
92
/* read the elevation points from the input raster map */
93
read_cell(parm.input->answer);
96
G_fatal_error(_("%s: no data points found"), G_program_name());
97
nsearch = npoints < search_points ? npoints : search_points;
99
/* get the window, allocate buffers, etc. */
100
G_get_set_window(&window);
102
cell = G_allocate_cell_buf();
104
if ((maskfd = G_maskfd()) >= 0)
105
mask = G_allocate_cell_buf();
109
fd = G_open_cell_new(parm.output->answer);
111
G_fatal_error(_("Unable to create raster map <%s>"),
112
parm.output->answer);
114
G_message(_("Interpolating raster map <%s>... %d rows... "),
115
parm.output->answer, window.rows);
117
north = window.north - window.ns_res / 2.0;
118
for (row = 0; row < window.rows; row++) {
119
G_percent(row, window.rows, 2);
122
if (G_get_map_row(maskfd, mask, row) < 0)
123
G_fatal_error(_("Cannot get row"));
125
north += window.ns_res;
126
east = window.west - window.ew_res / 2.0;
127
for (col = 0; col < window.cols; col++) {
128
east += window.ew_res;
129
/* don't interpolate outside of the mask */
130
if (mask && mask[col] == 0) {
134
/* fill list with first nsearch points */
135
for (i = 0; i < nsearch; i++) {
136
dy = points[i].north - north;
137
dx = points[i].east - east;
138
list[i].dist = dy * dy + dx * dx;
139
list[i].z = points[i].z;
141
/* find the maximum distance */
142
maxdist = list[max = 0].dist;
143
for (n = 1; n < nsearch; n++) {
144
if (maxdist < list[n].dist)
145
maxdist = list[max = n].dist;
147
/* go thru rest of the points now */
148
for (; i < npoints; i++) {
149
dy = points[i].north - north;
150
dx = points[i].east - east;
151
dist = dy * dy + dx * dx;
153
if (dist < maxdist) {
154
/* replace the largest dist */
155
list[max].z = points[i].z;
156
list[max].dist = dist;
157
maxdist = list[max = 0].dist;
158
for (n = 1; n < nsearch; n++) {
159
if (maxdist < list[n].dist)
160
maxdist = list[max = n].dist;
168
for (n = 0; n < nsearch; n++) {
169
if ((dist = list[n].dist)) {
170
sum1 += list[n].z / dist;
179
cell[col] = (CELL) (sum1 / sum2 + 0.5);
182
G_put_raster_row(fd, cell, CELL_TYPE);
189
/* writing history file */
190
G_short_history(parm.output->answer, "raster", &history);
191
G_command_history(&history);
192
G_write_history(parm.output->answer, &history);
198
int newpoint(double z, double east, double north)
200
if (npoints_alloc <= npoints) {
201
npoints_alloc += 1024;
202
points = (struct Point *)G_realloc(points,
204
sizeof(struct Point));
206
points[npoints].north = north;
207
points[npoints].east = east;
208
points[npoints].z = z;