En primer lugar, recomiendo la función os.path.splitext()
del módulo os.path
para obtener file_extension
y output_file
de una manera más elegante:
from os.path import splitext
input_file = '.../input_file.csv'
output_file, file_extension = splitext(input_file)
print(output_file, file_extension)
Supongamos que hay un archivo .csv llamado 'points.csv', ver imagen a continuación:
En esta etapa se creará el GeoDataFrame
:
import pandas as pd
import geopandas as gpd
from os.path import splitext
absolute_path_to_file = '.../input_file.csv'
file_name, file_extension = splitext(absolute_path_to_file)
if file_extension == ".txt" or ".text" or ".csv":
df = pd.read_table(absolute_path_to_file, sep=',')
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['x'], df['y'], df['z']), crs='EPSG:25832')
print(gdf)
que resulta en (ten en cuenta que la representación de la geometría del punto es POINT Z
):
id x y z geometry
0 1 513072.05 5402445.83 250.0 POINT Z (513072.050 5402445.830 250.000)
1 2 513212.88 5402852.80 245.0 POINT Z (513212.880 5402852.800 245.000)
2 3 513733.74 5403658.99 239.0 POINT Z (513733.740 5403658.990 239.000)
Ahora es posible obtener latitud, longitud y altitud usando:
-
la función to_crs()
de GeoPandas como sugirió @user2856:
import pandas as pd
import geopandas as gpd
from os.path import splitext
from pyproj import CRS
absolute_path_to_file = '.../input_file.csv'
file_name, file_extension = splitext(absolute_path_to_file)
if file_extension == ".txt" or ".text" or ".csv":
df = pd.read_table(absolute_path_to_file, sep=',')
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['x'], df['y'], df['z']), crs='EPSG:25832')
crs = CRS.from_string('EPSG:4326')
gdf = gdf.to_crs(crs=crs)
print(gdf)
id x y z geometry
0 1 513072.05 5402445.83 250.0 POINT Z (9.17792 48.77488 250.00000)
1 2 513212.88 5402852.80 245.0 POINT Z (9.17985 48.77854 245.00000)
2 3 513733.74 5403658.99 239.0 POINT Z (9.18697 48.78578 239.00000)
-
Pandas y una función personalizada con la clase Transformer
de pyproj:
import pandas as pd
from os.path import splitext
from pyproj import CRS, Transformer
def xyz_to_latlonalt(x, y, z, crs_in, crs_out):
crs_from = CRS.from_user_input(crs_in)
crs_to = CRS.from_user_input(crs_out)
proj = Transformer.from_crs(crs_from, crs_to, always_xy=True)
coordinates = proj.transform(x, y, z)
return coordinates
absolute_path_to_file = '.../input_file.csv'
file_name, file_extension = splitext(absolute_path_to_file)
if file_extension == ".txt" or ".text" or ".csv":
df = pd.read_table(absolute_path_to_file, sep=',')
df['lat'] = df.apply(lambda row: xyz_to_latlonalt(row['x'], row['y'], row['z'], 25832, 4326)[0], axis=1)
df['lon'] = df.apply(lambda row: xyz_to_latlonalt(row['x'], row['y'], row['z'], 25832, 4326)[1], axis=1)
df['alt'] = df.apply(lambda row: xyz_to_latlonalt(row['x'], row['y'], row['z'], 25832, 4326)[2], axis=1)
df = df.drop(['x', 'y', 'z'], axis=1)
print(df)
id lat lon alt
0 1 9.177920 48.774878 250.0
1 2 9.179850 48.778536 245.0
2 3 9.186966 48.785778 239.0