Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Projection classes to encapsulate arguments #379

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0c969bb
Initial commit for pygmt/projection.py; Contains a generic design/lay…
Nov 17, 2019
16819cc
Updated docstrings for class projection definitions.
Nov 18, 2019
979d057
Initial run of Black to reformat code.
Nov 18, 2019
c1161a3
Renamed lon0 to central_longitude and lat0 to central_latitude.
Nov 19, 2019
29e263d
Added the attribute to give meaning to the width argument (inches or…
Nov 19, 2019
129ae99
Updated example, and cleaned up docstring for unit description.
Nov 19, 2019
adef7ad
Removed center attribute from printed example.
Dec 7, 2019
97c836b
Specifying the projection code directly in the attrib creation, rathe…
Dec 7, 2019
a414f7e
Added the miscellaneous projections group; Mollweide, Sinusoidal, Eck…
Dec 7, 2019
c7ee1a2
Capitalised projection names where required, eg when named after the …
Dec 7, 2019
db7aca6
Added the Polyconic projection.
Dec 7, 2019
825cd66
Added the Miller and oblique 1, 2, 3 projections.
Dec 7, 2019
acaca7d
Added the Transverse Mercator and Universal Transverse Mercator Proje…
Dec 7, 2019
919102d
Added the equidistant cylindrical projection.
Dec 7, 2019
f5897a8
Fixed as per @leouieda suggestions.
Dec 7, 2019
7abe416
Missed one of the fixes as suggested by @leouieda
Dec 8, 2019
45abd59
Changed the default unit of inches to centimetres.
Dec 8, 2019
b9997c1
Removed superfluous comments regarding the private variables.
Dec 8, 2019
f92c7d1
Run Black formatting.
Dec 8, 2019
4161f9e
Update keyword args for the GeneralPerspective projection.
Jan 14, 2020
968b2cd
Initial unittests for the projection class configurations.
Jan 14, 2020
a77fd13
Added Polar and Linear projections. General cleanup.
Dec 18, 2022
d030593
Apply black formatting
Dec 18, 2022
cd38f61
Various reconfigs; some projs have updated, updated some that specifi…
Dec 19, 2022
4793155
Added a bunch more projections to the test suite.
Dec 19, 2022
0f24da2
Reworked the cylindrical projections to cater for the default and non…
Dec 20, 2022
a43d4c8
Added tests for the 3 oblique mercator projection options.
Dec 20, 2022
f39053a
Added tests for UTM, mercator, equidistant cylindrical. Minor additio…
Dec 21, 2022
f386b09
Applied black formatting.
Dec 21, 2022
8995715
Merge branch 'main' into proj-classes
Dec 23, 2022
20693e7
Caught test fails and updated.
Dec 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initial commit for pygmt/projection.py; Contains a generic design/lay…
…out for a sample of the projections supported by GMT.
  • Loading branch information
Josh Sixsmith committed Nov 17, 2019
commit 0c969bba7f3b8a4c9f8c8c1b4006d4c72642e8db
315 changes: 315 additions & 0 deletions pygmt/projection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
#!/usr/bin/env python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#!/usr/bin/env python


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both lines are unnecessary since this is never run as a script.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove.

"""
Contains the projections supported by GMT, and the necessary mechanisms
to create a projection and output a valid GMT projection string.

>>> from pygmt import projection
>>> proj = projection.LambertAzimuthalEqualArea(lon0=30, lat0=-20, horizon=60, width="8i")
>>> proj
LambertAzimuthalEqualArea(lon0=30, lat0=-20, horizon=60, width='8i')
>>> print(proj)
A30/-20/60/8i
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples are better placed in the tutorials, gallery, or class docstrings. This one will never be rendered in the documentation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove the module level example. I'll put some examples together in the tutorials section soon.

"""

from enum import Enum
import attr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attr is a good choice for this 👍



class Supported(Enum):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in #356, what is the intended use case for this class? Sorry if I'm being thick 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really needed anymore. I was mostly using it as a place holder for the GMT projection codes, and to keep track (for myself) what had and had not been implemented.
I'll remove it when the projection class module gets closer to completion.


"""
The supported projections and their GMT code.
"""

UNDEFINED = ""
LAMBERT_AZIMUTH_EQUAL_AREA = "A" # DONE
ALBERS_CONIC_EQUAL_AREA = "B" # DONE
CASSINI_CYLINDRICAL = "C" # DONE
CYLINDRICAL_STEROGRAPHIC = "JCyl_stere/" # includes `/` according to https://docs.generic-mapping-tools.org/latest/proj_codes.html # DONE
EQUIDISTANT_CONIC = "JD" # DONE
AZIMUTHAL_EQUIDISTANT = "E" # DONE
AZIMUTHAL_GNOMIC = "F" # DONE
AZIMUTHAL_ORTHOGRAPHIC = "G" # DONE
GENERAL_PERSPECTIVE = "G" # DONE
HAMMER_EQUAL_AREA = "H"
SINUSOIDAL_EQUAL_AREA = "I"
MILLER_CYLINDRICAL = "J"
ECKERT_IV_EQUAL_AREA = "Kf"
ECKERT_VI_EQUAL_AREA = "Ks"
LAMBERT_CONIC_CONFORMAL = "L"
MERCATOR_CYLINDRICAL = "M" # DONE
ROBINSON = "N"
OBLIQUE_MERCATOR_1 = "Oa"
OBLIQUE_MERCATOR_2 = "Ob"
OBLIQUE_MERCATOR_3 = "Oc"
POLAR = "P"
POLYCONIC = "Poly"
EQUIDISTANT_CYLINDRICAL = "Q"
WINKEL_TRIPEL = "R"
GENERAL_STEREOGRAPHIC = "S" # DONE
TRANSVERSE_MERCATOR = "T"
UNIVERSAL_TRANSVERSE_MERCATOR = "U"
VAN_DER_GRINTEN = "V"
MOLLWEIDE = "W"
LINEAR = "X"
CYLINDRICAL_EQUAL_AREA = "Y" # DONE


@attr.s()
class _Projection:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

No empty lines between class/function and docstring.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my bad. I'll update all places.

"""
Base class for all projections.
"""

# private; we don't want the user to care or know about
_fmt: str = attr.ib(init=False, repr=False, default="{_code}")
_code: str = attr.ib(init=False, repr=False,
default=Supported.UNDEFINED.value)

def __str__(self):
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
exclude = attr.fields(self.__class__)._fmt
kwargs = attr.asdict(self, filter=attr.filters.exclude(exclude))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does _code or other _ arguments not need to be excluded as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_code is required for the string formatter to work. But I thought it best to keep the attribute hidden from the user, and let the class populate by default the required GMT code.

return self._fmt.format(**kwargs)


@attr.s(kw_only=True)
class _Azimuthal(_Projection):

"""
Base class for azimuthal projections.
"""

lon0: float = attr.ib()
lat0: float = attr.ib()
horizon: float = attr.ib(default=90)
width: str = attr.ib()

# private; we don't want the user to care or know about
_fmt: str = attr.ib(init=False, repr=False,
default="{_code}{lon0}/{lat0}/{horizon}/{width}")
_code: str = attr.ib(init=False, repr=False,
default=Supported.UNDEFINED.value)

@horizon.validator
def check_horizon(self, attribute, value):
"""
Validate the horizon attribute.
"""
if value > 180:
raise ValueError("horizon must be less than or equal to 180")


@attr.s(kw_only=True)
class _Cylindrical(_Projection):

"""
Base class for cylindrical projections.
"""

lon0: float = attr.ib()
lat0: float = attr.ib()
width: str = attr.ib()

# private; we don't want the user to care or know about
_fmt: str = attr.ib(init=False, repr=False,
default="{_code}{lon0}/{lat0}/{wdith}")
_code: str = attr.ib(init=False, repr=False,
default=Supported.UNDEFINED.value)

@attr.s(kw_only=True)
class _Conic:

"""
Base class for conic projections.
"""

lon0: float = attr.ib()
lat0: float = attr.ib()
lat1: float = attr.ib()
lat2: float = attr.ib()
width: float = attr.ib()

# private; we don't want the user to care or know about
_fmt: str = attr.ib(init=False, repr=False,
default="{_code}{lon0}/{lat0}/{lat1}/{lat2}/{width}")


@attr.s(frozen=True)
class LambertAzimuthalEqualArea(_Azimuthal):

"""
Definition for the lambert azimuthal equal area projection.
"""

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.LAMBERT_AZIMUTH_EQUAL_AREA.value)


