# Latitude and longitude decimal range from degrees

**URL:** <https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964>\
**Category:** New to Julia\
**Tags:** geo\
**Created:** [December 18, 2024, 4:14pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964 "2024-12-18T16:14:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alecStewart1](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@alecStewart1](https://discourse.julialang.org/u/alecStewart1)\
**Post date:** [December 18, 2024, 4:14pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/1 "2024-12-18T16:14:11Z")

</div>

Hello friends!

One of the use cases for Julia that I’m finding for work is for geographical purposes. However geography wasn’t my specialization in school. 😅

One of the things I’m wanting to do is collect data within countries/regions/provinces/counties/etc, which are defined by some latitude and longitude degree range.

Thing is when looking at geographical data with something like GeoDataFrames.jl, lat and long aren’t stored as degrees but as large decimal points.

So say we look at Texas, which is massive, and it’s spans something like 25° 50’ N to 36° 30′ N (Latitude) and 93° 31′ W to 106° 439′ W (Longitude).

If I can transform the above degree ranges into decimal ranges, I can filter my geographical dataframe for points, polygons, whatever if their lat and long is within those ranges.

Is there something in ArchGDAL.jl to do this?

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [December 18, 2024, 5:36pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/2 "2024-12-18T17:36:09Z")

</div>

maybe worth taking a look at

- [GitHub - GeoRegionsEcosystem/GeoRegions.jl: Defining Geographical Regions for Geoscience and Climate](https://github.com/GeoRegionsEcosystem/GeoRegions.jl)
- [GitHub - JuliaGeo/NaturalEarth.jl: Julia interface to Natural Earth data](https://github.com/JuliaGeo/NaturalEarth.jl)

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [December 18, 2024, 5:48pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/3 "2024-12-18T17:48:13Z")

</div>

> [@alecStewart1](#):
>
> large decimal points

What do you mean by this?

This? 93° 31′ W == 93.5166666667° W  
or something else?

---

<div class="post-metadata">

**Author:** ![alecStewart1](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@alecStewart1](https://discourse.julialang.org/u/alecStewart1)\
**Post date:** [December 18, 2024, 5:53pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/4 "2024-12-18T17:53:06Z")

</div>

> [@TimG](#):
>
> This? 93° 31′ W == 93.5166666667° W  
> or something else?

More or less yes, because generally for anything GIS related (something I do at work) you don’t have `93° 31′ W` or `93.5166666667° W` in the data you want to put on a map. It’s usually just `93.5166666667` is a longitude data point in your data.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 18, 2024, 5:54pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/5 "2024-12-18T17:54:13Z")

</div>

> [@alecStewart1](#):
>
> 25° 50’ N to 36° 30′

Don’t know if it helps but GMT.jl accepts also the limits/regions in “dd::mm:ss”. _e.g._

```julia
using GMT

coast(region=("-108:43.9", "-91:31", "24:50", "37:30"), borders=(1,:red), shore=true, show=true)

```

Red line is the Texas border.

 ![texas](https://global.discourse-cdn.com/julialang/original/3X/4/4/449b021fa09ec8e9bbbace80de0f27682d6689a6.png)

---

<div class="post-metadata">

**Author:** ![alecStewart1](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@alecStewart1](https://discourse.julialang.org/u/alecStewart1)\
**Post date:** [December 18, 2024, 5:58pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/6 "2024-12-18T17:58:41Z")

</div>

> [@lazarusA](#):
>
> - [GitHub - GeoRegionsEcosystem/GeoRegions.jl: Defining Geographical Regions for Geoscience and Climate](https://github.com/GeoRegionsEcosystem/GeoRegions.jl)
> - [GitHub - JuliaGeo/NaturalEarth.jl: Julia interface to Natural Earth data](https://github.com/JuliaGeo/NaturalEarth.jl)

Interesting! I guess I would need to decide if

- Giorgi & Francisco
- Seneviratne, or
- IPCC AR6

Fits my use cases best for the first one. The second I’d have to look at a bit more.

---

<div class="post-metadata">

**Author:** ![alecStewart1](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@alecStewart1](https://discourse.julialang.org/u/alecStewart1)\
**Post date:** [December 18, 2024, 6:00pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/7 "2024-12-18T18:00:53Z")

</div>

Ah, thank you for reminding of GMT.jl! I was trying to recall what package I found previously to map geograhpical data.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2024, 6:02pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/8 "2024-12-18T18:02:43Z")

</div>

> [@alecStewart1](#):
>
> If I can transform the above degree ranges into decimal ranges

Isn’t this achieved by a simple function:

```julia
deg2dec(d, m, s) = d + m/60 + s/3600

# example:
deg2dec(25, 50, 0) # 25.833333333333332

```

---

<div class="post-metadata">

**Author:** ![alecStewart1](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@alecStewart1](https://discourse.julialang.org/u/alecStewart1)\
**Post date:** [December 18, 2024, 6:20pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/9 "2024-12-18T18:20:14Z")

</div>

If that correctly lines up with how latitudes and longitudes are represented, yes.

Apologies if this seemed like a silly question.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2024, 7:19pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/10 "2024-12-18T19:19:15Z")

</div>

> [@alecStewart1](#):
>
> If that correctly lines up with how latitudes and longitudes are represented, yes.

I think it does.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [December 18, 2024, 11:19pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/11 "2024-12-18T23:19:38Z")

</div>

> Blockquote

```julia
deg2dec(d, m, s) = d + m/60 + s/3600

# example:
deg2dec(25, 50, 0) # 25.833333333333332

```

You got to be careful if the values are NEGATIVE  
For example: -23degrees 12minutes 46sec

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2024, 11:32pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/12 "2024-12-18T23:32:17Z")

</div>

Good point.  
Can be edited to:

```julia
deg2dec(d, m, s) = (d!=0) ? sign(d)*(abs(d) + m/60 + s/3600) : m/60 + s/3600

```

_PS:_  
_sign(0) == 0 which is a bit annoying_

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 19, 2024, 12:09am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/13 "2024-12-19T00:09:01Z")

</div>

The formula above does not properly handle all cases near zero.

An algorithm is described in Matlab’s [`dms2degrees()` function description](https://www.mathworks.com/help/map/ref/dms2degrees.html?w.mathworks.com).

A first attempt below to write such function:

```julia
function deg2dec(d, m, s)
    t = (d, m, s)
    (t[findfirst(!=(0), t)] < 0) ? SGN = -1 : SGN = 1
    ((t[2] < 0 && t[1] != 0) || (t[3] < 0 && any(!=(0), t[1:2]))) && throw(ArgumentError("Invalid input"))
    return SGN * (abs(d) + abs(m)/60 + abs(s)/3600)
end

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 19, 2024, 1:26am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/14 "2024-12-19T01:26:08Z")

</div>

```julia
julia> using AstroAngles

julia> dms"1°2′3″N"
0.018049613347708025 # radians

julia> dms"1°2′3″N"deg
1.0341666666666667 # degrees

```

Also provides string parsing/formatting, and assemble/split into components, see AstroAngles.jl.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 19, 2024, 6:20am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/15 "2024-12-19T06:20:29Z")

</div>

@aplavin, I looked briefly at your code and the various cases handled by the Matlab function indicated above don’t seem to be treated?

Consider a small area of ​​interest defined across the equator with zero latitude (and/or across Greenwich with zero longitude). A location can then be defined with negative minutes or negative seconds, but the signs cannot be arbitrary.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 19, 2024, 11:00am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/16 "2024-12-19T11:00:46Z")

</div>

Can you give a specific example?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 19, 2024, 11:17am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/17 "2024-12-19T11:17:16Z")

</div>

You can consider this example:

```julia
deg2dec(0, -30, 17.12345) # -0.5047565138888889

using AstroAngles
dms2deg(0, -30, 17.12345) # -0.49524348611111113

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 19, 2024, 11:40am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/18 "2024-12-19T11:40:31Z")

</div>

Sounds like a straight up bug in that package.

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [December 19, 2024, 12:54pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/19 "2024-12-19T12:54:20Z")

</div>

In the OP case, though, won’t deg, min and sec always be of the same sign, and a simple sum will give the correct magnitude in decimal degrees.

The sign can be taken straight from E (+ve) or W (-ve), N (+ve) or S (-ve).

Is that too simplistic?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 19, 2024, 1:08pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/20 "2024-12-19T13:08:48Z")

</div>

As the OP data is far from zero latitude or longitude, the simple sum formula is fine.

[Next page](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964.md?page=2)
