forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kerneldensity.c
345 lines (310 loc) · 11.4 KB
/
kerneldensity.c
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
/******************************************************************************
*
* Project: MapServer
* Purpose: KernelDensity layer implementation and related functions.
* Author: Thomas Bonfort and the MapServer team.
*
******************************************************************************
* Copyright (c) 2014 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include "mapserver.h"
#include <float.h>
#ifdef USE_GDAL
#include "gdal.h"
void gaussian_blur(float *values, int width, int height, int radius) {
float *tmp = (float*)msSmallMalloc(width*height*sizeof(float));
int length = radius*2+1;
float *kernel = (float*)msSmallMalloc(length*sizeof(float));
float sigma=radius/3.0;
float a=1.0/ sqrt(2.0*M_PI*sigma*sigma);
float den=2.0*sigma*sigma;
int i,x,y;
for (i=0; i<length; i++) {
float x=i - radius;
float v=a * exp(-(x*x) / den);
kernel[i]=v;
}
memset(tmp,0,width*height*sizeof(float));
for(y=0; y<height; y++) {
float* src_row=values + width*y;
float* dst_row=tmp + width*y;
for(x=radius; x<width-radius; x++) {
float accum=0;
for(i=0; i<length; i++) {
accum+=src_row[x+i-radius] * kernel[i];
}
dst_row[x]=accum;
}
}
for(x=0; x<width; x++) {
float* src_col=tmp+x;
float* dst_col=values+x;
for(y=radius; y<height-radius; y++) {
float accum=0;
for (i=0; i<length; i++) {
accum+=src_col[width*(y+i-radius)] * kernel[i];
}
dst_col[y*width]=accum;
}
}
free(tmp);
free(kernel);
}
int msComputeKernelDensityDataset(mapObj *map, imageObj *image, layerObj *kerneldensity_layer, void **hDSvoid, void **cleanup_ptr) {
int status,layer_idx, i,j, nclasses=0, have_sample=0;
rectObj searchrect;
shapeObj shape;
layerObj *layer;
float *values;
int radius = 10, im_width = image->width, im_height = image->height;
int expand_searchrect=1;
float normalization_scale=0.0;
double invcellsize = 1.0 / map->cellsize, georadius=0;
float valmax=FLT_MIN, valmin=FLT_MAX;
unsigned char *iValues;
GDALDatasetH hDS;
const char *pszProcessing;
int *classgroup = NULL;
assert(kerneldensity_layer->connectiontype == MS_KERNELDENSITY);
*cleanup_ptr = NULL;
if(!kerneldensity_layer->connection || !*kerneldensity_layer->connection) {
msSetError(MS_MISCERR, "msComputeKernelDensityDataset()", "KernelDensity layer has no CONNECTION defined");
return MS_FAILURE;
}
pszProcessing = msLayerGetProcessingKey( kerneldensity_layer, "KERNELDENSITY_RADIUS" );
if(pszProcessing)
radius = atoi(pszProcessing);
else
radius = 10;
pszProcessing = msLayerGetProcessingKey( kerneldensity_layer, "KERNELDENSITY_COMPUTE_BORDERS" );
if(pszProcessing && strcasecmp(pszProcessing,"OFF"))
expand_searchrect = 1;
else
expand_searchrect = 0;
pszProcessing = msLayerGetProcessingKey( kerneldensity_layer, "KERNELDENSITY_NORMALIZATION" );
if(!pszProcessing || !strcasecmp(pszProcessing,"AUTO"))
normalization_scale = 0.0;
else {
normalization_scale = atof(pszProcessing);
if(normalization_scale != 0) {
normalization_scale = 1.0 / normalization_scale;
} else {
normalization_scale = 1.0;
}
}
layer_idx = msGetLayerIndex(map,kerneldensity_layer->connection);
if(layer_idx == -1) {
int nLayers, *aLayers;
aLayers = msGetLayersIndexByGroup(map, kerneldensity_layer->connection, &nLayers);
if(!aLayers || !nLayers) {
msSetError(MS_MISCERR, "KernelDensity layer (%s) references unknown layer (%s)", "msComputeKernelDensityDataset()",
kerneldensity_layer->name,kerneldensity_layer->connection);
return (MS_FAILURE);
}
for(i=0; i<nLayers; i++) {
layer_idx = aLayers[i];
layer = GET_LAYER(map, layer_idx);
if(msScaleInBounds(map->scaledenom, layer->minscaledenom, layer->maxscaledenom))
break;
}
free(aLayers);
if(i == nLayers) {
msSetError(MS_MISCERR, "KernelDensity layer (%s) references no layer for current scale", "msComputeKernelDensityDataset()",
kerneldensity_layer->name);
return (MS_FAILURE);
}
} else {
layer = GET_LAYER(map, layer_idx);
}
/* open the linked layer */
status = msLayerOpen(layer);
if(status != MS_SUCCESS) return MS_FAILURE;
status = msLayerWhichItems(layer, MS_FALSE, NULL);
if(status != MS_SUCCESS) {
msLayerClose(layer);
return MS_FAILURE;
}
/* identify target shapes */
if(layer->transform == MS_TRUE) {
searchrect = map->extent;
if(expand_searchrect) {
georadius = radius * map->cellsize;
searchrect.minx -= georadius;
searchrect.miny -= georadius;
searchrect.maxx += georadius;
searchrect.maxy += georadius;
im_width += 2 * radius;
im_height += 2 * radius;
}
}
else {
searchrect.minx = searchrect.miny = 0;
searchrect.maxx = map->width-1;
searchrect.maxy = map->height-1;
}
#ifdef USE_PROJ
layer->project = msProjectionsDiffer(&(layer->projection), &(map->projection));
if(layer->project)
msProjectRect(&map->projection, &layer->projection, &searchrect); /* project the searchrect to source coords */
#endif
status = msLayerWhichShapes(layer, searchrect, MS_FALSE);
if(status == MS_DONE) { /* no overlap */
msLayerClose(layer);
return MS_SUCCESS;
} else if(status != MS_SUCCESS) {
msLayerClose(layer);
return MS_FAILURE;
}
values = (float*) msSmallCalloc(im_width * im_height, sizeof(float));
if(layer->classgroup && layer->numclasses > 0)
classgroup = msAllocateValidClassGroups(layer, &nclasses);
msInitShape(&shape);
while((status = msLayerNextShape(layer, &shape)) == MS_SUCCESS) {
int l,p,s,c;
double weight = 1.0;
#ifdef USE_PROJ
if(layer->project)
msProjectShape(&layer->projection, &map->projection, &shape);
#endif
/* the weight for the sample is set to 1.0 by default. If the
* layer has some classes defined, we will read the weight from
* the class->style->size (which can be binded to an attribute)
*/
if(layer->numclasses > 0) {
c = msShapeGetClass(layer, map, &shape, classgroup, nclasses);
if((c == -1) || (layer->class[c]->status == MS_OFF)) {
goto nextshape; /* no class matched, skip */
}
for (s = 0; s < layer->class[c]->numstyles; s++) {
if (msScaleInBounds(map->scaledenom,
layer->class[c]->styles[s]->minscaledenom,
layer->class[c]->styles[s]->maxscaledenom)) {
if(layer->class[c]->styles[s]->bindings[MS_STYLE_BINDING_SIZE].index != -1) {
weight = atof(shape.values[layer->class[c]->styles[s]->bindings[MS_STYLE_BINDING_SIZE].index]);
} else {
weight = layer->class[c]->styles[s]->size;
}
break;
}
}
if(s == layer->class[c]->numstyles) {
/* no style in scale bounds */
goto nextshape;
}
}
for(l=0; l<shape.numlines; l++) {
for(p=0; p<shape.line[l].numpoints; p++) {
int x = MS_MAP2IMAGE_XCELL_IC(shape.line[l].point[p].x, map->extent.minx - georadius, invcellsize);
int y = MS_MAP2IMAGE_YCELL_IC(shape.line[l].point[p].y, map->extent.maxy + georadius, invcellsize);
if(x>=0 && y>=0 && x<im_width && y<im_height) {
float *value = values + y * im_width + x;
(*value) += weight;
have_sample = 1;
}
}
}
nextshape:
msFreeShape(&shape);
}
msLayerClose(layer);
if(status == MS_DONE) {
status = MS_SUCCESS;
} else {
status = MS_FAILURE;
}
if(have_sample) { /* no use applying the filtering kernel if we have no samples */
gaussian_blur(values,im_width, im_height, radius);
if(normalization_scale == 0.0) { /* auto normalization */
for (j=radius; j<im_height-radius; j++) {
for (i=radius; i<im_width-radius; i++) {
float val = values[j*im_width + i];
if(val >0 && val>valmax) {
valmax = val;
}
if(val>0 && val<valmin) {
valmin = val;
}
}
}
} else {
valmin = 0;
valmax = normalization_scale;
}
}
if(have_sample && expand_searchrect) {
iValues = msSmallMalloc(image->width*image->height*sizeof(unsigned char));
for (j=0; j<image->height; j++) {
for (i=0; i<image->width; i++) {
float norm=(values[(j+radius)*im_width + i + radius] - valmin) / valmax;
int v=255 * norm;
if (v<0) v=0;
else if (v>255) v=255;
iValues[j*image->width + i] = v;
}
}
} else {
iValues = msSmallCalloc(1,image->width*image->height*sizeof(unsigned char));
if(have_sample) {
for (j=radius; j<image->height-radius; j++) {
for (i=radius; i<image->width-radius; i++) {
float norm=(values[j*im_width + i] - valmin) / valmax;
int v=255 * norm;
if (v<0) v=0;
else if (v>255) v=255;
iValues[j*image->width + i]=v;
}
}
}
}
free(values);
{
char ds_string [1024];
double adfGeoTransform[6];
snprintf(ds_string,1024,"MEM:::DATAPOINTER=%p,PIXELS=%u,LINES=%u,BANDS=1,DATATYPE=Byte,PIXELOFFSET=1,LINEOFFSET=%u",
iValues,image->width,image->height,image->width);
hDS = GDALOpenShared( ds_string, GA_ReadOnly );
if(hDS==NULL) {
msSetError(MS_MISCERR,"msComputeKernelDensityDataset()","failed to create in-memory gdal dataset for interpolated data");
status = MS_FAILURE;
free(iValues);
}
adfGeoTransform[0] = map->extent.minx - map->cellsize * 0.5; /* top left x */
adfGeoTransform[1] = map->cellsize;/* w-e pixel resolution */
adfGeoTransform[2] = 0; /* 0 */
adfGeoTransform[3] = map->extent.maxy + map->cellsize * 0.5;/* top left y */
adfGeoTransform[4] =0; /* 0 */
adfGeoTransform[5] = -map->cellsize;/* n-s pixel resolution (negative value) */
GDALSetGeoTransform(hDS,adfGeoTransform);
*hDSvoid = hDS;
*cleanup_ptr = (void*)iValues;
}
return status;
}
#else
int msComputeKernelDensityDataset(mapObj *map, imageObj *image, layerObj *layer, void **hDSvoid, void **cleanup_ptr) {
msSetError(MS_MISCERR,"msComputeKernelDensityDataset()", "KernelDensity layers require GDAL support, however GDAL support is not compiled in this build");
return MS_FAILURE;
}
#endif
int msCleanupKernelDensityDataset(mapObj *map, imageObj *image, layerObj *layer, void *cleanup_ptr) {
free(cleanup_ptr);
return MS_SUCCESS;
}