… | |
… | |
31 | |
31 | |
32 | static inline int |
32 | static inline int |
33 | intmax (int a, int b) |
33 | intmax (int a, int b) |
34 | { |
34 | { |
35 | return a > b ? a : b; |
35 | return a > b ? a : b; |
|
|
36 | } |
|
|
37 | |
|
|
38 | static inline int |
|
|
39 | get_u16 (const U8 *ptr) |
|
|
40 | { |
|
|
41 | return ptr[0] | (ptr[1] << 8); |
36 | } |
42 | } |
37 | |
43 | |
38 | MODULE = Geo::LatLon2Place PACKAGE = Geo::LatLon2Place |
44 | MODULE = Geo::LatLon2Place PACKAGE = Geo::LatLon2Place |
39 | |
45 | |
40 | PROTOTYPES: ENABLE |
46 | PROTOTYPES: ENABLE |
… | |
… | |
75 | { |
81 | { |
76 | int rx = x; |
82 | int rx = x; |
77 | rx += rx < 0 ? blat : 0; |
83 | rx += rx < 0 ? blat : 0; |
78 | rx -= rx >= blat ? blat : 0; |
84 | rx -= rx >= blat ? blat : 0; |
79 | |
85 | |
80 | unsigned char key[4] = { |
86 | unsigned char key[4]; |
|
|
87 | key[0] = rx; |
81 | rx, rx >> 8, |
88 | key[1] = rx >> 8; |
|
|
89 | key[2] = y; |
82 | y, y >> 8, |
90 | key[3] = y >> 8; |
83 | }; |
|
|
84 | |
91 | |
85 | printf ("x,y %4d,%4d blat %d %d %g %02x%02x%02x%02x %d\n", rx, y, blat, (int)glat, TORAD(glat), key[0],key[1],key[2],key[3], sizeof(key)); |
92 | //printf ("x,y %4d,%4d blat %d %d %g %02x%02x%02x%02x %d\n", rx, y, blat, (int)glat, TORAD(glat), key[0],key[1],key[2],key[3], sizeof(key)); |
86 | |
93 | |
87 | if (cdb_find (db, key, sizeof (key)) <= 0) |
94 | if (cdb_find (db, key, sizeof (key)) <= 0) |
88 | continue; |
95 | continue; |
89 | |
96 | |
90 | int len = cdb_datalen (db); |
97 | int len = cdb_datalen (db); |
91 | const U8 *ptr = cdb_get (db, len, cdb_datapos (db)); |
98 | const U8 *ptr = (const U8 *)cdb_get (db, len, cdb_datapos (db)); |
92 | |
99 | |
93 | while (len > 0) |
100 | while (len > 0) |
94 | { |
101 | { |
95 | int datalen = ptr[5]; |
102 | int datalen = ptr[5]; |
96 | |
103 | |
97 | double plat = ptr[0] | (ptr[1] << 8) * ( 90. / 32767.); |
104 | double plat = get_u16 (ptr + 0) * ( 90. / 32767.); |
98 | double plon = ptr[2] | (ptr[3] << 8) * (180. / 32767.); |
105 | double plon = get_u16 (ptr + 2) * (180. / 32767.); |
99 | int w = ptr[4]; |
106 | int w = ptr[4]; |
100 | |
107 | |
101 | double dx = TORAD (lon - plon) * coslat; |
108 | double dx = TORAD (lon - plon) * coslat; |
102 | double dy = TORAD (lat - plat); |
109 | double dy = TORAD (lat - plat); |
103 | double d2 = (dx * dx + dy * dy) * w; |
110 | double d2 = (dx * dx + dy * dy) * w; |
104 | printf ("%g,%g %g %.*s\n", plat,plon,d2, datalen,ptr+6); |
111 | //printf ("%g,%g %g %.*s\n", plat,plon,d2, datalen,ptr+6); |
105 | |
112 | |
106 | if (d2 < mind) |
113 | if (d2 < mind) |
107 | { |
114 | { |
108 | mind = d2; |
115 | mind = d2; |
109 | resptr = ptr; |
116 | resptr = ptr; |
… | |
… | |
117 | } |
124 | } |
118 | |
125 | |
119 | if (!reslen) |
126 | if (!reslen) |
120 | XSRETURN_EMPTY; |
127 | XSRETURN_EMPTY; |
121 | |
128 | |
122 | RETVAL = newSVpvn (resptr + 6, reslen); |
129 | RETVAL = newSVpvn ((const char *)resptr + 6, reslen); |
123 | } |
130 | } |
124 | OUTPUT: RETVAL |
131 | OUTPUT: RETVAL |
125 | |
132 | |
126 | ############################################################################# |
133 | ############################################################################# |
127 | |
134 | |
128 | int |
135 | int |
129 | cdb_init (SV *self, int fd) |
136 | cdb_init (SV *self, int fd) |
130 | CODE: |
137 | CODE: |
131 | sv_upgrade (self, SVt_PV); |
138 | sv_upgrade (self, SVt_PV); |
|
|
139 | sv_grow (self, sizeof (struct cdb)); |
132 | SvCUR_set (self, sizeof (struct cdb)); |
140 | SvCUR_set (self, sizeof (struct cdb)); |
133 | SvPOK_only (self); |
141 | SvPOK_only (self); |
134 | RETVAL = cdb_init ((struct cdb *)SvPVX (self), fd); |
142 | RETVAL = cdb_init ((struct cdb *)SvPVX (self), fd); |
135 | OUTPUT: RETVAL |
143 | OUTPUT: RETVAL |
136 | |
144 | |
… | |
… | |
151 | if (cdb_find (db, s, l) <= 0) |
159 | if (cdb_find (db, s, l) <= 0) |
152 | XSRETURN_UNDEF; |
160 | XSRETURN_UNDEF; |
153 | |
161 | |
154 | p = cdb_datapos (db); |
162 | p = cdb_datapos (db); |
155 | l = cdb_datalen (db); |
163 | l = cdb_datalen (db); |
156 | RETVAL = newSVpvn (cdb_get (db, l, p), l); |
164 | RETVAL = newSVpvn ((const char *)cdb_get (db, l, p), l); |
157 | } |
165 | } |
158 | OUTPUT: RETVAL |
166 | OUTPUT: RETVAL |
159 | |
167 | |
160 | ############################################################################# |
168 | ############################################################################# |
161 | |
169 | |