Page 1 of 1
Display number of images with GPS
Posted: Thu Apr 24, 2025 7:58 am
by Mates-K1
Hi Stephen,
Could you please suggest me a code to display the number of images with GPS coordinates from the total number of images in the gallery?
Either so I can put it somewhere in the theme, or maybe this information could appear on the Gallery Stats page.
And maybe if I go a little further with the idea, maybe this information would be interesting for each album in the administration (e.g. in the right column).
I have already abandoned the idea of displaying a map with all images with GPS, because I think it would be very heavy on the server.
Re: Display number of images with GPS
Posted: Thu Apr 24, 2025 4:25 pm
by stephenbillard
The following loop will find images in the album that have gps data:
Code: Select all
$count = 0;
$images = $album->getImages(0, 0, null, null, false);
$total_images = count($images);
foreach ($images as $an_image) {
$image = newImage($album, $an_image);
$lat = (string) $image->getGPSLatitude();
$long = (string) $image->getGPSLongitude();
if (!(empty($lat) || empty($long))) {
/* this image has geo data, so count it or whatever */
$count ++;
}
}
So far as performance is concerned I do not think this will add much to the processing. netPhotoGraphics caches the list of images and any image object that is instantiated. Since if you are showing a map of the album with image points the image list and all the images will have already been cached so it is just the above loop that will be added to the processing overhead.
Re: Display number of images with GPS
Posted: Thu Apr 24, 2025 5:13 pm
by stephenbillard
You could also use an SQL query to get the count:
Code: Select all
$sql = 'SELECT count(*) FROM ' . prefix('images') . ' WHERE `albumid`=' . $album->id . ' and `gpslatitude` is not null and `gpslongitude` is not null';
$row = query_single_row($sql);
$count = reset($row);
Re: Display number of images with GPS
Posted: Sun Apr 27, 2025 7:18 pm
by Mates-K1
Well, I get error, when I have this code:
Code: Select all
<footer id="footer" class="footer">
<div class="container">
<div id="copyright">
<?php
echo getMainSiteName();
if (getOption('zpB_show_archive')) {
printCustomPageURL(gettext('Archive View'), 'archive', '', ' | ');
}
if (extensionEnabled('daily-summary')) {
printDailySummaryLink(gettext('Daily summary'), '', ' | ', '');
}
?>
</div>
<div>
<?php print_SW_Link(); ?> & <a href="https://getbootstrap.com/docs/3.4/" target="_blank" title="Bootstrap">Bootstrap</a>
<?php
$count = 0;
$images = $album->getImages(0, 0, null, null, false);
$total_images = count($images);
foreach ($images as $an_image) {
$image = newImage($album, $an_image);
$lat = (string) $image->getGPSLatitude();
$long = (string) $image->getGPSLongitude();
if (!(empty($lat) || empty($long))) {
// Tato fotografie má GPS data
$count++;
}
}
?>
</div>
</div>
</footer>
</body>
<?php npgFilters::apply('theme_body_close'); ?>
</html>
<!-- Bootstrap 2.3 - a theme by Vincent3569 -->
Error

- Screenshot 2025-04-27 at 21-17-24 Skautská galerie - fotografie a videa střediska Žamberk.png (6.42 KiB) Viewed 6452 times
:
Re: Display number of images with GPS
Posted: Sun Apr 27, 2025 9:37 pm
by stephenbillard
The $album variable must be set to the object for the album you want to count. If this is for a normal album page then you can use the global variable $_current_album.
Re: Display number of images with GPS
Posted: Thu May 08, 2025 6:37 pm
by Mates-K1
In the SQL query variant that I managed to get working, it was necessary to modify
to
. Then the number of images with GPS in that album will be displayed.
Is there any way to find out the number of images with GPS for the whole gallery at once?
Re: Display number of images with GPS
Posted: Thu May 08, 2025 6:46 pm
by stephenbillard
Just remove the albumid part of the WHERE clause and it will search the whole image table.
Code: Select all
$sql = 'SELECT count(*) FROM ' . prefix('images') . ' WHERE `gpslatitude` is not null and `gpslongitude` is not null';
$row = query_single_row($sql);
$count = reset($row);
Re: Display number of images with GPS
Posted: Thu May 08, 2025 6:58 pm
by Mates-K1
Thx. You are pretty fast.
I tried it this way:
Code: Select all
<?php
// Get the count of all images with GPS coordinates
$sql = 'SELECT count(*) FROM ' . prefix('images') . ' WHERE `gpslatitude` IS NOT NULL AND `gpslongitude` IS NOT NULL';
$row = query_single_row($sql);
$gpsCount = reset($row);
// Get the total count of all images in the gallery
$sql = 'SELECT count(*) FROM ' . prefix('images');
$row = query_single_row($sql);
$totalCount = reset($row);
// Optionally: calculate the percentage of images with GPS
$percent = round(($gpsCount / max($totalCount, 1)) * 100);
// Display the information in the desired format
echo "The gallery contains $gpsCount images with GPS coordinates (i.e. $percent%) out of a total of $totalCount images.";
?>
Would it please be possible to insert this line into, for example, Gallery Statistics? Or expand the current view from "21396 images, 887 albums" to "21396 images (18356 with GPS), 887 albums"?