Changeset 33125 for main/trunk/greenstone2/perllib/doc.pm
- Timestamp:
- 2019-05-31T20:02:40+12:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/doc.pm
r32590 r33125 42 42 ##use hashdoc; 43 43 use docprint; 44 use JSON; 45 46 # We just need pi from the Trig lib 47 # Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4). 48 use Math::Trig ':pi'; 44 49 45 50 # the document type may be indexed_doc, nonindexed_doc, or … … 1129 1134 1130 1135 #Only add the metadata if it has not already been added 1131 my $metaMap = $self->get_metadata_hashmap($section); 1136 my $metaMap = $self->get_metadata_hashmap($section); ### TODO: metaMap not used. Unnecesssary step? (Called function has no side-effects.) 1132 1137 } 1133 1138 } 1134 1139 1140 elsif($field eq "GPS.mapOverlay") { # then the value is a JSON string 1141 print STDERR "GPS.mapOverlay before val: " . $value . "\n"; 1142 1143 # TODO html decode? 1144 $value =~ s@[@[@g; 1145 $value =~ s@]@]@g; 1146 $value =~ s@"@"@g; 1147 print STDERR "GPS.mapOverlay after val: " . $value . "\n"; 1148 1149 my $json_array = decode_json $value; 1150 #my $json = JSON->new->allow_nonref; 1151 #&printAllShapes($json, $json_array); 1152 1153 foreach my $shape (@$json_array) { 1154 1155 my $type = $shape->{"type"}; 1156 print STDERR "Type : " . $type . "\n"; 1157 1158 if($type eq "circle") { 1159 #print STDERR "Found a circle:\n" . &printShape($json, $shape); 1160 1161 # work out bounding box 1162 # SCARY! 1163 # want the inverse of this useful page: 1164 # https://stackoverflow.com/questions/639695/how-to-convert-latitude-or-longitude-to-meters 1165 # https://www.geodatasource.com/developers/javascript 1166 1167 1168 # for now, just process the circle centre 1169 #my $centre = $shape->{"center"}; 1170 #$self->processLatOrLng($section_ptr, "Latitude", $centre->{"lat"}); 1171 #$self->processLatOrLng($section_ptr, "Longitude", $centre->{"lng"}); 1172 1173 1174 # Dr Bainbridge wants us to use: https://gis.stackexchange.com/questions/5821/calculating-latitude-longitude-x-miles-from-point 1175 # But we're using the rule of thumb here, since for N,E,S,W it works out the same: 1176 # https://gis.stackexchange.com/questions/2951/algorithm-for-offsetting-a-latitude-longitude-by-some-amount-of-meters 1177 # which states 1178 # "If your displacements aren't too great (less than a few kilometers) and you're not right at the poles, 1179 # use the quick and dirty estimate that 111,111 meters (111.111 km) in the y direction is 1 degree 1180 # (of latitude) and 111,111 * cos(latitude) meters in the x direction is 1 degree (of longitude)." 1181 my $centre_lat = $shape->{"center"}->{"lat"}; 1182 my $centre_lng = $shape->{"center"}->{"lng"}; 1183 my $radius = $shape->{"radius"}; # in metres! 1184 1185 print STDERR "@@@ circle centre: ($centre_lat, $centre_lng), radius: $radius\n"; 1186 1187 my $lat_north = $centre_lat + ($radius/111111); 1188 my $lat_south = $centre_lat - ($radius/111111); 1189 1190 print STDERR "### lat_north: $lat_north\n"; 1191 print STDERR "### lat_south: $lat_south\n"; 1192 1193 # our latitude and longitude values are in degrees. But cos and sin etc in perl and generally all prog languages 1194 # all expect their angle to be in radians. So need to convert from degree to radians before we can take the cose of it. 1195 my $centre_lat_radians = $self->degreesToRadians($centre_lat); 1196 my $cos_in_radians = cos($centre_lat_radians); 1197 print STDERR "cos $centre_lat_radians " . cos($centre_lat_radians) . "\n"; 1198 my $lng_east = $centre_lng + ($radius/(111111 * $cos_in_radians)); 1199 my $lng_west = $centre_lng - ($radius/(111111 * $cos_in_radians)); 1200 print STDERR "### lng_east $lng_east\n"; 1201 print STDERR "### lng_west $lng_west\n"; 1202 1203 1204 1205 my $cos_lat = cos($centre_lat); 1206 print STDERR "cos $centre_lat is $cos_lat\n"; 1207 1208 $self->processCoordinate($section_ptr, $lat_north, $lng_east); 1209 $self->processCoordinate($section_ptr, $lat_south, $lng_east); 1210 $self->processCoordinate($section_ptr, $lat_south, $lng_west); 1211 $self->processCoordinate($section_ptr, $lat_north, $lng_west); 1212 1213 } 1214 elsif ($type eq "marker") { 1215 print STDERR "@@ MARKER FOUND WITH LAT: " . $shape->{"position"}->{"lat"} . "\n"; 1216 print STDERR "@@ MARKER FOUND WITH LNG: " . $shape->{"position"}->{"lng"} . "\n"; 1217 $self->processCoordinate($section_ptr, $shape->{"position"}->{"lat"}, $shape->{"position"}->{"lng"}); 1218 } 1219 elsif ($type eq "polyline" || $type eq "polygon") { 1220 my $path_array = $shape->{"path"}; 1221 foreach my $position (@$path_array) { 1222 $self->processCoordinate($section_ptr, $position->{"lat"}, $position->{"lng"}); 1223 } 1224 } 1225 elsif ($type eq "rectangle") { 1226 1227 my $bounds = $shape->{"bounds"}; 1228 1229 $self->processCoordinate($section_ptr, $bounds->{"north"}, $bounds->{"east"}); 1230 $self->processCoordinate($section_ptr, $bounds->{"south"}, $bounds->{"east"}); 1231 $self->processCoordinate($section_ptr, $bounds->{"south"}, $bounds->{"west"}); 1232 $self->processCoordinate($section_ptr, $bounds->{"north"}, $bounds->{"west"}); 1233 } 1234 1235 } # end for on each shape in GPS.mapOverlay 1236 1237 } # end GPS.mapOverlay meta 1238 1135 1239 push (@{$section_ptr->{'metadata'}}, [$field, $value]); 1240 } 1241 1242 # https://en.wikipedia.org/wiki/Radian 1243 sub degreesToRadians 1244 { 1245 my $self = shift (@_); 1246 my ($degrees) = @_; 1247 1248 return $degrees * pi /180; # returns radians 1249 } 1250 1251 sub radiansToDegrees 1252 { 1253 my $self = shift (@_); 1254 my ($radians) = @_; 1255 1256 return $radians * 180 / pi; # returns degrees 1257 } 1258 1259 sub printAllShapes { 1260 my ($json, $json_array) = @_; 1261 1262 1263 #my $pretty_print_shape = $json->pretty->encode( $json_array->[0] ); 1264 foreach my $shape (@$json_array) { 1265 my $pretty_print_shape = $json->pretty->encode( $shape ); 1266 print STDERR "Shape: $pretty_print_shape\n"; 1267 #&printShape($shape); 1268 } 1269 1270 } 1271 1272 sub processCoordinate { 1273 my $self = shift (@_); 1274 my ($section_ptr, $latitude, $longitude) = @_; 1275 1276 my $lat_direction = ($latitude =~ m/^-/) ? "S" : "N"; 1277 my $lng_direction = ($longitude =~ m/^-/) ? "W" : "E"; 1278 1279 # have to store (lat, lng) in pairs, when there are so many coords to store 1280 #push (@{$section_ptr->{'metadata'}}, ["Latitude", $latitude]); 1281 #push (@{$section_ptr->{'metadata'}}, ["Longitude", $longitude]); 1282 1283 push (@{$section_ptr->{'metadata'}}, ["Coordinate", "$latitude $longitude"]); # "$latitude$lat_direction $longitude$lng_direction" 1284 1285 my ($latBeforeDec, $latAfterDec); 1286 my ($lngBeforeDec, $lngAfterDec); 1287 1288 if($latitude !~ m/\./) { 1289 $latBeforeDec = $latitude; 1290 $latAfterDec = ""; 1291 } else { 1292 ($latBeforeDec, $latAfterDec) = ($latitude =~ m/^-?([0-9]+)\.([0-9]+)$/); 1293 } 1294 if($longitude !~ m/\./) { 1295 $lngBeforeDec = $longitude; 1296 $lngAfterDec = ""; 1297 } else { 1298 ($lngBeforeDec, $lngAfterDec) = ($longitude =~ m/^-?([0-9]+)\.([0-9]+)$/); 1299 } 1300 1301 #if(defined $beforeDec && defined $afterDec) 1302 #{ 1303 my $name = "CoordShort"; 1304 push (@{$section_ptr->{'metadata'}}, [$name, "$latBeforeDec$lat_direction $lngBeforeDec$lng_direction"]); 1305 1306 for(my $i = 2; $i <= 4; $i++) 1307 { 1308 my $latDecPlaces = (length($latAfterDec) >= $i) ? substr($latAfterDec, 0, $i) : ""; 1309 my $lngDecPlaces = (length($lngAfterDec) >= $i) ? substr($lngAfterDec, 0, $i) : ""; 1310 1311 push (@{$section_ptr->{'metadata'}}, [$name, 1312 $latBeforeDec . $lat_direction. $latDecPlaces . " " . $lngBeforeDec . $lng_direction. $lngDecPlaces]); 1313 1314 } 1315 1316 #Only add the metadata if it has not already been added 1317 #my $metaMap = $self->get_metadata_hashmap($section); ### TODO: metaMap not used. Unnecesssary step? (Called function has no side-effects.) 1318 #} 1319 1320 1136 1321 } 1137 1322
Note:
See TracChangeset
for help on using the changeset viewer.