[59] | 1 | ;+ |
---|
[232] | 2 | ; |
---|
[136] | 3 | ; @file_comments |
---|
[125] | 4 | ; to find if an (x,y) point is in a quadrilateral (x1,x2,x3,x4) |
---|
[59] | 5 | ; |
---|
[226] | 6 | ; @categories |
---|
[157] | 7 | ; Grid |
---|
[59] | 8 | ; |
---|
[118] | 9 | ; @param x {in}{required} |
---|
| 10 | ; @param y {in}{required} |
---|
[136] | 11 | ; the coordinates of the point we want to know where it is. |
---|
| 12 | ; Must be a scalar if /ONSPHERE activated else can be scalar or array. |
---|
[59] | 13 | ; |
---|
[118] | 14 | ; @param x1 {in}{required} |
---|
| 15 | ; @param y1 {in}{required} |
---|
| 16 | ; @param x2 {in}{required} |
---|
| 17 | ; @param y2 {in}{required} |
---|
| 18 | ; @param x3 {in}{required} |
---|
| 19 | ; @param y3 {in}{required} |
---|
| 20 | ; @param x4 {in}{required} |
---|
| 21 | ; @param y4 {in}{required} |
---|
[136] | 22 | ; the coordinates of the quadrilateral given in the CLOCKWISE order. |
---|
| 23 | ; Scalar or array. |
---|
[59] | 24 | ; |
---|
[136] | 25 | ; @keyword DOUBLE |
---|
| 26 | ; use double precision to perform the computation |
---|
[59] | 27 | ; |
---|
[136] | 28 | ; @keyword ONSPHERE |
---|
[163] | 29 | ; to specify that the quadrilateral are on a sphere and |
---|
| 30 | ; that their coordinates are longitude-latitude coordinates. In this |
---|
[136] | 31 | ; case, est-west periodicity, poles singularity and other pbs |
---|
| 32 | ; related to longitude-latitude coordinates are managed |
---|
| 33 | ; automatically. |
---|
[59] | 34 | ; |
---|
[121] | 35 | ; @keyword ZOOMRADIUS {default=4} |
---|
[163] | 36 | ; the zoom (circle centered on the (x,y) with a radius of |
---|
[136] | 37 | ; zoomradius degree where we look for the the quadrilateral which |
---|
| 38 | ; contains the (x,y) point) used for the satellite projection |
---|
| 39 | ; when /ONSPHERE is activated. |
---|
| 40 | ; 4 seems to be the minimum which can be used. |
---|
| 41 | ; Can be increase if the cell size is larger than 5 degrees. |
---|
[59] | 42 | ; |
---|
[136] | 43 | ; @keyword NOPRINT |
---|
| 44 | ; to suppress the print messages. |
---|
[125] | 45 | ; |
---|
[118] | 46 | ; @keyword NEWCOORD |
---|
| 47 | ; |
---|
[101] | 48 | ; @returns |
---|
[136] | 49 | ; a n element vector. Where n is the number of elements of |
---|
| 50 | ; x. res[i]=j means that the point number i is located in the |
---|
| 51 | ; quadrilateral number j with (0 <= j <= n_elements(x0)-1) |
---|
[59] | 52 | ; |
---|
[125] | 53 | ; @restrictions |
---|
| 54 | ; I think degenerated quadrilateral (e.g. flat of twisted) is not work. |
---|
| 55 | ; This has to be tested. |
---|
[59] | 56 | ; |
---|
[125] | 57 | ; @examples |
---|
[59] | 58 | ; |
---|
[118] | 59 | ; IDL> x = 1.*[1, 2, 6, 7, 3] |
---|
| 60 | ; IDL> y = 1.*[1, 3, 3, 4, 7] |
---|
| 61 | ; IDL> x1 = 1.*[0,4,2] |
---|
| 62 | ; IDL> y1 = 1.*[1,4,8] |
---|
| 63 | ; IDL> x2 = 1.*[1,6,4] |
---|
| 64 | ; IDL> y2 = 1.*[5,6,8] |
---|
| 65 | ; IDL> x3 = 1.*[3,8,4] |
---|
| 66 | ; IDL> y3 = 1.*[4,4,6] |
---|
| 67 | ; IDL> x4 = 1.*[2,6,2] |
---|
| 68 | ; IDL> y4 = 1.*[0,2,6] |
---|
| 69 | ; IDL> splot, [0,10], [0,10], xstyle = 1, ystyle = 1,/nodata |
---|
| 70 | ; IDL> for i=0,2 do oplot, [x4[i],x1[i],x2[i],x3[i],x4[i]],[y4[i],y1[i],y2[i],y3[i],y4[i]] |
---|
| 71 | ; IDL> oplot, x, y, color = 20, psym = 1, thick = 2 |
---|
| 72 | ; IDL> print, inquad(x, y, x1, y1, x2, y2, x3, y3, x4, y4) |
---|
[59] | 73 | ; |
---|
[231] | 74 | ; On a sphere see |
---|
[232] | 75 | ; <pro>clickincell</pro> ... |
---|
[59] | 76 | ; |
---|
[101] | 77 | ; @history |
---|
| 78 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[59] | 79 | ; August 2003 |
---|
[125] | 80 | ; Based on Convert_clic_ij.pro written by Gurvan Madec |
---|
[59] | 81 | ; |
---|
[231] | 82 | ; @version |
---|
| 83 | ; $Id$ |
---|
[118] | 84 | ; |
---|
[59] | 85 | ;- |
---|
[231] | 86 | ; |
---|
[59] | 87 | FUNCTION inquad, x, y, x1, y1, x2, y2, x3, y3, x4, y4, ONSPHERE = onsphere, DOUBLE = double, ZOOMRADIUS = zoomradius, NOPRINT = noprint, NEWCOORD = newcoord |
---|
| 88 | ; |
---|
[114] | 89 | compile_opt idl2, strictarrsubs |
---|
[59] | 90 | ; |
---|
| 91 | ntofind = n_elements(x) |
---|
| 92 | nquad = n_elements(x2) |
---|
| 93 | ; |
---|
| 94 | IF keyword_set(onsphere) THEN BEGIN |
---|
| 95 | ; save the inputs parameters |
---|
| 96 | xin = x |
---|
| 97 | yin = y |
---|
| 98 | x1in = x1 |
---|
| 99 | y1in = y1 |
---|
| 100 | x2in = x2 |
---|
| 101 | y2in = y2 |
---|
| 102 | x3in = x3 |
---|
| 103 | y3in = y3 |
---|
| 104 | x4in = x4 |
---|
| 105 | y4in = y4 |
---|
| 106 | ; for map_set |
---|
| 107 | x = x MOD 360 |
---|
| 108 | x1 = x1 MOD 360 |
---|
| 109 | x2 = x2 MOD 360 |
---|
| 110 | x3 = x3 MOD 360 |
---|
| 111 | x4 = x4 MOD 360 |
---|
[125] | 112 | ; save !map |
---|
[59] | 113 | save = {map:!map, x:!x, y:!y, z:!z, p:!p} |
---|
[125] | 114 | ; do a satellite projection |
---|
[59] | 115 | IF NOT keyword_set(zoomradius) THEN zoomradius = 4 |
---|
| 116 | map_set, y[0], x[0], 0, /satellite, sat_p = [1+zoomradius*20/6371.229, 0, 0], /noerase, /iso, /noborder |
---|
| 117 | ; use normal coordinates to reject cells which are out of the projection. |
---|
[125] | 118 | tmp = convert_coord(x, y, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 119 | tmp1 = convert_coord(x1, y1, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 120 | tmp2 = convert_coord(x2, y2, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 121 | tmp3 = convert_coord(x3, y3, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 122 | tmp4 = convert_coord(x4, y4, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
[59] | 123 | ; remove cell which have one corner with coordinates equal to NaN |
---|
| 124 | test = finite(tmp1[0, *]+tmp1[1, *]+tmp2[0, *]+tmp2[1, *] $ |
---|
| 125 | +tmp3[0, *]+tmp3[1, *]+tmp4[0, *]+tmp4[1, *]) |
---|
| 126 | good = where(temporary(test) EQ 1) |
---|
| 127 | ; |
---|
| 128 | IF good[0] EQ -1 THEN BEGIN |
---|
| 129 | IF NOT keyword_set(noprint) THEN print, 'The point is out of the cells' |
---|
| 130 | ; restore the input parameters |
---|
| 131 | x = temporary(xin) |
---|
| 132 | y = temporary(yin) |
---|
| 133 | x1 = temporary(x1in) |
---|
| 134 | y1 = temporary(y1in) |
---|
| 135 | x2 = temporary(x2in) |
---|
| 136 | y2 = temporary(y2in) |
---|
| 137 | x3 = temporary(x3in) |
---|
| 138 | y3 = temporary(y3in) |
---|
| 139 | x4 = temporary(x4in) |
---|
| 140 | y4 = temporary(y4in) |
---|
| 141 | ; restore old !map... |
---|
| 142 | !map = save.map |
---|
| 143 | !x = save.x |
---|
| 144 | !y = save.y |
---|
| 145 | !z = save.z |
---|
| 146 | !p = save.p |
---|
| 147 | RETURN, -1 |
---|
| 148 | ENDIF |
---|
| 149 | ; |
---|
| 150 | x = tmp[0] |
---|
| 151 | y = tmp[1] |
---|
| 152 | x1 = tmp1[0, good] |
---|
| 153 | y1 = tmp1[1, good] |
---|
| 154 | x2 = tmp2[0, good] |
---|
| 155 | y2 = tmp2[1, good] |
---|
| 156 | x3 = tmp3[0, good] |
---|
| 157 | y3 = tmp3[1, good] |
---|
| 158 | x4 = tmp4[0, good] |
---|
| 159 | y4 = tmp4[1, good] |
---|
| 160 | ; |
---|
| 161 | tmp1 = -1 & tmp2 = -1 & tmp3 = -1 & tmp4 = -1 |
---|
[125] | 162 | ; remove cells which are obviously bad |
---|
[59] | 163 | test = (x1 GT x AND x2 GT x AND x3 GT x AND x4 GT x) $ |
---|
| 164 | OR (x1 LT x AND x2 LT x AND x3 LT x AND x4 LT x) $ |
---|
| 165 | OR (y1 GT y AND y2 GT y AND y3 GT y AND y4 GT y) $ |
---|
| 166 | OR (y1 LT y AND y2 LT y AND y3 LT y AND y4 LT y) |
---|
| 167 | good2 = where(temporary(test) EQ 0) |
---|
| 168 | ; |
---|
| 169 | IF good2[0] EQ -1 THEN BEGIN |
---|
| 170 | IF NOT keyword_set(noprint) THEN print, 'The point is out of the cells' |
---|
| 171 | ; restore the input parameters |
---|
| 172 | x = temporary(xin) |
---|
| 173 | y = temporary(yin) |
---|
| 174 | x1 = temporary(x1in) |
---|
| 175 | y1 = temporary(y1in) |
---|
| 176 | x2 = temporary(x2in) |
---|
| 177 | y2 = temporary(y2in) |
---|
| 178 | x3 = temporary(x3in) |
---|
| 179 | y3 = temporary(y3in) |
---|
| 180 | x4 = temporary(x4in) |
---|
| 181 | y4 = temporary(y4in) |
---|
| 182 | ; restore old !map... |
---|
| 183 | !map = save.map |
---|
| 184 | !x = save.x |
---|
| 185 | !y = save.y |
---|
| 186 | !z = save.z |
---|
| 187 | !p = save.p |
---|
| 188 | RETURN, -1 |
---|
| 189 | ENDIF |
---|
| 190 | ; |
---|
[125] | 191 | nquad = n_elements(good2) |
---|
[59] | 192 | x1 = x1[good2] |
---|
| 193 | y1 = y1[good2] |
---|
| 194 | x2 = x2[good2] |
---|
| 195 | y2 = y2[good2] |
---|
| 196 | x3 = x3[good2] |
---|
| 197 | y3 = y3[good2] |
---|
| 198 | x4 = x4[good2] |
---|
| 199 | y4 = y4[good2] |
---|
| 200 | ENDIF |
---|
| 201 | ; |
---|
| 202 | ; |
---|
[163] | 203 | ; the point is inside the quadrilateral if test eq 1 |
---|
[59] | 204 | ; with test equal to: |
---|
[101] | 205 | ; test = ((x-x1)*(y2-y1) GE (x2-x1)*(y-y1)) $ |
---|
[59] | 206 | ; *((x-x2)*(y3-y2) GT (x3-x2)*(y-y2)) $ |
---|
| 207 | ; *((x-x3)*(y4-y3) GT (x4-x3)*(y-y3)) $ |
---|
[125] | 208 | ; *((x-x4)*(y1-y4) GE (x1-x4)*(y-y4)) |
---|
[59] | 209 | ; |
---|
| 210 | ; computation of test without any do loop for ntofind points (x,y) and |
---|
[226] | 211 | ; nquad quadrilateral((x1,x2,x3,x4),(y1,y2,y3,y4)) |
---|
[59] | 212 | ; test dimensions are (ntofind, nquad) |
---|
| 213 | ; column i of test corresponds to the intersection of point i with all |
---|
[226] | 214 | ; quadrilateral. |
---|
[125] | 215 | ; row j of test corresponds to all the points localized in cell j |
---|
[59] | 216 | test = $ |
---|
| 217 | ; (x-x1) |
---|
| 218 | ((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x1[*]) $ |
---|
| 219 | ; *(y2-y1) |
---|
| 220 | *(replicate(1, ntofind)#(y2-y1)[*]) $ |
---|
| 221 | ; GE (x2-x1) |
---|
| 222 | GE ((replicate(1, ntofind)#(x2-x1)[*]) $ |
---|
| 223 | ; *(y-y1) |
---|
| 224 | *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y1[*]))) |
---|
| 225 | ;------- |
---|
| 226 | test = temporary(test) $ |
---|
| 227 | ; *(x-x2) |
---|
| 228 | *((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x2[*]) $ |
---|
| 229 | ; *(y3-y2) |
---|
| 230 | *(replicate(1, ntofind)#(y3-y2)[*]) $ |
---|
| 231 | ; GE (x3-x2) |
---|
| 232 | GE ((replicate(1, ntofind)#(x3-x2)[*]) $ |
---|
| 233 | ; *(y-y2) |
---|
| 234 | *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y2[*]))) |
---|
| 235 | ;------- |
---|
| 236 | test = temporary(test) $ |
---|
| 237 | ; *(x-x3) |
---|
| 238 | *((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x3[*]) $ |
---|
| 239 | ; *(y4-y3) |
---|
| 240 | *(replicate(1, ntofind)#(y4-y3)[*]) $ |
---|
| 241 | ; GE (x4-x3) |
---|
| 242 | GE ((replicate(1, ntofind)#(x4-x3)[*]) $ |
---|
| 243 | ; *(y-y3) |
---|
| 244 | *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y3[*]))) |
---|
| 245 | ;------- |
---|
| 246 | test = temporary(test) $ |
---|
| 247 | ; *(x-x4) |
---|
| 248 | *((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x4[*]) $ |
---|
| 249 | ; *(y1-y4) |
---|
| 250 | *(replicate(1, ntofind)#(y1-y4)[*]) $ |
---|
| 251 | ; GE (x1-x4) |
---|
| 252 | GE ((replicate(1, ntofind)#(x1-x4)[*]) $ |
---|
| 253 | ; *(y-y4) |
---|
| 254 | *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y4[*]))) |
---|
| 255 | ; |
---|
| 256 | ; check test if ntofind gt 1 |
---|
| 257 | ; if ntofind gt 1, each point must be localised in one uniq cell. |
---|
| 258 | IF ntofind GT 1 THEN BEGIN |
---|
| 259 | ; each column of test must have only 1 position equal to one |
---|
| 260 | chtest = total(test, 2) |
---|
| 261 | ; points out of the cells |
---|
| 262 | IF (where(chtest EQ 0))[0] NE -1 THEN BEGIN |
---|
[236] | 263 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 264 | ras = report('Points number '+strjoin(strtrim(where(chtest EQ 0), 1), ', ')+' are out of the grid') |
---|
| 265 | ENDIF |
---|
[59] | 266 | stop |
---|
| 267 | ENDIF |
---|
| 268 | ; points in more than one cell |
---|
| 269 | IF (where(chtest GT 1))[0] NE -1 THEN BEGIN |
---|
[236] | 270 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 271 | ras = report('Points number '+strjoin(strtrim(where(chtest GT 1), 1), ', ')+' are in more than one cell') |
---|
| 272 | ENDIF |
---|
[59] | 273 | stop |
---|
| 274 | ENDIF |
---|
| 275 | ENDIF |
---|
| 276 | ; find the points for which test eq 1 |
---|
| 277 | found = where(temporary(test) EQ 1) |
---|
| 278 | ; if ntofind eq 1, the point may be localised in more than one grid |
---|
| 279 | ; cell ou may also be out of the cells |
---|
| 280 | IF ntofind EQ 1 THEN BEGIN |
---|
| 281 | CASE 1 OF |
---|
| 282 | found[0] EQ -1:BEGIN |
---|
[236] | 283 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 284 | ras = report('The point is out of the cells') |
---|
| 285 | ENDIF |
---|
[59] | 286 | IF keyword_set(onsphere) THEN BEGIN |
---|
| 287 | ; restore old !map... |
---|
| 288 | !map = save.map |
---|
| 289 | !x = save.x |
---|
| 290 | !y = save.y |
---|
| 291 | !z = save.z |
---|
| 292 | !p = save.p |
---|
| 293 | ENDIF |
---|
| 294 | return, -1 |
---|
[125] | 295 | END |
---|
[59] | 296 | n_elements(found) GT ntofind:BEGIN |
---|
[236] | 297 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 298 | ras = report('The point is in more than one cell') |
---|
| 299 | ENDIF |
---|
[125] | 300 | END |
---|
[59] | 301 | ELSE: |
---|
| 302 | ENDCASE |
---|
| 303 | ENDIF ELSE BEGIN |
---|
[125] | 304 | ; if ntofind GT 1, found must be sorted |
---|
[59] | 305 | ; i position of found. this corresponds to one x,y point |
---|
| 306 | forsort = found MOD ntofind |
---|
| 307 | ; j position of found. this corresponds to cell in which is one x,y |
---|
| 308 | ; point |
---|
| 309 | found = temporary(found)/ntofind |
---|
[136] | 310 | ; found must be sorted according to forsort |
---|
[59] | 311 | found = found[sort(forsort)] |
---|
| 312 | ENDELSE |
---|
| 313 | ; |
---|
| 314 | IF keyword_set(onsphere) THEN BEGIN |
---|
| 315 | IF arg_present(newcoord) THEN BEGIN |
---|
[110] | 316 | found = found[0] |
---|
[59] | 317 | newcoord = [[x1[found], y1[found]] $ |
---|
| 318 | , [x2[found], y2[found]] $ |
---|
| 319 | , [x3[found], y3[found]] $ |
---|
| 320 | , [x4[found], y4[found]] $ |
---|
| 321 | , [x, y]] |
---|
| 322 | ENDIF |
---|
| 323 | ; |
---|
| 324 | found = good[good2[found]] |
---|
| 325 | ; restore the input parameters |
---|
| 326 | x = temporary(xin) |
---|
| 327 | y = temporary(yin) |
---|
| 328 | x1 = temporary(x1in) |
---|
| 329 | y1 = temporary(y1in) |
---|
| 330 | x2 = temporary(x2in) |
---|
| 331 | y2 = temporary(y2in) |
---|
| 332 | x3 = temporary(x3in) |
---|
| 333 | y3 = temporary(y3in) |
---|
| 334 | x4 = temporary(x4in) |
---|
| 335 | y4 = temporary(y4in) |
---|
| 336 | ; restore old !map... |
---|
| 337 | !map = save.map |
---|
| 338 | !x = save.x |
---|
| 339 | !y = save.y |
---|
| 340 | !z = save.z |
---|
| 341 | !p = save.p |
---|
| 342 | ENDIF |
---|
| 343 | ;; |
---|
| 344 | RETURN, found |
---|
| 345 | END |
---|