jgdtrans.mesh module#

Provides utilities handling the mesh.

We note that MeshCoord supports non-negative latitude and longitude only. Therefore, MeshNode and MeshCell have the same restriction of MeshCoord.

The third digit of MeshCoord depends on mesh. If the mesh unit is 5, it takes 0 or 5 only. Hence, the methods/operations that relate with MeshCoord throws an exception, if 5 is given even though the third digit is neither 0 nor 5, in general.

jgdtrans.mesh.MESH_COORD_MIN: Final[MeshCoord] = MeshCoord(first=0, second=0, third=0)#

Smallest MeshCoord value.

Equals to MeshCoord(first=0, second=0, third=0).

jgdtrans.mesh.MESH_COORD_MAX: Final[MeshCoord] = MeshCoord(first=99, second=7, third=9)#

Largest MeshCoord value.

Equals to MeshCoord(first=99, second=7, third=9).

jgdtrans.mesh.MESH_NODE_MIN: Final[MeshNode] = MeshNode(latitude=MeshCoord(first=0, second=0, third=0), longitude=MeshCoord(first=0, second=0, third=0))#

Smallest MeshNode value.

Equals to MeshNode(latitude=MeshCoord(0, 0, 0), longitude=MeshCoord(0, 0, 0)).

jgdtrans.mesh.MESH_NODE_MAX: Final[MeshNode] = MeshNode(latitude=MeshCoord(first=99, second=7, third=9), longitude=MeshCoord(first=80, second=0, third=0))#

Largest MeshNode value.

Equals to MeshNode(latitude=MeshCoord(99, 7, 9), longitude=MeshCoord(80, 0, 0)).

jgdtrans.mesh.mesh_unit(format: _types.FormatType) _types.MeshUnitType#

Returns the mesh unit of the format.

Returns:

1 or 5

Examples

>>> mesh_unit("TKY2JGD")
1
>>> mesh_unit("SemiDynaEXE")
5
jgdtrans.mesh.is_meshcode(meshcode: int) bool#

Returns True when meshcode is a valid.

Notes, this does not test meshcode is a int obj.

Parameters:

meshcode – a test value.

Returns:

True when meshcode is a valid.

Examples

>>> is_meshcode(54401027)
True
>>> is_meshcode(-1)
False
>>> is_meshcode(100000000)
False
class jgdtrans.mesh.MeshCoord(first: int, second: int, third: int)#

Bases: object

Represents mesh coordinate, namely, discrete latitude and/or longitude.

This supports total ordering, and non-negative latitude and/or longitude only.

The coordinate has three digits, first, second and third, the first values 0 to 9, the second does 0 to 7 and the third does 0 to 99 inclusive.

The constructor throws ValueError when the digits are out-of-range.

We note that the third digits takes either 0 or 5 only on the mesh with unit 5.

Raises:

ValueError – any of first, second and third is out-of-range

Examples

The selection of MeshCoord depends on unit

>>> MeshCoord.from_latitude(36.103774791666666, 1)
MeshCoord(first=54, second=1, third=2)

Every fifth MeshCoord is taken, if mesh_unit is MeshUnit::Five

>>> MeshCoord.from_latitude(36.103774791666666, 5)
MeshCoord(first=54, second=1, third=0)

Total orderd

>>> MeshCoord(45, 7, 9) < MeshCoord(45, 7, 9)
False
>>> MeshCoord(45, 7, 9) < MeshCoord(45, 7, 10)
True
>>> MeshCoord(45, 7, 9) <= MeshCoord(45, 7, 9)
True

Increment/decrement (not inplace)

>>> MeshCoord(54, 1, 2).next_up(1)
MeshCoord(first=54, second=1, third=3)
>>> MeshCoord(54, 1, 2).next_down(1)
MeshCoord(first=54, second=1, third=1)
first: int#

takes values 0, …, 99.

second: int#

takes values 0, …, 7.

third: int#

takes values 0, …, 9.

classmethod from_latitude(degree: float, mesh_unit: _types.MeshUnitType) Self#

Makes the greatest MeshCoord obj less than the latitude v with unit.

Parameters:
  • degree – the latitude [deg] which satisfies 0.0 <= and <= 66.666…

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the MeshCoord obj

Raises:

ValueError – if v is out-of-range

Examples

>>> MeshCoord.from_latitude(36.103774791666666, 1)
MeshCoord(first=54, second=1, third=2)
>>> MeshCoord.from_latitude(36.103774791666666, 5)
MeshCoord(first=54, second=1, third=0)
classmethod from_longitude(degree: float, mesh_unit: _types.MeshUnitType) Self#

Makes the greatest MeshCoord obj less than the longitude v with unit.

Parameters:
  • degree – the longitude [deg] which satisfies 100.0 <= and <= 180.0

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the MeshCoord obj

Raises:

ValueError – when v is out-of-range

Examples

>>> MeshCoord.from_longitude(140.08785504166664, 1)
MeshCoord(first=40, second=0, third=7)
>>> MeshCoord.from_longitude(140.08785504166664, 5)
MeshCoord(first=40, second=0, third=5)
is_mesh_unit(mesh_unit: _types.MeshUnitType) bool#

Returns True when self is compatible to the unit.

Always returns True when unit is 1.

Parameters:

mesh_unit – the mesh unit, 1 or 5

Returns:

True when self is compatible to the unit.

Examples

>>> MeshCoord(1, 2, 3).is_mesh_unit(1)
True
>>> MeshCoord(1, 2, 3).is_mesh_unit(5)
False
to_latitude() float#

Returns the latitude that self converts into.

This does not check self represents latitude.

Returns:

the latitude [deg]

Examples

>>> latitude = 36.103774791666666
>>> MeshCoord.from_latitude(latitude, 1).to_latitude()
36.1
>>> MeshCoord.from_latitude(latitude, 5).to_latitude()
36.083333333333336
to_longitude() float#

Returns the longitude that self converts into.

This does not check self represents longitude.

Returns:

the longitude [deg]

Examples

>>> longitude = 140.08785504166664
>>> MeshCoord.from_longitude(longitude, 1).to_longitude()
140.0875
>>> MeshCoord.from_longitude(longitude, 5).to_longitude()
140.0625
next_up(mesh_unit: _types.MeshUnitType) MeshCoord#

Returns the smallest MeshCoord obj greater than self.

Parameters:

mesh_unit – the mesh unit, 1 or 5

Returns:

the up-next MeshCoord

Raises:
  • ValueError – when unit is 5 although self.third is either 0 or 5

  • OverflowError – when self is MeshCoord(first=99, second=7, third=9)

Examples

>>> MeshCoord(0, 0, 0).next_up(1)
MeshCoord(0, 0, 1)
>>> MeshCoord(0, 0, 0).next_up(5)
MeshCoord(0, 0, 5)
>>> MeshCoord(0, 0, 9).next_up(1)
MeshCoord(0, 1, 0)
>>> MeshCoord(0, 7, 9).next_up(1)
MeshCoord(1, 0, 0)
next_down(mesh_unit: _types.MeshUnitType) MeshCoord#

Returns the greatest MeshCoord obj less than self.

Parameters:

mesh_unit – the mesh unit, 1 or 5

Raises:
  • ValueError – when unit is 5 although self.third is either 0 or 5

  • OverflowError – when self is MeshCoord(first=0, second=0, third=0)

Returns:

the down-next MeshCoord

Examples

>>> MeshCoord(0, 0, 1).next_down(1)
MeshCoord(0, 0, 0)
>>> MeshCoord(0, 0, 5).next_down(5)
MeshCoord(0, 0, 5)
>>> MeshCoord(0, 1, 0).next_down(1)
MeshCoord(0, 0, 9)
>>> MeshCoord(1, 0, 0).next_down(1)
MeshCoord(0, 7, 9)
class jgdtrans.mesh.MeshNode(latitude: MeshCoord, longitude: MeshCoord)#

Bases: object

Represents mesh node, a pair of the MeshCoord objs.

We note that this supports non-negative latitude and longitude only, and longitude satisfies MeshCoord(0, 0, 0) <= and <= MeshCoord(80, 0, 0).

Raises:

ValueError – If longitude is out-of-range.

Examples

Construct from latitude and longitude

>>> MeshNode.from_pos(36.10377479, 140.087855041, mesh_unit=1)
MeshNode(latitude=MeshCoord(54, 1, 2), longitude=MeshCoord(40, 0, 7))

The result depends on the selection of the mesh unit

>>> MeshNode.from_pos(36.10377479, 140.087855041, mesh_unit=5)
MeshNode(latitude=MeshCoord(54, 1, 0), longitude=MeshCoord(40, 0, 5))

Construct from meshcode

>>> MeshNode.from_meshcode(54401027)
MeshNode(latitude=MeshCoord(54, 1, 2), longitude=MeshCoord(40, 0, 7))
latitude: MeshCoord#

The mesh coord of latitude.

longitude: MeshCoord#

The mesh coord of longitude.

This satisfies MeshCoord(0, 0, 0) <= and <= MeshCoord(80, 0, 0).

is_mesh_unit(mesh_unit: _types.MeshUnitType) bool#

Returns True when self is compatible to the unit.

Always returns True when unit is 1.

Parameters:

mesh_unit – the mesh unit, 1 or 5

Returns:

True when self is compatible to the unit.

Examples

>>> MeshNode.from_meshcode(54401027).is_mesh_unit(1)
True
>>> MeshNode.from_meshcode(54401027).is_mesh_unit(5)
False
classmethod from_meshcode(meshcode: int) MeshNode#

Makes a MeshNode obj represented by meshcode code.

This method is an inverse of MeshNode.to_meshcode().

Parameters:

meshcode – the meshcode

Returns:

the MeshNode

Raises:

ValueError – when code is invalid.

Examples

>>> MeshNode.from_meshcode(54401027)
MeshNode(latitude=MeshCoord(54, 1, 2), longitude=MeshCoord(40, 0, 7))
classmethod from_point(point: _point.Point, mesh_unit: _types.MeshUnitType) Self#

Makes the nearest north-west MeshNode of point.

We note that the result is independend of the Point.altitude.

Parameters:
  • point – the point

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the MeshNode obj

Raises:

ValueErrorpoint.latitude and/or point.longitude is out-of-range

Examples

>>> point = Point(36.103774791666666, 140.08785504166664, 10.0)
>>> MeshNode.from_point(point, 1)
MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7))
>>> MeshNode.from_point(point, 5)
MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 0, 5))
classmethod from_pos(latitude: float, longitude: float, mesh_unit: _types.MeshUnitType) Self#

Makes the nearest north-west MeshNode of point.

Parameters:
  • latitude – the latitude [deg] of the point which satisfies 0.0 <= and <= 66.6…

  • longitude – the longitude [deg] of the point which satisfies 100.0 <= and <= 180.0

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the MeshNode obj

Raises:

ValueErrorlatitude or longitude is out-of-range

Examples

>>> lat, lng = 36.103774791666666, 140.08785504166664
>>> MeshNode.from_pos(lat, lng, 1)
MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7))
>>> MeshNode.from_pos(lat, lng, 5)
MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 0, 5))
to_meshcode() int#

Returns a meshcode represents self.

The result is up to 8 digits.

This method is an inverse of MeshNode.from_meshcode().

Returns:

the meshcode

Examples

>>> node = MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7))
>>> node.to_meshcode()
54401027
to_point() Point#

Returns a Point (latitude and longitude) where self locates.

The resulting altitude is 0.0.

Returns:

a Point of the mesh node

Examples

>>> node = MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7))
>>> node.to_point()
Point(latitude=36.1, longitude=140.0875, altitude=0.0)
to_pos() tuple[float, float]#

Return the latitude and the longitude where self locates.

Returns:

the latitude [deg] and the longitude [deg] of the mesh node

Examples

>>> node = MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7))
>>> node.to_pos()
(36.1, 140.0875)
class jgdtrans.mesh.MeshCell(south_west: MeshNode, south_east: MeshNode, north_west: MeshNode, north_east: MeshNode, mesh_unit: _types.MeshUnitType)#

Bases: object

Represents the unit mesh cell (mesh cell or cell shortly).

This is a quadruplet of the mesh nodes (and unit), and has no other MeshNode inside self in the unit.

The cell must be a unit cell in the unit, otherwise, this constructor throws ValueError. Also, MeshCoord.third of the nodes must be 1 or 5 when unit is 5.

The cell is, roughly, a square with unit [km] length edges.

Raises:

ValueError – when unit is inconsistent with nodes, or the nodes does not construct a unit mesh cell with unit.

Examples

Construct from latitude and longitude, and altitude ignores (the result depends on the selection of the mesh unit)

>>> cell = MeshCell.from_pos(36.10377479, 140.087855041, mesh_unit=1)
>>> cell
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7)),
    south_east=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 8)),
    north_west=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 7)),
    north_east=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 8)),
    unit=1,
)

Construct from node

>>> node = MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7))
>>> cell == MeshCell.from_node(node, mesh_unit=1)
True

Construct from meshcode

>>> cell == MeshCell.from_meshcode(54401027, mesh_unit=1)
True

Find the position within cell, from 0.0 to 1.0 (again, the result depends on the selection of the mesh unit)

>>> cell.position(36.10377479, 140.087855041)
(0.4529748000001632, 0.028403280000475206)
south_west: MeshNode#

The south-west node of the cell.

south_east: MeshNode#

The south-east node of the cell.

north_west: MeshNode#

The north-west node of the cell.

north_east: MeshNode#

The north-east node of the cell.

mesh_unit: _types.MeshUnitType#

The mesh unit, 1 or 5.

classmethod from_meshcode(meshcode: int, mesh_unit: _types.MeshUnitType) Self#

Makes a MeshCell with the south-east MeshNode which represented by meshcode code.

Parameters:
  • meshcode – the meshcode

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the meth cell

Raises:

ValueError – when code is invalid, or unit is inconsistent meshcode

Examples

>>> meshcode = 54401027
>>> MeshCell.from_meshcode(meshcode, mesh_unit=1)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7)),
    south_east=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 8)),
    north_west=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 7)),
    north_east=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 8)),
    unit=1,
)
>>> meshcode = 54401005
>>> MeshCell.from_meshcode(meshcode, mesh_unit=5)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 0, 5)),
    south_east=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 1, 0)),
    north_west=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 0, 5)),
    north_east=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 1, 0)),
    unit=5,
)
classmethod from_point(point: _point.Point, mesh_unit: _types.MeshUnitType) Self#

Makes a MeshCell which contains the Point.

We note that the result does not depend on Point.altitude.

Parameters:
  • point – the point

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the mesh cell

Raises:
  • ValueErrorp.latitude and/or p.longitude is out-of-range, or one of nodes constructing the cell is out-of-range.

  • OverflowError – the south-west node points (90, x) for latitude and longitude.

Examples

>>> point = Point(36.10377479, 140.087855041, 10.0)
>>> MeshCell.from_point(point, mesh_unit=1)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7)),
    south_east=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 8)),
    north_west=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 7)),
    north_east=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 8)),
    unit=1,
)
>>> MeshCell.from_pos(point, mesh_unit=5)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 0, 5)),
    south_east=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 1, 0)),
    north_west=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 0, 5)),
    north_east=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 1, 0)),
    unit=5,
)
classmethod from_pos(latitude: float, longitude: float, mesh_unit: _types.MeshUnitType) Self#

Makes a MeshCell which contains the point.

Parameters:
  • latitude – the latitude [deg] of the point which satisries 0 <= and <= 66.666…

  • longitude – the longitude [deg] of the point which satisries 100 <= and <= 180

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the mesh cell

Raises:
  • ValueError – when latitude or longitude is out-of-range, or one of nodes constructing the cell is out-of-range.

  • OverflowError – the south-west node points (90, x) for latitude and longitude.

Examples

>>> lat, lng = 36.10377479, 140.087855041
>>> MeshCell.from_pos(lat, lng, mesh_unit=1)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7)),
    south_east=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 8)),
    north_west=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 7)),
    north_east=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 8)),
    unit=1,
)
>>> MeshCell.from_pos(lat, lng, mesh_unit=5)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 0, 5)),
    south_east=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 1, 0)),
    north_west=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 0, 5)),
    north_east=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 1, 0)),
    unit=5,
)
classmethod from_node(node: MeshNode, mesh_unit: _types.MeshUnitType) Self#

Return the unit cell which has node as a south-east node.

Parameters:
  • node – the south-west mesh node of the resulting cell

  • mesh_unit – the mesh unit, 1 or 5

Returns:

the mesh cell

Raises:
  • ValueError – when unit is inconsistent node, or one of nodes constructing the cell is out-of-range

  • OverflowError – the south-west node points (90, x) for latitude and longitude.

Examples

>>> node = MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7))
>>> MeshCell.from_node(node, mesh_unit=1)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 7)),
    south_east=MeshNode(MeshCoord(54, 1, 2), MeshCoord(40, 0, 8)),
    north_west=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 7)),
    north_east=MeshNode(MeshCoord(54, 1, 3), MeshCoord(40, 0, 8)),
    unit=1,
)
>>> node = MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 0, 5))
>>> MeshCell.from_node(node, mesh_unit=5)
MeshCell(
    south_west=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 0, 5)),
    south_east=MeshNode(MeshCoord(54, 1, 0), MeshCoord(40, 1, 0)),
    north_west=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 0, 5)),
    north_east=MeshNode(MeshCoord(54, 1, 5), MeshCoord(40, 1, 0)),
    unit=5,
)
position(latitude: float, longitude: float) tuple[float, float]#

Return the position in the cell.

The result’s components takes values from 0.0 to 1.0 (inclusive), when latitude and/or longitude is inside self.

We note that the result is a (latitude, longitude) pair, not a (right-handed) (x, y) pair.

Sample latitude and longitude

>>> lat, lng = 36.10377479, 140.087855041
>>> cell = MeshCell.from_pos(lat, lng, mesh_unit=1)

The south-west of the cell is (0, 0), origin

>>> cell.position(*cell.south_west.to_pos())
(0, 0)

The south-east is (0, 1)

>>> cell.position(*cell.south_east.to_pos())
(0.0, 0.9999999999990905)

The north-west is (1, 0)

>>> cell.position(*cell.north_west.to_pos())
(0.9999999999999432, 0.0)

The north-east is (1, 1)

>>> cell.position(*cell.north_east.to_pos())
(0.9999999999999432, 0.9999999999990905)
Parameters:
  • latitude – the latitude of the point

  • longitude – the longitude of the point

Returns:

the position, a pair of the latitude and the longitude, in the cell

Examples

The reuslt depends on unit

>>> lat, lng = 36.10377479, 140.087855041
>>> cell = MeshCell.from_pos(lat, lng, mesh_unit=1)
>>> cell.position(lat, lng)
(0.4529748000001632, 0.028403280000475206)
>>> cell = MeshCell.from_pos(lat, lng, mesh_unit=5)
>>> cell.position(lat, lng)
(0.4905949600000099, 0.405680656000186)