# What's the function to find a city nearest to a given latitude?

I have recently started programming in Mathematica and I am currently struggling to find a way to locate the nearest city based on a given latitude. I have looked into GeoPosition and GeoNearest, but I find all these functions a bit confusing.

If someone could point me in the right direction or guide me, I would greatly appreciate it!

• Welcome to Mathematica StackExchange! So if I understand correctly: You are looking at the circle on the Earth with given latitude, and you want to find the city with this latitude? Most likely, there will be many cities that are crossed by this latitude ... So are you sure you want just one of them? The centerpoint (coordinates) of the city are somehow arbitrarily defined. Commented May 1, 2023 at 10:40
• Thank you! Mathematica feels a bit overwhelming from writing Python :) With all these given functions and symbols. But, hoping to find supportive community. Commented May 1, 2023 at 12:30
• I suggest reading The Wolfram Language: Fast Introduction for Programmers which includes notes for people comming from Python background. Commented May 1, 2023 at 12:39
• @Domen I read that tutorial, with my horror, saw only one YouTube video that used Mathematica and the guy started to write Prepend inside a function and I was like WHAT! And that fast introduction does not involve much with loops and functions in general, so not provided much help, unfortunately! Commented May 1, 2023 at 12:57
• That is because loops (do, while, for) are very rarerly used in Mathematica. You just have to get used that there are some conceptual differences between the two languages. I strongly suggest you read through the tutorial (also Stephen Wolfram An Elementary Introduction to the Wolfram Language because it will save you a lot of confusion. Commented May 1, 2023 at 13:05

You can use GeoNearest and GeoPath.

latitude = 46.06;

cities = GeoNearest["City", GeoPath[{"Parallel", latitude}], {All, 0},
DistanceFunction -> "Center"]
(* {Entity["City", {"Dobrova", "Osrednjeslovenska", "Slovenia"}],
Entity["City", {"Ljubljana", "Osrednjeslovenska", "Slovenia"}],
Entity["City", {"Premariacco", "FriuliVeneziaGiulia", "Italy"}],
...} *)

Length[cities]
(* 11 *)

Show[GeoGraphics[GeoPath[{"Parallel", latitude}]], GeoListPlot[cities]]


However, there are two problems. First, as shown above, there may be multiple cities having the exact same lattitude. Second, if no city is found at the exact lattitude, you will have to repeat the procedure repeatedly by increasing the allowed tolerance (set to 0 in the code above) until a match is found. This is because there seems to be a bug in GeoNearest when used with GeoPath so that results are not given in correct order.

If you want to restrict the search only to capitals, you can first get a list of all of them, then use it inside GeoNearest.

capitals = Map[CountryData[#, "CapitalCity"] &, CountryData["UN"]];
GeoNearest[capitals, GeoPath[{"Parallel", latitude}]]
(* {Entity["City", {"Ljubljana", "Osrednjeslovenska", "Slovenia"}]} *)


Now there are only two such lattitudes with more than one capital city:

Select[GatherBy[EntityValue[capitals, {"Name", "Latitude"}], Last],
Length[#] > 1 &] // Grid
(* {Beijing, 39.93°}, {Ankara, 39.93°}
{Tehran, 35.67°}, {Tokyo, 35.67°} *)

• for a given latitude, I want to find the closest city to it. That means If I use the function or code, I just type the latitude and my code will show the nearest city to that latitude. Can we do that? Commented May 1, 2023 at 12:29
• Welcome Ammar, here is a hint to get you started in the right direction. Make a function that starts like findCitiesAtLatitude[latitude_, number_]:= Block[{cities=…..},…] Commented May 1, 2023 at 12:35
• @Ammar, there is a problem with what you want. For example: both Ljubljana (Slovenia) and Madaras (Hungary) have the same latitude which you can see by evaluating Entity["City", {"Ljubljana", "Osrednjeslovenska", "Slovenia"}]["Latitude"] and Entity["City", {"Madaras", "BacsKiskun", "Hungary"}]["Latitude"], both returning Quantity[46.06, "AngularDegrees"]. How to then choose one of them if you give lattitude 46.06? Commented May 1, 2023 at 12:38
• @Domen Yes, you're right! Can we somehow just limit the function or code to show the nearest capital city of a given latitude? Commented May 1, 2023 at 12:50
• @CraigCarter Thank you and I actually, thought of that, but, the functions to find the cities nearest to something, I am having a hard time to understand what to use or how to do that. Documentation is OKayish! And don't really have any further materials : ) Trying get to used to myself with mathematica, haha! Commented May 1, 2023 at 12:54