@@ -18,12 +18,50 @@ tags: ["big data", "movement data in gis", "movingpandas", "spatio-temporal data
1818
1919
2020
21- <p >written together with my fellow co-authors and EMERALDS project team member Argyrios Kyrgiazos.</p >
21+ <p >written together with my fellow& nbsp ; co-authors and EMERALDS project team member Argyrios Kyrgiazos.</p >
2222
2323
2424
25- <p >For the technically inclined, the highlight are the presented < strong >UDFs in Snowflake</ strong > to process and transform the trajectory data.</ p >
25+ <figure class = " wp-block-image size-large " >< a href = " https://carto.com/blog/urban-mobility-insights-with-movingpandas-carto-in-snowflake " >< img alt = " " class = " wp-image-9334 " height = " 537 " src = " https://anitagraser.com/wp-content/uploads/2024/12/image.png?w=1024 " width = " 1024 " /></ a ></ figure >
2626
2727
2828
29- <figure class =" wp-block-image size-large " ><a href =" https://carto.com/blog/urban-mobility-insights-with-movingpandas-carto-in-snowflake " ><img alt =" " class =" wp-image-9334 " height =" 537 " src =" https://anitagraser.com/wp-content/uploads/2024/12/image.png?w=1024 " width =" 1024 " /></a ></figure >
29+ <p >For the technically inclined, the highlight are the presented <strong >UDFs in Snowflake</strong > to process and transform the trajectory data. For example, here’ ; s a TemporalSplitter UDF: </p >
30+
31+
32+ <div class =" wp-block-syntaxhighlighter-code " ><pre class =" brush: sql; title: ; notranslate " >
33+ CREATE OR REPLACE FUNCTION CARTO_DATABASE.CARTO.TemporalSplitter(geom ARRAY, t ARRAY, mode STRING)
34+ RETURNS ARRAY
35+ LANGUAGE PYTHON
36+ RUNTIME_VERSION = 3.11
37+ PACKAGES = (' ; numpy' ; ,' ; pandas' ; , ' ; geopandas' ; ,' ; movingpandas' ; , ' ; shapely' ; )
38+ HANDLER = ' ; udf' ;
39+ AS $$
40+ import numpy as np
41+ import pandas as pd
42+ import geopandas as gpd
43+ import movingpandas as mpd
44+ import shapely
45+ from shapely.geometry import shape, mapping, Point, Polygon
46+ from shapely.validation import make_valid
47+ from datetime import datetime, timedelta
48+
49+ def udf(geom, t, mode):
50+ valid_df = pd.DataFrame(geom, columns=[ ;' ; geometry' ; ] )
51+ valid_df[ ;' ; t' ; ] = pd.to_datetime(t)
52+ valid_df[ ;' ; geometry' ; ] = valid_df[ ;' ; geometry' ; ] .apply(lambda x: shapely .wkt.loads(x))
53+ gdf = gpd.GeoDataFrame(valid_df, geometry=' ; geometry' ; , crs=' ; epsg:4326' ; )
54+ gdf = gdf.set_index(' ; t' ; )
55+ traj = mpd.Trajectory(gdf, 1)
56+ traj_sm = mpd.TemporalSplitter(traj).split(mode=mode)
57+ if len(traj_sm.trajectories)> ; 0:
58+ res = traj_sm.to_point_gdf()
59+ res[ ;' ; geometry' ; ] = res[ ;' ; geometry' ; ] .apply(lambda x: shapely.wkt.dumps(x))
60+ return res.reset_index().values
61+ else:
62+ return [ ; ]
63+ $$ ;
64+ </pre></div>
65+
66+
67+ <p>You can find the full code here: <a href="https://github.com/anitagraser/carto-research-public/tree/master/movingpandas_carto_in_snowflake">https://github.com/anitagraser/carto-research-public/tree/master/movingpandas_carto_in_snowflake</a> </p>
0 commit comments