Display number of images with GPS

A forum for discussing possible new features or changes to netPhotoGraphics. But NOTE, only a formal request via the GitHub repository ticket system is tracked or responded to.
Post Reply
Mates-K1
Posts: 37
Joined: Tue Aug 28, 2018 9:43 pm
Location: Ústí nad Orlicí, Czech Republic

Display number of images with GPS

Post 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.
stephenbillard
Site Admin
Posts: 96
Joined: Tue Aug 14, 2018 6:33 pm
Location: Huntington Beach, CA, USA

Re: Display number of images with GPS

Post 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.
-Stephen
stephenbillard
Site Admin
Posts: 96
Joined: Tue Aug 14, 2018 6:33 pm
Location: Huntington Beach, CA, USA

Re: Display number of images with GPS

Post 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);
-Stephen
Mates-K1
Posts: 37
Joined: Tue Aug 28, 2018 9:43 pm
Location: Ústí nad Orlicí, Czech Republic

Re: Display number of images with GPS

Post 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
Screenshot 2025-04-27 at 21-17-24 Skautská galerie - fotografie a videa střediska Žamberk.png (6.42 KiB) Viewed 5430 times
:
stephenbillard
Site Admin
Posts: 96
Joined: Tue Aug 14, 2018 6:33 pm
Location: Huntington Beach, CA, USA

Re: Display number of images with GPS

Post 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.
-Stephen
Mates-K1
Posts: 37
Joined: Tue Aug 28, 2018 9:43 pm
Location: Ústí nad Orlicí, Czech Republic

Re: Display number of images with GPS

Post by Mates-K1 »

In the SQL query variant that I managed to get working, it was necessary to modify

Code: Select all

$album->id
to

Code: Select all

$album->getID()
. 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?
stephenbillard
Site Admin
Posts: 96
Joined: Tue Aug 14, 2018 6:33 pm
Location: Huntington Beach, CA, USA

Re: Display number of images with GPS

Post 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);
-Stephen
Mates-K1
Posts: 37
Joined: Tue Aug 28, 2018 9:43 pm
Location: Ústí nad Orlicí, Czech Republic

Re: Display number of images with GPS

Post 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"?
Post Reply