@attr.s(frozen=True)
class AzimuthalEquidistant(_Azimuthal):

"""
Definition for the azimuthal equidistant projection.
"""

horizon: float = attr.ib(default=180, kw_only=True)

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.AZIMUTHAL_EQUIDISTANT.value)


@attr.s(frozen=True)
class AzimuthalGnomic(_Azimuthal):

"""
Definition for the azimuthal gnomic projection.
"""

horizon: float = attr.ib(default=60, kw_only=True)

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.AZIMUTHAL_EQUIDISTANT.value)

@horizon.validator
def check_horizon(self, attribute, value):
"""
Validate the horizon attribute.
"""
if value >= 90:
raise ValueError("horizon must be less than 90")


@attr.s(frozen=True)
class AzimuthalOrthographic(_Azimuthal):

"""
Definition for the azimuthal orthographic projection.
"""

horizon: float = attr.ib(default=60, kw_only=True)

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.AZIMUTHAL_EQUIDISTANT.value)

@horizon.validator
def check_horizon(self, attribute, value):
"""
Validate the horizon attribute.
"""
if value > 90:
raise ValueError("horizon must be less than or equal to 90")


@attr.s(frozen=True, kw_only=True)
class GeneralPerspective(_Projection):

"""
Definition for the azimuthal general perspective projection.
"""

