A memorandum regarding the implementation of the coordinate conversion process (WGS84-> UTM) of WKT format LineString in Python.
Python: v3.8.5 Library used: Shapely = 1.7.0, Pyproj = 3.0.0.post1
For the coordinate string of LineString read by Shapely, batch coordinate transformation with Transformer.itransformer of Pyproj with the specified EPSG code
transform_linestr.py
from pyproj import Transformer
from shapely import wkt
from shapely.geometry import LineString
WGS84_EPSG = 4326
UTM_EPSG = 6691
def main():
#WKT format LineString load
linestr_wkt = "LINESTRING (138.385554841666 34.9748902437125, 138.385886903329 34.9752925491883, 138.38621257919 34.9757140120678, 138.386397767425 34.97612270334488)"
linestr = wkt.loads(linestr_wkt)
#Coordinate conversion from WGS84 to UTM
trans_linestr = transform_linestr(linestr, WGS84_EPSG, UTM_EPSG)
#Coordinate reconversion from UTM to WGS84(The value may change slightly when the accuracy of the original coordinates is high.)
re_trans_linestr = transform_linestr(trans_linestr, UTM_EPSG, WGS84_EPSG)
print("Load WKT: ", linestr)
print("WGS84->UTM:", trans_linestr)
print("UTM->WGS84:", re_trans_linestr)
def transform_linestr(linestr: LineString, from_epsg: int, to_epsg: int) -> LineString:
"""LineString coordinate conversion process in the specified EPSG code
Args:
linestr (LineString):Coordinate conversion source LineString
from_epsg (int):Conversion source EPSG code
to_epsg (int):Conversion destination EPSG code
Returns:
LineString:LineString after coordinate conversion
"""
transformer = Transformer.from_crs(from_epsg, to_epsg)
trans_coords = transformer.itransform(linestr.coords, switch=True)
return LineString(trans_coords)
if __name__ == "__main__":
main()
Execution output result
Load WKT: LINESTRING (138.385554841666 34.9748902437125, 138.385886903329 34.9752925491883, 138.38621257919 34.9757140120678, 138.386397767425 34.97612270334488)
WGS84->UTM: LINESTRING (3873381.187880124 261326.0666616608, 3873425.024292707 261357.5560269007, 3873471.001297946 261388.5176213919, 3873515.897231507 261406.6137873351)
UTM->WGS84: LINESTRING (138.385554841666 34.9748902437125, 138.385886903329 34.9752925491883, 138.38621257919 34.9757140120678, 138.386397767425 34.97612270334487)
Shapely + Pyproj made it very easy to implement the coordinate sequence conversion process. Pyproj convenient.