# 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:** 14\
**Page:** 2

<div class="post-metadata">

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

</div>

```julia
julia> deg2dec(-0.0, 30.0, 17.12345)
0.5047565138888889

```

Doesn’t seem to work very well.

---

<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:54pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/22 "2024-12-19T13:54:21Z")

</div>

> [@giordano](#):
>
> Doesn’t seem to work very well.

What is the issue, as I don’t get it from your example, sorry. The degrees input should be an integer, not float, and then -0 deg does not exist in data, it is always defined as 0 deg, AFAIK. And in this case the sign should go to the minutes (or seconds, if the minutes are also 0).

---

<div class="post-metadata">

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

</div>

> [@rafael.guerra](#):
>
> You can consider this example:
> 
> ```julia
> deg2dec(0, -30, 17.12345) # -0.5047565138888889
> 
> using AstroAngles
> dms2deg(0, -30, 17.12345) # -0.49524348611111113
> 
> ```

I suppose the questions are, what is the standard representation? And based on that, what is the bug. Thinking about astronomy specifically, if one were writing a paper, then the position of the sign would **always** be in front of degrees, even if it were zero, e.g.

```julia
DEC = -0d30m17.12345s # the correct way, in a paper
DEC = 0d-30m17.12345s # the wrong way
DEC = -30m17.12345s # the most wrong way

```

It seems the best solution would be either use the GMT code that @joa-quim provided, or use one of the code patterns that accepts string input, e.g. `dms"1°2′3″"deg` or `dms"12:17:25.3"deg` syntax from AstroAngles. Especially considering that DMS coordinates need to be read as strings to begin with.

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 19, 2024, 2:00pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/24 "2024-12-19T14:00:29Z")

</div>

I would also add that this code would need to be refined a bit to handle edge cases such as wrapping around at 360 deg or +/-180 deg for longitude, and disallowing values greater than 90 degrees (or less than -90) for latitude.

---

<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 19, 2024, 3:22pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/25 "2024-12-19T15:22:18Z")

</div>

It’s seems like I would need to figure out how GMT deals with lat/long coordinates or use AstroAngles so far.

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 19, 2024, 3:47pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/26 "2024-12-19T15:47:23Z")

</div>

And getting back to your original question, I would also add that there is almost certainly a `gdal` funciton that does this conversion, so it would then be accessible through `ArchGDAL`, but I’m not familiar enough with either package to know how to code it.

---

<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 19, 2024, 4:09pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/27 "2024-12-19T16:09:33Z")

</div>

I mean, I would _hope_ so. I’ll have to dig around the ArchGDAL API docs some more, but it couldn’t find anything initially.

**EDIT:** There’s `CoordTransform` which I would initially think is related to what I want to do, but I can’t find anything specifically about turning degree coordinates into decimals.

---

<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 19, 2024, 6:58pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/28 "2024-12-19T18:58:47Z")

</div>

> [@alecStewart1](#):
>
> It’s seems like I would need to figure out how GMT deals with lat/long coordinates

First, it is **lon/lat** , **NOT** lat/lon 🙂

GMT does that all the time but normally in its internals. A dd:mm:ss is a string so we can read from file but not (for now) possible via function argument. If we put this in a file

```julia
-0:30:17.12345	13S	4344.8	2001-12-12T14:55	2c	1e	44:45N	88.4	The First String

```

than we can read it with

```julia
gmtread("ddmm.txt")
...
 Row │ col.1 col.2 col.3 Time col.5 col.6 col.7 col.8 Text
─────┼────────────────────────────────────────────────────────────────────────────────────────────────
   1 │ -0.504757 -13.0 4344.8 2001-12-12T14:55:00 0.787402 0.001 44.75 88.4 The First String

```

GMT also requires that the degree part must be provided, even if `0`

Maybe you can show us an example why/where you are actually needing this.

---

<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 20, 2024, 4:53am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/29 "2024-12-20T04:53:22Z")

</div>

This is my code for degree decimal

```julia
function deg2dec(d,m,s)
    local negdeg = false
    if (d < 0) || (m < 0) || (s < 0)
        negdeg = true
    end
    return (negdeg == true ? -1 : 1) * ( abs(d) + (abs(m)/60) + (abs(s)/3600) )
end

```

---

<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 20, 2024, 5:34am UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/30 "2024-12-20T05:34:32Z")

</div>

It is weird to allow both non-zero degrees and negative minutes, for instance. The negative minutes or negative seconds input to the function should only be allowed when all previous arguments are zero. See the linked Matlab page in my post further above.

---

<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 20, 2024, 3:55pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/31 "2024-12-20T15:55:23Z")

</div>

I guess maybe to simply this, I found the following:

ArchGDAL.jl has the methods `getx` and `gety` which return a spatial location for a GDAL geometry. These number are quite big, represented in Julia as `6.99827e6` and using `@printf` to get the “regular” representation of that number is something like `6998271.088400`.

If I were to plot this on a map, which would be with ArcGIS, would those be properly placed on a map?

---

<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 20, 2024, 6:49pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/32 "2024-12-20T18:49:18Z")

</div>

UTM `y's` go from 0 to 10000 km (N hemisphere) so a `6998271.0884` can be a UTM number. But the shapefile also has information about the coordinate system, which is a fundamental must know info.

Again, what are you trying to do? If you try GMT.jl I bet that this will produce something comprehensible.

```julia
using GMT

D = gmtread("path/to/shape.shp")
viz(D)

```

---

<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 20, 2024, 7:35pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/33 "2024-12-20T19:35:24Z")

</div>

I would eventually like to be able to find points in a given range of coordinates that represent a region/country/county/province/terriority/whatever. Based on the data of those points that are within that range I can calculate something, be it sum, average, whatever.

To give an example, say I read some data from a CSV file (it really doesn’t matter what the source is so long as I can do the filter that I want) that has latitude (`Float64`), longitude (`Float64`), some name and some numeric value I’m interested in:

```julia
using CSV
using DataFrames

df = CSV.read("my_file.csv", DataFrame)

# the dataframe might look something like this
#
# | id | lat | long | numeric_value |
# |------+---------+---------+---------------|
# | xxxx | xxxxxxx | xxxxxxx | 10000 |

```

It seems like converting degrees to decimals is a tad difficult to get correct, but at the end of the day, the data is in the decimal representation of latitude and longitude. It’s not any special geospatial data type, just a decimal or specially `Float64` in Julia.

Do I care to plot the data on the map? Maybe at some point, but that’s not what I care about at the moment.

---

<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 20, 2024, 8:28pm UTC](https://discourse.julialang.org/t/latitude-and-longitude-decimal-range-from-degrees/123964/34 "2024-12-20T20:28:05Z")

</div>

> [@alecStewart1](#):
>
> I would eventually like to be able to find points in a given range of coordinates that represent a region/country/county/province/terriority/whatever.

See [gmtselect](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/gmtselect/), [inwhichpolygon](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/utilities/inwhichpolygon/index.html#inwhichpolygon) and others in the GMT manual. But that’s one example. Other packages offer this type of operations as well.

Regarding the degrees-to-dec I still fail to see what is the problem.

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