# Cannot create 2D Cartesian points with specified units

**URL:** <https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020>\
**Category:** General Usage\
**Tags:** question, geo\
**Created:** [October 30, 2024, 9:38pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020 "2024-10-30T21:38:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 9:38pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/1 "2024-10-30T21:38:55Z")

</div>

```julia
using CoordRefSystems, Unitful

rand(Cartesian2D{NoDatum, typeof(1.0u"m")})

```

does not work.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 30, 2024, 9:50pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/2 "2024-10-30T21:50:06Z")

</div>

What is your ultimate goal? The `rand` api in CoordRefSystems.jl is pretty low-level and mostly internal at this point.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 9:54pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/3 "2024-10-30T21:54:05Z")

</div>

I want to create synthetic scenes to test geometric inference algorithms. For that, I need to generate random data. I can’t generate random data without default units, which seems limiting.

The Unitful types are not easy to parse (or understand). Unfortunately, I could not add a rand function that will allow me to specify units.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 30, 2024, 9:54pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/4 "2024-10-30T21:54:52Z")

</div>

```julia
using Meshes
using CoordRefSystems
using Unitful

rand(Point, crs=Cartesian2D, 100) |> LengthUnit(u"ft")

```

```julia
100-element Vector{Point{𝔼{2}, Cartesian2D{NoDatum, Quantity{Float64, 𝐋, Unitful.FreeUnits{(ft,), 𝐋, nothing}}}}}:
 Point(x: 1.22995723127453 ft, y: 1.2578581984621078 ft)
 Point(x: 3.2635361225527983 ft, y: 0.971571140767104 ft)
 Point(x: 1.1144842750799535 ft, y: 0.2029331909786202 ft)
 Point(x: 2.6062199812344695 ft, y: 1.858510920611926 ft)
 Point(x: 2.0589584140282247 ft, y: 0.41389288343385094 ft)
 Point(x: 3.216505822297655 ft, y: 2.2673788472511975 ft)
 Point(x: 2.41786630208288 ft, y: 0.9093777827293031 ft)
 ⋮
 Point(x: 2.926702134428096 ft, y: 0.6350027654665833 ft)
 Point(x: 0.5839771521068765 ft, y: 2.6268370350416834 ft)
 Point(x: 0.9673777618597657 ft, y: 0.028165002572651938 ft)
 Point(x: 0.7529699261753467 ft, y: 0.7977590577763877 ft)
 Point(x: 2.4358324180744475 ft, y: 1.354883318069243 ft)
 Point(x: 2.4433776979835145 ft, y: 2.717828820597342 ft)

```

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 9:55pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/5 "2024-10-30T21:55:19Z")

</div>

No I want random points with units that I specify, not default u"m"

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 30, 2024, 9:58pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/6 "2024-10-30T21:58:21Z")

</div>

I shared an example above where the `LengthUnit` transform is used to change the units after creation of the random `Point` from Meshes.jl

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 9:59pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/7 "2024-10-30T21:59:05Z")

</div>

Okay, yes, that works, but you’re changing the default units from u"m" to u"ft." It seems you should be able to specify units as a type parameter to Cartesian2D, so this can be passed around as one type. I would like to specify the Cartesian coordinate system (with units) and then have the synthetic scene generated according to type, instead of carrying around separate type info for the units.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 10:05pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/8 "2024-10-30T22:05:51Z")

</div>

This would apply to Meshes as well, where random geometries are constructed.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 30, 2024, 10:09pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/9 "2024-10-30T22:09:11Z")

</div>

`Cartesian2D` is an alias to `Cartesian`:

```julia
const Cartesian2D{Datum} = Cartesian{Datum,2}

```

The full type is

```julia
Cartesian{Datum,N,L<:Len} <: Basic{Datum}

```

where `Len` is another alias:

```julia
const Len{T} = Quantity{T,u"𝐋"}

```

You can read the source code here:

> <https://github.com/JuliaEarth/CoordRefSystems.jl/blob/main/src/crs/basic.jl>

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 10:12pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/10 "2024-10-30T22:12:01Z")

</div>

I understand, but the original code doesn’t work when I specify L \<: Len. I don’t know why when the type is fully defined.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 10:16pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/11 "2024-10-30T22:16:01Z")

</div>

e.g. this doesnt work either:

`rand(Cartesian{NoDatum,2,Quantity{Float64,u"𝐋"}})`

which I would expect should.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 30, 2024, 10:42pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/12 "2024-10-30T22:42:12Z")

</div>

The type that is generated by default is:

```julia
Cartesian{NoDatum,2,CoordRefSystems.Met{Float64}}

```

where `Met` is also an alias:

```julia
const Met{T} = Quantity{T,u"𝐋",typeof(m)}

```

You can create an alias for the unit you have in mind, and define the type likewise:

```julia
const Ft{T} = Quantity{T,u"𝐋",typeof(ft)}

Cartesian{NoDatum,2,Ft{Float64}}

```

The `rand` methods are only defined for default units with default machine precision (Float64).

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 30, 2024, 10:45pm UTC](https://discourse.julialang.org/t/cannot-create-2d-cartesian-points-with-specified-units/122020/13 "2024-10-30T22:45:40Z")

</div>

Thanks for the responses.
