2.2 C
New York
Saturday, January 11, 2025

swift – Getting Metropolis and State from iOS picture library


You may significantly optimize your reverse geocoding by profiting from some issues:

  1. We are likely to shoot a whole lot of images in the identical common location. (Across the neighborhood. On the park. On the children’ faculty. On a trip in another metropolis.)
  2. The areas which can be assigned the identical metropolis/municipality identify are huge. Actually huge.
  3. For a given latitude, the variety of kilometers per diploma of latitude and longitude do not change very a lot until you progress to a really completely different latitude.*

This is what I might do: Kind your footage to be reverse-geocoded by the date they had been taken. (That must be within the EXIF metadata. You need to course of them within the order they had been taken as a result of we are likely to take a complete sequence of pictures in a single common location.) Take your first image. Fetch the lat/lengthy from the EXIF knowledge. Calculate kilometers/diploma of latitude and kilometers/diploma of longitude. Save these values.

Create an empty desk of metropolis/state names and latitudes/logitudes.
Reverse geocode that first image. Save its metropolis/state and lat/lengthy into your desk, and bear in mind the index within the desk. (If you wish to enhance your knowledge, fetch and save the lat/lengthy of the centroid of town/municipality, and its approximate diameter if out there. Then when matching new places, you possibly can see if the brand new location is roughly inside that diameter.)

Load your subsequent image. Use your kilometers/diploma values to calculate how far that image’s location is from the present metropolis/state you’re working with. If it is inside some margin (say 1/2 kilometer) then simply assume it is in the identical metropolis/state, and assign that image to town/state with out reverse-geocoding.

If a brand new image is simply too far-off from the earlier image to imagine it is in the identical metropolis/state, do math on all the opposite saved metropolis/states in your desk to seek out the space between the brand new image and every of these metropolis/states. In case you discover a metropolis/state shut sufficient, assign that one to the image. If not, reverse-geocode your new image’s location and save IT to your metropolis/state desk.

Write code that runs the above course of within the background, working by way of your picture library, monitoring what number of reverse-geocoding requests you make per minute, and easily pause whenever you hit the speed restrict. (Notice that on iOS, apps cannot actually run within the background. You’d write your code to do its batch processing on a background thread so the UI stays responsive, and also you’d have to maintain the app frontmost and maintain your cellphone awake to ensure that it to maintain getting processor time. (If that is for private use, you possibly can cheat and do issues like set off a long-playing sound and set your self up as a music enjoying app. These are allowed to run within the background, and should you do not submit it to the App Retailer you will get away with the cheat.)

In case you shoot a big batch of images inside a day’s drive, the above grouping strategy ought to allow you to get metropolis/state knowledge for 1000’s of images with only a handful of reverse-geocoding requests.


*In case you journey to wildly completely different latitudes, like Alaska and Key West, my suggestion of calculating a set variety of kilometers per diploma of latitude and longitude will not work properly as a result of the space between diploma strains adjustments an excessive amount of in numerous latitudes.

If you wish to make your calculations extra correct you possibly can lookup the “Havershine method”, which helps you to calculate “nice circle” distances between any 2 factors on the globe. It makes use of 3D trig to calculate distances between factors on the floor of a sphere. It is far more math-intensive than the approximations I recommended, and it is extra work to determine find out how to use it, however it’ll work anyplace on the globe, from the poles to the equator. On a contemporary pc/cellphone it is nonetheless fairly quick.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles