Projection Classes
Before performing analyses on latitude and longitude coordinates, it is most often necessary to first project them into a linear coordinate system. There are many techniques to do this, and each has its own strengths and weaknesses. The one you should use depends on your analysis and the location that your analysis is taking place. GeoJikuu currently provides tools for converting to three different projection classes: CartesianProjector, MGA2020Projector, and MGA1994Projector.
Importing a Projection Class
Each projection class is located in GeoJikuu's preprocessing.projection module:
from geojikuu.preprocessing.projection import CartesianProjector
from geojikuu.preprocessing.projection import MGA2020Projector
from geojikuu.preprocessing.projection import MGA1994Projector
Creating a Projection Object
To create a projection object, simply call the constructor of the projection class you would like to use by passing in the coordinate system that you are projecting from. If your coordinates are in latitude and longitude, then you are most likely using the WGS84 format:
cartesian_projector = CartesianProjector("wgs84")
mga_2020_projector = MGA2020Projector("wgs84")
mga_1994_projector = MGA1994Projector("wgs84")
Project Coordinates
Coordinates are projected using the project() function which can be called from any object created from one of the projection classes. The project() function takes a DataFrame column or list of coordinates to project, and outputs a dictionary that contains a DataFrame column with the projected coordinates as well as the projection system's unit conversion value. The unit conversion value represents how many kilometres are represented by one unit of projection distance and can be used to convert projected distances to their physical approximations.
If you are working with a DataFrame that contains latitude and longitude as separate columns, then you can use Python's list() and zip() functions to convert them to a list of tuples before passing them into the project() function:
data = {
"lat": [35.5020581, 34.6775704, 35.0977501],
"lon": [138.4504777, 135.403636, 135.3892183]
}
df = pd.DataFrame.from_dict(data)
results = cartesian_projector.project(list(zip(df["lat"], df["lon"])))
df["cartesian_coordinates"] = results["cartesian_coordinates"]
df.head()
lat | lon | cartesian_coordinates | |
---|---|---|---|
0 | 35.502058 | 138.450478 | (-0.6092543754271601, 0.5399622386951436, 0.5807321988715536) |
1 | 34.677570 | 135.403636 | (-0.5855832476960234, 0.577390223190525, 0.5689576347864341) |
2 | 35.097750 | 135.389218 | (-0.5824518708327167, 0.5745917893800737, 0.5749731243636291) |
Obtain the Unit Conversion
The unit conversion is included in the dictionary outputted by the project() function:
unit_conversion = results["unit_conversion"]
unit_conversion
Output: 6371.557545552401
Inverse Projection
Coordinates can be projected back to their original system (i.e., lat and lon) by using the object's inverse_project() function. As with project(), a DataFrame column or list of coordinates can be used as input:
df["inverse_projection"] = cartesian_projector.inverse_project(results["cartesian_coordinates"])
df.head()
lat | lon | cartesian_coordinates | inverse_projection | |
---|---|---|---|---|
0 | 35.502058 | 138.450478 | (-0.6092543754271601, 0.5399622386951436, 0.5807321988715536) | (35.5020581, 138.45047770000002) |
1 | 34.677570 | 135.403636 | (-0.5855832476960234, 0.577390223190525, 0.5689576347864341) | (34.6775704, 135.403636) |
2 | 35.097750 | 135.389218 | (-0.5824518708327167, 0.5745917893800737, 0.5749731243636291) | (35.0977501, 135.3892183) |