lon0: float = attr.ib()
lat0: float = attr.ib()
altitude: float = attr.ib()
azimuth: float = attr.ib()
tilt: float = attr.ib()
twist: float = attr.ib()
Width: float = attr.ib()
Height: float = attr.ib()
width: float = attr.ib()

# private; we don't want the user to care or know about
_fmt: str = attr.ib(init=False, repr=False,
default=("{_code}{lon0}/{lat0}/{altitude}/{azimuth}/"
"{tilt}/{twist}/{Width}/{Height}/{width}"))
_code: str = attr.ib(init=False, repr=False,
default=Supported.GENERAL_PERSPECTIVE.value)


@attr.s(frozen=True)
class GeneralSterographic(_Azimuthal):

"""
Definition for the azimuthal general sterographic projection.
"""

horizon: float = attr.ib(default=90, kw_only=True)

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.GENERAL_STEREOGRAPHIC.value)

@horizon.validator
def check_horizon(self, attribute, value):
"""
Validate the horizon attribute.
"""
if value >= 180:
raise ValueError("horizon must be less than 180")


@attr.s(frozen=True, kw_only=True)
class AlbersConicEqualArea(_Conic):

"""
Definition for the albers conic equal area projection.
"""

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.ALBERS_CONIC_EQUAL_AREA.value)


@attr.s(frozen=True, kw_only=True)
class EquidistantConic(_Conic):

"""
Definition for the equidistant conic projection.
"""

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.EQUIDISTANT_CONIC)


@attr.s(frozen=True)
class CassiniCylindrical(_Cylindrical):

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.CASSINI_CYLINDRICAL.value)


@attr.s(frozen=True)
class MercatorCylindrical(_Cylindrical):

lon0: float = attr.ib(default=180, kw_only=True)
lat0: float = attr.ib(default=0, kw_only=True)

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.MERCATOR_CYLINDRICAL.value)


@attr.s(frozen=True)
class CylindricalStereographic(_Cylindrical):

lon0: float = attr.ib(default=180, kw_only=True)
lat0: float = attr.ib(default=0, kw_only=True)

# private; we don't want the user to care or know about
_code: str = attr.ib(init=False, repr=False,
default=Supported.CYLINDRICAL_STEROGRAPHIC.value)


@attr.s(frozen=True)
class CylindricalEqualArea(_Cylindrical):

# private; we don't want the user to care or know about
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# private; we don't want the user to care or know about

No need to repeat the same comment everywhere. In fact, _ is commonly used in Python to indicate "private" so there is no need for this comment at all. In general, our rule is that it's better to have code that doesn't need comments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair comment. I'll remove them.

_code: str = attr.ib(init=False, repr=False,
default=Supported.CYLINDRICAL_EQUAL_AREA.value)