%3CLINGO-SUB%20id%3D%22lingo-sub-1859278%22%20slang%3D%22en-US%22%3EAnalyzing%20GPS%20trajectories%20at%20scale%20with%20Postgres%2C%20MobilityDB%2C%20%26amp%3B%20Citus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1859278%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EGPS%20has%20become%20part%20of%20our%20daily%20life.%20GPS%20is%20in%20cars%20for%20navigation%2C%20in%20smartphones%20helping%20us%20to%20find%20places%2C%20and%20more%20recently%20GPS%20has%20been%20helping%20us%20to%20avoid%20getting%20infected%20by%20COVID-19.%20Managing%20and%20analyzing%20mobility%20tracks%20is%20the%20core%20of%20my%20work.%20My%20group%20in%20Universit%C3%A9%20libre%20de%20Bruxelles%20specializes%20in%20mobility%20data%20management.%20We%20build%20an%20open%20source%20database%20system%20for%20spatiotemporal%20trajectories%2C%20called%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMobilityDB%2FMobilityDB%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EMobilityDB%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%20MobilityDB%20adds%20support%20for%20temporal%20and%20spatio-temporal%20objects%20to%20the%20Postgres%20database%20and%20its%20spatial%20extension%2C%20PostGIS.%20If%20you%E2%80%99re%20not%20yet%20familiar%20with%20spatiotemporal%20trajectories%2C%20not%20to%20worry%2C%20we%E2%80%99ll%20walk%20through%20some%20movement%20trajectories%20for%20a%20public%20transport%20bus%20in%20just%20a%20bit.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOne%20of%20my%20team%E2%80%99s%20projects%20is%20to%20develop%20a%20distributed%20version%20of%20MobilityDB.%20This%20is%20where%20we%20came%20in%20touch%20with%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fcitus%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20extension%20to%20Postgres%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20and%20the%20Citus%20engineering%20team.%20This%20post%20presents%20issues%20and%20solutions%20for%20distributed%20query%20processing%20of%20movement%20trajectory%20data.%20GPS%20is%20the%20most%20common%20source%20of%20trajectory%20data%2C%20but%20the%20ideas%20in%20this%20post%20also%20apply%20to%20movement%20trajectories%20collected%20by%20other%20location%20tracking%20sensors%2C%20such%20as%20radar%20systems%20for%20aircraft%2C%20and%20AIS%20systems%20for%20sea%20vessels.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Illustration-of-bus-in-motion-on-a-map-MobilityDB-and-Citus-1920x1080.jpg%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232168iED50F34C6816AD48%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Illustration-of-bus-in-motion-on-a-map-MobilityDB-and-Citus-1920x1080.jpg%22%20alt%3D%22Illustration-of-bus-in-motion-on-a-map-MobilityDB-and-Citus-1920x1080.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20a%20start%2C%20let's%20explore%20the%20main%20concepts%20of%20trajectory%20data%20management%2C%20so%20you%20can%20see%20how%20to%20analyze%20geospatial%20movement%20trajectories.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20following%20animated%20gif%20shows%20a%20geospatial%20trajectory%20of%20a%20public%20transport%20bus%3CSUP%3E%3CA%20id%3D%22fnref-1%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn-1%22%20target%3D%22_self%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20name%3D%22fnref-1%22%3E1%3C%2FA%3E%3C%2FSUP%3E%20that%20goes%20nearby%20an%20advertising%20billboard.%20What%20if%20you%20wanted%20to%20assess%20the%20visibility%20of%20the%20billboard%20to%20the%20passengers%20in%20the%20bus%3F%20If%20you%20can%20do%20this%20for%20all%20billboards%20and%20vehicles%2C%20then%20you%20would%20be%20able%20to%20extract%20interesting%20insights%20for%20advertising%20agencies%20to%20price%20the%20billboards%2C%20and%20for%20advertisers%20who%20are%20looking%20to%20optimize%20their%20campaigns.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22gif-of-map-with-moving-bus-passing-by-advertising-billboards-1.gif%22%20style%3D%22width%3A%20395px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232169i8FE1C14D04DCCCC9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22gif-of-map-with-moving-bus-passing-by-advertising-billboards-1.gif%22%20alt%3D%22Throughout%20this%20post%2C%20I%E2%80%99ll%20use%20maps%20to%20visualize%20bus%20trajectories%20and%20advertising%20billboards%20in%20Brussels%2C%20so%20you%20can%20learn%20how%20to%20query%20where%20(and%20for%20how%20long)%20the%20advertising%20billboards%20are%20visible%20to%20the%20bus%20passengers.%20The%20background%20maps%20are%20courtesy%20of%20OpenStreetMap.%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EThroughout%20this%20post%2C%20I%E2%80%99ll%20use%20maps%20to%20visualize%20bus%20trajectories%20and%20advertising%20billboards%20in%20Brussels%2C%20so%20you%20can%20learn%20how%20to%20query%20where%20(and%20for%20how%20long)%20the%20advertising%20billboards%20are%20visible%20to%20the%20bus%20passengers.%20The%20background%20maps%20are%20courtesy%20of%20OpenStreetMap.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20the%20animated%20gif%20above%2C%20we%20simply%20assume%20that%20if%20the%20bus%20is%20within%2030%20meters%20to%20the%20billboard%2C%20then%20it%20is%20visible%20to%20its%20passengers.%20This%20%E2%80%9Cvisibility%E2%80%9D%20is%20indicated%20in%20the%20animation%20by%20the%20yellow%20flash%20around%20the%20billboard%20when%20the%20bus%20is%20within%2030%20meters%20of%20the%20billboard.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1211173498%22%20id%3D%22toc-hId--1211173498%22%3EHow%20to%20measure%20the%20billboard%E2%80%99s%20visibility%20to%20a%20moving%20bus%20using%20a%20database%20query%3F%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELet%E2%80%99s%20prepare%20a%20toy%20PostGIS%20database%20that%20minimally%20represents%20the%20example%20in%20the%20previous%20animated%20gif%E2%80%94and%20then%20gradually%20develop%20an%20SQL%20query%20to%20assess%20the%20billboard%20visibility%20to%20the%20passengers%20in%20a%20moving%20bus.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20you%20are%20not%20familiar%20with%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EPostGIS%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%2C%20it%20is%20probably%20the%20most%20popular%20extension%20to%20Postgres%20and%20is%20used%20for%20storing%20and%20querying%20spatial%20data.%20For%20the%20sake%20of%20this%20post%2C%20all%20you%20need%20to%20know%20is%20that%20PostGIS%20extends%20Postgres%20with%20data%20types%20for%20geometry%20point%2C%20line%2C%20and%20polygon.%20PostGIS%20also%20defines%20functions%20to%20measure%20the%20distance%20between%20geographic%20features%20and%20to%20test%20topological%20relationships%20such%20as%20intersection.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20the%20SQL%20code%20block%20below%2C%20first%20you%20create%20the%20PostGIS%20extension.%20And%20then%20you%20will%20create%20two%20tables%3A%3CCODE%3EgpsPoint%3C%2FCODE%3Eand%3CCODE%3Ebillboard%3C%2FCODE%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20EXTENSION%20PostGIS%3B%0ACREATE%20TABLE%20gpsPoint%20(tripID%20int%2C%20pointID%20int%2C%20t%20timestamp%2C%20geom%20geometry(Point%2C%203812))%3B%0ACREATE%20TABLE%20billboard(billboardID%20int%2C%20geom%20geometry(Point%2C%203812))%3B%0A%0AINSERT%20INTO%20gpsPoint%20Values%0A(1%2C%201%2C%20'2020-04-21%2008%3A37%3A27'%2C%20'SRID%3D3812%3BPOINT(651096.993815166%20667028.114604598)')%2C%0A(1%2C%202%2C%20'2020-04-21%2008%3A37%3A39'%2C%20'SRID%3D3812%3BPOINT(651080.424535144%20667123.352304597)')%2C%20%20%20%20%0A(1%2C%203%2C%20'2020-04-21%2008%3A38%3A06'%2C%20'SRID%3D3812%3BPOINT(651067.607438095%20667173.570340437)')%2C%0A(1%2C%204%2C%20'2020-04-21%2008%3A38%3A31'%2C%20'SRID%3D3812%3BPOINT(651052.741845233%20667213.026797244)')%2C%20%20%20%20%0A(1%2C%205%2C%20'2020-04-21%2008%3A38%3A49'%2C%20'SRID%3D3812%3BPOINT(651029.676773636%20667255.556944161)')%2C%20%20%20%20%0A(1%2C%206%2C%20'2020-04-21%2008%3A39%3A08'%2C%20'SRID%3D3812%3BPOINT(651018.401101238%20667271.441380755)')%2C%20%20%20%20%0A(2%2C%201%2C%20'2020-04-21%2008%3A39%3A29'%2C%20'SRID%3D3812%3BPOINT(651262.17004873%20%20667119.331513367)')%2C%20%20%20%20%0A(2%2C%202%2C%20'2020-04-21%2008%3A38%3A36'%2C%20'SRID%3D3812%3BPOINT(651201.431447782%20667089.682115196)')%2C%20%20%20%20%0A(2%2C%203%2C%20'2020-04-21%2008%3A38%3A43'%2C%20'SRID%3D3812%3BPOINT(651186.853162155%20667091.138189286)')%2C%20%20%20%20%0A(2%2C%204%2C%20'2020-04-21%2008%3A38%3A49'%2C%20'SRID%3D3812%3BPOINT(651181.995412783%20667077.531372716)')%2C%20%20%20%20%0A(2%2C%205%2C%20'2020-04-21%2008%3A38%3A56'%2C%20'SRID%3D3812%3BPOINT(651101.820139904%20667041.076539663)')%3B%20%20%20%0A%0AINSERT%20INTO%20billboard%20Values%0A(1%2C%20'SRID%3D3812%3BPOINT(651066.289442793%20667213.589577551)')%2C%0A(2%2C%20'SRID%3D3812%3BPOINT(651110.505092035%20667166.698041233)')%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20database%20is%20visualized%20in%20the%20map%20below.%26nbsp%3B%20You%20can%20see%20that%20the%20gpsPoint%20table%20has%20points%20of%20two%20bus%20trips%2C%20%3C%2FSPAN%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CSPAN%3Etrip%201%20in%20blue%3C%2FSPAN%3E%3C%2FFONT%3E%3CSPAN%3E%20and%20%3C%2FSPAN%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSPAN%3Etrip%202%20in%20red%3C%2FSPAN%3E%3C%2FFONT%3E%3CSPAN%3E.%20In%20the%20table%2C%20each%20point%20has%20a%20timestamp.%20The%20two%20billboards%20are%20the%20%3C%2FSPAN%3E%3CFONT%20color%3D%22%23808080%22%3E%3CSPAN%3Egray%3C%2FSPAN%3E%3C%2FFONT%3E%3CSPAN%3E%3CFONT%20color%3D%22%23808080%22%3E%20diamonds%3C%2FFONT%3E%20in%20the%20map.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22map-with-gpspoints-for-two-bus-trips-2.jpg%22%20style%3D%22width%3A%20681px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232170i53890E46A8860A12%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22map-with-gpspoints-for-two-bus-trips-2.jpg%22%20alt%3D%22map-with-gpspoints-for-two-bus-trips-2.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYour%20next%20step%20is%20to%20find%20the%20locations%20where%20a%20bus%20is%20within%2030%20meters%20from%20a%20billboard%E2%80%94and%20also%20the%20durations%2C%20i.e.%2C%20how%20long%20the%20moving%20bus%20is%20within%2030%20meters%20of%20the%20billboard.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20tripID%2C%20pointID%2C%20billboardID%0AFROM%20gpsPoint%20a%2C%20billboard%20b%0AWHERE%20st_dwithin(a.geom%2C%20b.geom%2C%2030)%3B%0A%0A--1%20%20%20%204%20%20%20%201%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20PostGIS%20query%20above%20does%20not%20solve%20the%20problem.%20Yes%2C%20the%20condition%20in%20the%3CCODE%3EWHERE%3C%2FCODE%3Eclause%20finds%20the%20GPS%20points%20that%20are%20within%2030%20meters%20from%20a%20billboard.%20But%20the%20PostGIS%20query%20does%20not%20tell%20the%20duration%20of%20this%20event.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFurthermore%2C%20imagine%20that%20point%204%20in%20%3CFONT%20color%3D%22%230000FF%22%3Etrip%201%20(the%20blue%20trip)%3C%2FFONT%3E%20was%20not%20given.%20This%20query%20would%20have%20then%20returned%20null.%20The%20problem%20with%20this%20query%20is%20that%20it%20does%20not%20deal%20with%20the%20%3C%2FSPAN%3E%3CI%3E%3CSPAN%3Econtinuity%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%20of%20the%20bus%20trip%2C%20i.e.%20the%20query%20does%20not%20deal%20with%20the%20%3C%2FSPAN%3E%3CI%3E%3CSPAN%3Emovement%20trajectory%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%20of%20the%20bus.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%20need%20to%20reconstruct%20a%20continuous%20movement%20trajectory%20out%20of%20the%20given%20GPS%20points.%20Below%20is%20another%20PostGIS%20query%20that%20would%20find%20both%20the%20locations%20of%20the%20billboard%E2%80%99s%20visibility%20to%20the%20passengers%20in%20the%20bus%2C%20and%20also%20the%20duration%20of%20how%20long%20the%20billboard%20was%20visible%20to%20the%20bus%20passengers.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E1%20%20%20WITH%20pointPair%20AS(%0A2%20%20%20%20%20SELECT%20tripID%2C%20pointID%20AS%20p1%2C%20t%20AS%20t1%2C%20geom%20AS%20geom1%2C%0A3%20%20%20%20%20%20%20lead(pointID%2C%201)%20OVER%20(PARTITION%20BY%20tripID%20ORDER%20BY%20pointID)%20p2%2C%0A4%20%20%20%20%20%20%20lead(t%2C%201)%20OVER%20(PARTITION%20BY%20tripID%20ORDER%20BY%20pointID)%20t2%2C%0A5%20%20%20%20%20%20%20lead(geom%2C%201)%20OVER%20(PARTITION%20BY%20tripID%20ORDER%20BY%20pointID)%20geom2%20%20%20%20%0A6%20%20%20%20%20FROM%20gpsPoint%0A7%20%20%20)%2C%20segment%20AS(%0A8%20%20%20%20%20SELECT%20tripID%2C%20p1%2C%20p2%2C%20t1%2C%20t2%2C%0A9%20%20%20%20%20%20%20st_makeline(geom1%2C%20geom2)%20geom%0A10%20%20%20%20FROM%20pointPair%0A11%20%20%20%20WHERE%20p2%20IS%20NOT%20NULL%20%20%20%20%0A12%20%20)%2C%20approach%20AS(%0A13%20%20%20%20SELECT%20tripID%2C%20p1%2C%20p2%2C%20t1%2C%20t2%2C%20a.geom%2C%0A14%20%20%20%20%20%20st_intersection(a.geom%2C%20st_exteriorRing(st_buffer(b.geom%2C%2030)))%20visibilityTogglePoint%0A15%20%20%20%20FROM%20segment%20a%2C%20billboard%20b%0A16%20%20%20%20WHERE%20st_dwithin(a.geom%2C%20b.geom%2C%2030)%0A17%20%20)%0A18%20%20SELECT%20tripID%2C%20p1%2C%20p2%2C%20t1%2C%20t2%2C%20geom%2C%20visibilityTogglePoint%2C%0A19%20%20%20%20(st_lineLocatePoint(geom%2C%20visibilityTogglePoint)%20*%20(t2%20-%20t1))%20%2B%20t1%20visibilityToggleTime%0A20%20%20FROM%20approach%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYes%2C%20the%20above%20PostGIS%20query%20is%20a%20rather%20complex%20one.%20We%20split%20the%20query%20into%20multiple%20common%20table%20expressions%20CTEs%2C%20to%20make%20it%20readable.%20In%20Postgres%2C%20CTEs%20give%20you%20the%20ability%20to%20%E2%80%9Cname%E2%80%9D%20a%20subquery%2C%20to%20make%20it%20easier%20to%20write%20SQL%20queries%20consisting%20of%20multiple%20steps.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3EThe%20first%20CTE%2C%3CCODE%3EpointPair%3C%2FCODE%3Ein%20Lines%201-7%2C%20uses%20the%20window%20function%3CCODE%3Elead%3C%2FCODE%3E%2C%20in%20order%20to%20pack%20every%20pair%20of%20consecutive%20points%2C%20that%20belong%20to%20the%20same%20bus%20trip%2C%20into%20one%20tuple.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThis%20is%20a%20preparation%20for%20the%20second%20CTE%2C%3CCODE%3Esegment%3C%2FCODE%3Ein%20Lines%207-12%2C%20which%20then%20connects%20the%20two%20points%20with%20a%20line%20segment.%20This%20step%20can%20be%20seen%20as%20a%20linear%20interpolation%20of%20the%20path%20between%20every%20two%20GPS%20points.%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20result%20of%20these%20two%20CTEs%20can%20be%20visualized%20in%20the%20map%20below%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22map-with-blue-and-red-lines-depicting-two-bus-routes-3.jpg%22%20style%3D%22width%3A%20659px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232171i5A7515827F5D32DE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22map-with-blue-and-red-lines-depicting-two-bus-routes-3.jpg%22%20alt%3D%22map-with-blue-and-red-lines-depicting-two-bus-routes-3.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThen%20the%20third%20CTE%2C%20approach%20Lines%2012-18%2C%20finds%20the%20locations%20where%20the%20bus%20starts%2Fends%20to%20be%20within%2030%20meters%20from%20the%20billboard.%20This%20is%20done%20by%20drawing%20a%20circular%20ring%20with%2030%20meters%20diameter%20around%20the%20billboard%2C%20and%20intersecting%20it%20with%20the%20segments%20of%20the%20bus%20trajectory.%20We%20thus%20get%20the%20two%20points%20in%20the%20map%20below%2C%20marked%20with%20the%20black%20cross.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22map-marking-points-with-X-where-billboards-visible-to-moving-bus-4.jpg%22%20style%3D%22width%3A%20660px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232172iAD2819D312BB068D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22map-marking-points-with-X-where-billboards-visible-to-moving-bus-4.jpg%22%20alt%3D%22map-marking-points-with-X-where-billboards-visible-to-moving-bus-4.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20last%20step%20in%20the%20earlier%20PostGIS%20query%2C%20Lines%2019-22%2C%20computes%20the%20time%20at%20these%20two%20points%20using%20linear%20referencing%2C%20that%20is%20assuming%20a%20constant%20speed%20per%20segment%3CSUP%3E%3CA%20id%3D%22fnref-2%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn-2%22%20target%3D%22_self%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20name%3D%22fnref-2%22%3E2%3C%2FA%3E%3C%2FSUP%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22map-marking-points-with-timestamps-when-billboards-visible-to-moving-bus-5.jpg%22%20style%3D%22width%3A%20721px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232173i885AB554E70152DF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22map-marking-points-with-timestamps-when-billboards-visible-to-moving-bus-5.jpg%22%20alt%3D%22map-marking-points-with-timestamps-when-billboards-visible-to-moving-bus-5.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1276339335%22%20id%3D%22toc-hId-1276339335%22%3EExercise%3A%20try%20to%20find%20a%20simpler%20way%20to%20express%20the%20PostGIS%20query%20displayed%20earlier.%20I%20couldn%E2%80%99t.%20%3A)%3C%2Fimg%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20PostGIS%20query%20had%20to%20be%20that%20complex%2C%20because%20it%20programs%20two%20non-trivial%20concepts%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EContinuous%20movement%20trajectory%3C%2FSTRONG%3E%3CSPAN%3E%3A%20while%20the%20GPS%20data%20is%20discrete%2C%20we%20had%20to%20reconstruct%20the%20continuous%20movement%20trajectory.%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ESpatiotemporal%20proximity%3C%2FSTRONG%3E%3CSPAN%3E%3A%20the%20continuous%20movement%20trajectory%20was%20used%20to%20find%20the%20location%20and%20time%20(i.e.%2C%20spatiotemporal)%20during%20which%20the%20bus%20was%20within%2030%20meters%20from%20the%20billboard.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20good%20news%20for%20you%20is%20that%20MobilityDB%20can%20help%20make%20it%20easier%20to%20analyze%20these%20types%20of%20movement%20trajectories.%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMobilityDB%2FMobilityDB%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EMobilityDB%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20is%20an%20extension%20of%20PostgreSQL%20and%20PostGIS%20that%20has%20implemented%20these%20spatiotemporal%20concepts%20as%20custom%20types%20and%20functions%20in%20Postgres.%3C%2FSPAN%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--531115128%22%20id%3D%22toc-hId--531115128%22%3EMobilityDB%3A%20a%20moving%20object%20database%20system%20for%20Postgres%20%26amp%3B%20PostGIS%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELet%E2%80%99s%20have%20a%20look%20on%20how%20to%20express%20this%20PostGIS%20query%20more%20simply%20using%20MobilityDB.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3EHere%20is%20how%20the%20previous%20PostGIS%20query%20would%20be%20expressed%20in%20MobilityDB.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20astext(atperiodset(trip%2C%20getTime(atValue(tdwithin(a.trip%2C%20b.geom%2C%2030)%2C%20TRUE))))%0AFROM%20busTrip%20a%2C%20billboard%20b%0AWHERE%20dwithin(a.trip%2C%20b.geom%2C%2030)%0A%0A--%7B%5BPOINT(651063.737915354%20667183.840879818)%402020-04-21%2008%3A38%3A12.507515%2B02%2C%20%0A--%20%20POINT(651052.741845233%20667213.026797244)%402020-04-21%2008%3A38%3A31%2B02%2C%20%20%20%0A--%20%20POINT(651042.581085347%20667231.762425657)%402020-04-21%2008%3A38%3A38.929465%2B02%5D%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWhat%20you%20need%20to%20know%20about%20the%20MobilityDB%20query%20above%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3EThe%20table%3CCODE%3EbusTrip%3C%2FCODE%3Ehas%20the%20attribute%20trip%20of%20type%3CCODE%3Etgeompoint%3C%2FCODE%3E.%20It%20is%20the%20MobilityDB%20type%20for%20storing%20a%20complete%20trajectory.%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThe%20nesting%20of%3CCODE%3Etdwithin-%26gt%3BatValue-%26gt%3BgetTime%3C%2FCODE%3Ewill%20return%20the%20time%20periods%20during%20which%20a%20bus%20trip%20has%20been%20within%20a%20distance%20of%2030%20meters%20to%20a%20billboard.%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThe%20function%3CCODE%3Eatperiodset%3C%2FCODE%3Ewill%20restrict%20the%20bus%20trip%20to%20only%20these%20time%20periods.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThe%20function%3CCODE%3Eastext%3C%2FCODE%3Econverts%20the%20coordinates%20in%20the%20output%20to%20textual%20format.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EAccordingly%2C%20the%20result%20shows%20the%20part%20of%20the%20bus%20trip%20that%20starts%20at%202020-04-21%2008%3A38%3A12.507515%2B02%20and%20ends%20at%2008%3A38%3A38.929465%2B02.%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.mobilitydb.com%2FMobilityDB%2Fdevelop%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EMobilityDB%20documentation%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20describes%20all%20of%20MobilityDB%E2%80%99s%20operations.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20we%20step%20back%2C%20and%20show%20the%20creation%20of%20the%3CCODE%3EbusTrip%3C%2FCODE%3Etable.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20EXTENSION%20MobilityDB%20CASCADE%3B%0A%0ACREATE%20TABLE%20busTrip(tripID%2C%20trip)%20AS%0A%20%20SELECT%20tripID%2Ctgeompointseq(array_agg(tgeompointinst(geom%2C%20t)%20ORDER%20BY%20t))%0AFROM%20gpsPoint%0AGROUP%20BY%20tripID%3B%0A%0A--SELECT%202%20%0A--Query%20returned%20successfully%20in%2078%20msec.%0A%0A%0ASELECT%20tripID%2C%20astext(trip)%20FROM%20busTrip%3B%0A%0A1%20%20%22%5BPOINT(651096.993815166%20667028.114604598)%402020-04-21%2008%3A37%3A27%2B02%2C%0A%20%20%20%20%20%20%20POINT(651080.424535144%20667123.352304597)%402020-04-21%2008%3A37%3A39%2B02%2C%0A%20%20%20%20%20%20%20POINT(651067.607438095%20667173.570340437)%402020-04-21%2008%3A38%3A06%2B02%2C%0A%20%20%20%20%20%20%20POINT(651052.741845233%20667213.026797244)%402020-04-21%2008%3A38%3A31%2B02%2C%0A%20%20%20%20%20%20%20POINT(651029.676773636%20667255.556944161)%402020-04-21%2008%3A38%3A49%2B02%2C%0A%20%20%20%20%20%20%20POINT(651018.401101238%20667271.441380755)%402020-04-21%2008%3A39%3A08%2B02%5D%22%0A2%20%20%22%5BPOINT(651201.431447782%20667089.682115196)%402020-04-21%2008%3A38%3A36%2B02%2C%0A%20%20%20%20%20%20%20POINT(651186.853162155%20667091.138189286)%402020-04-21%2008%3A38%3A43%2B02%2C%0A%20%20%20%20%20%20%20POINT(651181.995412783%20667077.531372716)%402020-04-21%2008%3A38%3A49%2B02%2C%0A%20%20%20%20%20%20%20POINT(651101.820139904%20667041.076539663)%402020-04-21%2008%3A38%3A56%2B02%2C%0A%20%20%20%20%20%20%20POINT(651262.17004873%20%20667119.331513367)%402020-04-21%2008%3A39%3A29%2B02%5D%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3EThe%20first%20step%20above%20is%20to%20create%20the%20MobilityDB%20extension%20in%20the%20database.%20In%20Postgres%2C%20the%3CCODE%3ECASCADE%3C%2FCODE%3Eoption%20results%20in%20executing%20the%20same%20statement%20on%20all%20the%20dependencies.%20In%20the%20query%20above%E2%80%94because%20PostGIS%20is%20a%20dependency%20of%20MobilityDB%E2%80%94%3CCODE%3ECASCADE%3C%2FCODE%3Ewill%20also%20create%20the%20PostGIS%20extension%2C%20if%20it%20has%20not%20yet%20been%20created.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThe%20second%20query%20above%20creates%20the%3CCODE%3EbusTrip%3C%2FCODE%3Etable%20with%20two%20attributes%3CCODE%3E(tripID%20int%2C%20trip%20tgeompoint)%3C%2FCODE%3E.%3CCODE%3Etgeompoint%3C%2FCODE%3Eis%20the%20MobilityDB%20type%20to%20represent%20a%20movement%20trajectory.%20The%3CCODE%3Etgeompoint%3C%2FCODE%3Eattribute%20is%20constructed%20from%20a%20temporally%20sorted%20array%20of%20instants%2C%20each%20of%20which%20is%20a%20pair%20of%20a%20spatial%20point%20and%20a%20timestamp.%20This%20construction%20is%20expressed%20in%20the%20query%20above%20by%20the%20nesting%20of%3CCODE%3Etgeompointinst%20-%26gt%3B%20array_agg%20-%26gt%3B%20tgeompointseq%3C%2FCODE%3E.%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThe%20last%3CCODE%3ESELECT%3C%2FCODE%3Equery%20above%20shows%20that%20the%3CCODE%3EbusTrip%3C%2FCODE%3Etable%20contains%20two%20tuples%2C%20corresponding%20to%20the%20two%20trips.%20Every%20trip%20has%20the%20format%3CCODE%3E%5Bpoint1%40time1%2C%20point2%40time2%2C%20...%5D%3C%2FCODE%3E.%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1956397705%22%20id%3D%22toc-hId-1956397705%22%3EBigger%20than%20an%20elephant%3A%20how%20to%20query%20movement%20trajectories%20at%20scale%2C%20when%20a%20single%20Postgres%20node%20won%E2%80%99t%20do%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20we%20now%20have%20two%20working%20solutions%20for%20measuring%20the%20billboard%20visibility%3A%20one%20in%20PostGIS%20and%20another%20one%20in%20MobilityDB%2C%20the%20next%20natural%20move%20is%20to%20apply%20these%20solutions%20to%20a%20big%20database%20of%20all%20bus%20trips%20in%20Brussels%20in%20the%20last%20year%2C%20and%20all%20billboards%20in%20Brussels.%20This%20amounts%20to%20roughly%205%20million%20bus%20trips%20(roughly%205%20billion%20GPS%20points)%20and%20a%20few%20thousand%20billboards.%20This%20size%20goes%20beyond%20what%20a%20single%20Postgres%20node%20can%20handle.%20Hence%2C%20we%20need%20to%20distribute%20the%20Postgres%20database.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20is%20a%20job%20for%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcitusdata%2Fcitus%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%2C%20the%20extension%20to%20Postgres%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20that%20transforms%20Postgres%20into%20a%20distributed%20database.%20Efficiently%20distributing%20the%20complex%20PostGIS%20query%20with%20many%20CTEs%20is%20a%20challenge%20we'll%20leave%20to%20the%20Citus%20engineering%20team.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWhat%20I%20want%20to%20discuss%20here%20is%20the%20distribution%20of%20the%20MobilityDB%20query.%20Citus%20does%20not%20know%20the%20types%20and%20operations%20of%20MobilityDB.%20So%20the%20distribution%20is%20limited%20by%20what%20Citus%20can%20do%20in%20general%20for%20custom%20types%20and%20functions.%20My%20colleague%2C%20Mohamed%20Bakli%2C%20has%20done%20this%20assessment%20and%20published%20it%20in%20a%20paper%20titled%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdl.acm.org%2Fdoi%2F10.1145%2F3356999.3365467%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3E%E2%80%9CDistributed%20moving%20object%20data%20management%20in%20MobilityDB%E2%80%9D%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20in%20the%20ACM%20BigSpatial%20workshop%20(%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.mobilitydb.com%2Fpub%2FDistMobilityDB_BigSpatial19.pdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Epreprint%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E)%20and%20in%20a%20demo%20paper%20titled%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fieeexplore.ieee.org%2Fdocument%2F9162249%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3E%E2%80%9CDistributed%20Mobility%20Data%20Management%20in%20MobilityDB%E2%80%9D%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20in%20the%20IEEE%20MDM%20conference%20(%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.mobilitydb.com%2Fpub%2FMobilityDBDemo_MDM2020.pdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Epreprint%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E).%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20papers%20presented%20a%20solution%20to%20distribute%20MobilityDB%20using%20Citus.%20All%20the%20nodes%20in%20the%20Citus%20database%20cluster%20had%20PostgreSQL%2C%20PostGIS%2C%20MobilityDB%2C%20and%20Citus%20installed.%20The%20goal%20was%20to%20assess%20to%20what%20extent%20the%20spatiotemporal%20functions%20in%20MobilityDB%20can%20be%20distributed.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ETo%20do%20this%20assessment%2C%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMobilityDB%2FMobilityDB-BerlinMOD%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EBerlinMOD%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20benchmark%20(a%20tool%20for%20comparing%20moving%20object%20databases)%20was%20used.%20BerlinMOD%20consists%20of%20a%20trajectory%20data%20generator%2C%20and%2017%20benchmark%20queries%20that%20assess%20the%20features%20of%20a%20moving%20object%20database%20system.%20It%20was%20possible%20to%20execute%2013%20of%20the%2017%20BerlinMOD%20benchmark%20queries%20on%20a%20MobilityDB%20database%20cluster%20that%20is%20managed%20by%20Citus%2C%20without%20special%20customization.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESee%20also%20the%20illuminating%20blog%20post%20about%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fblog%2F2020%2F06%2F25%2Fusing-custom-types-with-citus-and-postgres%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eusing%20custom%20types%20with%20Citus%20%26amp%3B%20Postgres%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%2C%20by%20Nils%20Dijk.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EBack%20to%20our%20MobilityDB%20billboard%20visibility%20query%2C%20our%20mission%20is%20to%20calculate%20the%20billboard%20visibility%20for%20all%20billboards%20and%20all%20common%20transport%20vehicles%20in%20Brussels%20for%20an%20entire%20year.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%20had%20set%20up%20a%20Citus%20database%20cluster%2C%20and%20created%20the%20MobilityDB%20extension%20in%20all%20its%20nodes.%20Then%20we%20used%20the%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.citusdata.com%2Fen%2Fstable%2Fdevelop%2Freference_ddl.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20%60create_distributed_table%60%20function%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20to%20distribute%20the%3CCODE%3EbusTrip%3C%2FCODE%3Etable%20across%20all%20the%20worker%20nodes%20in%20the%20Citus%20database%20cluster.%20Next%20we%20made%20the%20billboard%20table%20a%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.citusdata.com%2Fen%2Fstable%2Fdevelop%2Freference_ddl.html%23reference-tables%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20reference%20table%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%2C%20and%20copied%20the%20reference%20table%20to%20all%20the%20worker%20nodes.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EHere%20is%20the%20resulting%20distributed%20query%20plan%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EEXPLAIN%0ASELECT%20atperiodset(trip%2C%20getTime(atValue(tdwithin(a.trip%2C%20b.geom%2C%2030)%2C%20TRUE)))%0AFROM%20busTrip%20a%2C%20billboard%20b%0AWHERE%20dwithin(a.trip%2C%20b.geom%2C%2030)%3B%0A%0A%0AQuery%20plan%0A----------------------------------------------------------------------------------------%0ACustom%20Scan%20(Citus%20Adaptive)%20%20(cost%3D0.00..0.00%20rows%3D100000%20width%3D32)%0A%20%20Task%20Count%3A%2032%0A%20%20Tasks%20Shown%3A%20One%20of%2032%0A%20%20-%26gt%3B%20%20Task%0A%20%20%20%20%20%20Node%3A%20host%3D10.140.135.15%20port%3D5432%20dbname%3Droma%0A%20%20%20%20%20%20-%26gt%3B%20%20Nested%20Loop%20%20(cost%3D0.14..41.75%20rows%3D1%20width%3D32)%0A%20%20%20%20%20%20%20%20%20%20-%26gt%3B%20%20Seq%20Scan%20on%20provinces_dist_102840%20b%20(cost%3D0.00..7.15%20rows%3D15%20width%3D32)%0A%20%20%20%20%20%20%20%20%20%20-%26gt%3B%20%20Index%20Scan%20using%20spgist_bustrip_idx_102808%20on%20bustrip_hash_tripid_102808%20a%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20(cost%3D0.14..2.30%20rows%3D1%20width%3D32)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20Index%20Cond%3A%20(trip%20%26amp%3B%26amp%3B%20st_expand(b.geom%2C%20'30'%3A%3Adouble%20precision))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20Filter%3A%20_dwithin(trip%2C%20b.geom%2C%20'30'%3A%3Adouble%20precision)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fblog%2F2020%2F04%2F27%2Fhow-citus-distributed-query-executor-adapts-to-postgres-workload%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20distributed%20query%20executor%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20parallelizes%20the%20query%20over%20all%20workers%20in%20the%20Citus%20cluster.%20Every%20node%20also%20has%20the%20MobilityDB%20extension%2C%20which%20means%20we%20can%20use%20MobilityDB%20functions%20such%20as%3CCODE%3Edwithin%3C%2FCODE%3Ein%20the%20query%20and%20in%20the%20indexes.%20Here%20for%20example%2C%20we%20see%20that%20the%20SP-GiST%20index%20on%20the%20Citus%20worker%20is%20used%20to%20efficiently%20evaluate%20the%3CCODE%3EWHERE%20dwithin(...)%3C%2FCODE%3Eclause.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWith%20this%2C%20we%20come%20to%20the%20end%20of%20this%20post.%20To%20sum%20up%2C%20this%20post%20has%20two%20main%20takeaways%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20you%E2%80%99re%20ever%20looking%20to%20analyze%20movement%20trajectories%20to%20understand%20the%20spatiotemporal%20interaction%20of%20things%20across%20space%20and%20time%2C%20you%20now%20have%20a%20few%20new%20(open%20source!)%20options%20in%20your%20Postgres%20and%20PostGIS%20toolbox%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMobilityDB%2FMobilityDB%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EMobilityDB%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20can%20help%20you%20to%20manage%20and%20analyze%20geospatial%20(e.g.%20GPS%2C%20radar)%26nbsp%3B%20movement%20trajectories%20in%20PostgreSQL.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EMobilityDB%20%2B%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fdownload%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3ECitus%20open%20source%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20work%20together%20out%20of%20the%20box%2C%20so%20you%20can%20analyze%20geospatial%20movement%20trajectories%20at%20scale%2C%20too.%20Just%20add%20the%20two%20Postgres%20extensions%20(along%20with%20PostGIS)%20into%20your%20Postgres%20database%2C%20and%20you%20are%20ready%20to%20manage%20big%20geospatial%20trajectory%20datasets.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20style%3D%22margin-bottom%3A%200.75em%3B%22%3E%3CSTRONG%3EFootnotes%3C%2FSTRONG%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20id%3D%22fn-1%22%20role%3D%22doc-endnote%22%3ECurious%20about%20the%20source%20of%20this%20data%3F%20The%20trajectory%20is%20for%20line%2071%20in%20Brussels%2C%20when%20it%20goes%20in%20front%20of%20my%20university%20campus%20ULB%20Solbosch.%20The%20public%20transport%20company%20in%20Brussels%20publishes%20an%20open%20API%2C%20where%20all%20the%20trajectories%20of%20their%20vehicles%20can%20be%20probed%20%3CA%20href%3D%22https%3A%2F%2Fopendata.stib-mivb.be%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fopendata.stib-mivb.be%3C%2FA%3E.%20The%20billboard%20location%20was%20invented%20by%20me%2C%20and%20the%20background%20map%20comes%20from%20%3CA%20href%3D%22http%3A%2F%2Fwww.openstreetmap.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOpenStreetMap%3C%2FA%3E.%20%3CA%20role%3D%22doc-backlink%22%20href%3D%22%23fnref-1%22%20target%3D%22_self%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22fn-2%22%20role%3D%22doc-endnote%22%3EIt%20remains%20to%20compute%20the%20visibility%20duration%2C%20i.e.%2C%20the%20difference%20in%20seconds%20between%20the%20two%20timestamps%2C%20which%20can%20be%20done%20by%20another%20CTE%20and%20window%20functions.%20Not%20to%20further%20complicate%20the%20query%2C%20we%20skip%20this%20detail%20here.%20%3CA%20role%3D%22doc-backlink%22%20href%3D%22%23fnref-2%22%20target%3D%22_self%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1859278%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Illustration-of-bus-in-motion-on-a-map-MobilityDB-and-Citus.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F232167iC2EB89FC22ADA11E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Illustration-of-bus-in-motion-on-a-map-MobilityDB-and-Citus.jpg%22%20alt%3D%22Illustration-of-bus-in-motion-on-a-map-MobilityDB-and-Citus.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EHow%20to%20analyze%20geospatial%20movement%20trajectories%20at%20scale%20with%20Postgres%20and%20the%20MobilityDB%2C%20PostGIS%2C%20%26amp%3B%20Citus%20extensions.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1859278%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECitus%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Egeospatial%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOpen%20Source%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPostgres%20extensions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgresql%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Escalability%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Frequent Visitor

GPS has become part of our daily life. GPS is in cars for navigation, in smartphones helping us to find places, and more recently GPS has been helping us to avoid getting infected by COVID-19. Managing and analyzing mobility tracks is the core of my work. My group in Université libre de Bruxelles specializes in mobility data management. We build an open source database system for spatiotemporal trajectories, called MobilityDB. MobilityDB adds support for temporal and spatio-temporal objects to the Postgres database and its spatial extension, PostGIS. If you’re not yet familiar with spatiotemporal trajectories, not to worry, we’ll walk through some movement trajectories for a public transport bus in just a bit.

 

One of my team’s projects is to develop a distributed version of MobilityDB. This is where we came in touch with the Citus extension to Postgres and the Citus engineering team. This post presents issues and solutions for distributed query processing of movement trajectory data. GPS is the most common source of trajectory data, but the ideas in this post also apply to movement trajectories collected by other location tracking sensors, such as radar systems for aircraft, and AIS systems for sea vessels.

 

Illustration-of-bus-in-motion-on-a-map-MobilityDB-and-Citus-1920x1080.jpg

 

As a start, let's explore the main concepts of trajectory data management, so you can see how to analyze geospatial movement trajectories.

 

The following animated gif shows a geospatial trajectory of a public transport bus1 that goes nearby an advertising billboard. What if you wanted to assess the visibility of the billboard to the passengers in the bus? If you can do this for all billboards and vehicles, then you would be able to extract interesting insights for advertising agencies to price the billboards, and for advertisers who are looking to optimize their campaigns.

 

Throughout this post, I’ll use maps to visualize bus trajectories and advertising billboards in Brussels, so you can learn how to query where (and for how long) the advertising billboards are visible to the bus passengers. The background maps are courtesy of OpenStreetMap.Throughout this post, I’ll use maps to visualize bus trajectories and advertising billboards in Brussels, so you can learn how to query where (and for how long) the advertising billboards are visible to the bus passengers. The background maps are courtesy of OpenStreetMap.

 

In the animated gif above, we simply assume that if the bus is within 30 meters to the billboard, then it is visible to its passengers. This “visibility” is indicated in the animation by the yellow flash around the billboard when the bus is within 30 meters of the billboard.

 

How to measure the billboard’s visibility to a moving bus using a database query?

 

Let’s prepare a toy PostGIS database that minimally represents the example in the previous animated gif—and then gradually develop an SQL query to assess the billboard visibility to the passengers in a moving bus.

 

If you are not familiar with PostGIS, it is probably the most popular extension to Postgres and is used for storing and querying spatial data. For the sake of this post, all you need to know is that PostGIS extends Postgres with data types for geometry point, line, and polygon. PostGIS also defines functions to measure the distance between geographic features and to test topological relationships such as intersection.

 

In the SQL code block below, first you create the PostGIS extension. And then you will create two tables: gpsPoint and billboard.

 

CREATE EXTENSION PostGIS;
CREATE TABLE gpsPoint (tripID int, pointID int, t timestamp, geom geometry(Point, 3812));
CREATE TABLE billboard(billboardID int, geom geometry(Point, 3812));

INSERT INTO gpsPoint Values
(1, 1, '2020-04-21 08:37:27', 'SRID=3812;POINT(651096.993815166 667028.114604598)'),
(1, 2, '2020-04-21 08:37:39', 'SRID=3812;POINT(651080.424535144 667123.352304597)'),    
(1, 3, '2020-04-21 08:38:06', 'SRID=3812;POINT(651067.607438095 667173.570340437)'),
(1, 4, '2020-04-21 08:38:31', 'SRID=3812;POINT(651052.741845233 667213.026797244)'),    
(1, 5, '2020-04-21 08:38:49', 'SRID=3812;POINT(651029.676773636 667255.556944161)'),    
(1, 6, '2020-04-21 08:39:08', 'SRID=3812;POINT(651018.401101238 667271.441380755)'),    
(2, 1, '2020-04-21 08:39:29', 'SRID=3812;POINT(651262.17004873  667119.331513367)'),    
(2, 2, '2020-04-21 08:38:36', 'SRID=3812;POINT(651201.431447782 667089.682115196)'),    
(2, 3, '2020-04-21 08:38:43', 'SRID=3812;POINT(651186.853162155 667091.138189286)'),    
(2, 4, '2020-04-21 08:38:49', 'SRID=3812;POINT(651181.995412783 667077.531372716)'),    
(2, 5, '2020-04-21 08:38:56', 'SRID=3812;POINT(651101.820139904 667041.076539663)');   

INSERT INTO billboard Values
(1, 'SRID=3812;POINT(651066.289442793 667213.589577551)'),
(2, 'SRID=3812;POINT(651110.505092035 667166.698041233)');

 

The database is visualized in the map below.  You can see that the gpsPoint table has points of two bus trips, trip 1 in blue and trip 2 in red. In the table, each point has a timestamp. The two billboards are the gray diamonds in the map. 

 

map-with-gpspoints-for-two-bus-trips-2.jpg

 

Your next step is to find the locations where a bus is within 30 meters from a billboard—and also the durations, i.e., how long the moving bus is within 30 meters of the billboard.

 

SELECT tripID, pointID, billboardID
FROM gpsPoint a, billboard b
WHERE st_dwithin(a.geom, b.geom, 30);

--1    4    1

 

This PostGIS query above does not solve the problem. Yes, the condition in the WHERE clause finds the GPS points that are within 30 meters from a billboard. But the PostGIS query does not tell the duration of this event. 

 

Furthermore, imagine that point 4 in trip 1 (the blue trip) was not given. This query would have then returned null. The problem with this query is that it does not deal with the continuity of the bus trip, i.e. the query does not deal with the movement trajectory of the bus. 

 

We need to reconstruct a continuous movement trajectory out of the given GPS points. Below is another PostGIS query that would find both the locations of the billboard’s visibility to the passengers in the bus, and also the duration of how long the billboard was visible to the bus passengers.

 

1   WITH pointPair AS(
2     SELECT tripID, pointID AS p1, t AS t1, geom AS geom1,
3       lead(pointID, 1) OVER (PARTITION BY tripID ORDER BY pointID) p2,
4       lead(t, 1) OVER (PARTITION BY tripID ORDER BY pointID) t2,
5       lead(geom, 1) OVER (PARTITION BY tripID ORDER BY pointID) geom2    
6     FROM gpsPoint
7   ), segment AS(
8     SELECT tripID, p1, p2, t1, t2,
9       st_makeline(geom1, geom2) geom
10    FROM pointPair
11    WHERE p2 IS NOT NULL    
12  ), approach AS(
13    SELECT tripID, p1, p2, t1, t2, a.geom,
14      st_intersection(a.geom, st_exteriorRing(st_buffer(b.geom, 30))) visibilityTogglePoint
15    FROM segment a, billboard b
16    WHERE st_dwithin(a.geom, b.geom, 30)
17  )
18  SELECT tripID, p1, p2, t1, t2, geom, visibilityTogglePoint,
19    (st_lineLocatePoint(geom, visibilityTogglePoint) * (t2 - t1)) + t1 visibilityToggleTime
20  FROM approach;

 

Yes, the above PostGIS query is a rather complex one. We split the query into multiple common table expressions CTEs, to make it readable. In Postgres, CTEs give you the ability to “name” a subquery, to make it easier to write SQL queries consisting of multiple steps. 

  • The first CTE, pointPair in Lines 1-7, uses the window function lead, in order to pack every pair of consecutive points, that belong to the same bus trip, into one tuple.

  • This is a preparation for the second CTE, segment in Lines 7-12, which then connects the two points with a line segment. This step can be seen as a linear interpolation of the path between every two GPS points. 

 

The result of these two CTEs can be visualized in the map below:

 

map-with-blue-and-red-lines-depicting-two-bus-routes-3.jpg

 

Then the third CTE, approach Lines 12-18, finds the locations where the bus starts/ends to be within 30 meters from the billboard. This is done by drawing a circular ring with 30 meters diameter around the billboard, and intersecting it with the segments of the bus trajectory. We thus get the two points in the map below, marked with the black cross.

 

map-marking-points-with-X-where-billboards-visible-to-moving-bus-4.jpg

 

The last step in the earlier PostGIS query, Lines 19-22, computes the time at these two points using linear referencing, that is assuming a constant speed per segment2.

 

map-marking-points-with-timestamps-when-billboards-visible-to-moving-bus-5.jpg

 

Exercise: try to find a simpler way to express the PostGIS query displayed earlier. I couldn’t. :)

 

The PostGIS query had to be that complex, because it programs two non-trivial concepts:

  • Continuous movement trajectory: while the GPS data is discrete, we had to reconstruct the continuous movement trajectory. 
  • Spatiotemporal proximity: the continuous movement trajectory was used to find the location and time (i.e., spatiotemporal) during which the bus was within 30 meters from the billboard.

 

The good news for you is that MobilityDB can help make it easier to analyze these types of movement trajectories. MobilityDB is an extension of PostgreSQL and PostGIS that has implemented these spatiotemporal concepts as custom types and functions in Postgres. 

 

MobilityDB: a moving object database system for Postgres & PostGIS

 

Let’s have a look on how to express this PostGIS query more simply using MobilityDB. Here is how the previous PostGIS query would be expressed in MobilityDB. 

 

SELECT astext(atperiodset(trip, getTime(atValue(tdwithin(a.trip, b.geom, 30), TRUE))))
FROM busTrip a, billboard b
WHERE dwithin(a.trip, b.geom, 30)

--{[POINT(651063.737915354 667183.840879818)@2020-04-21 08:38:12.507515+02, 
--  POINT(651052.741845233 667213.026797244)@2020-04-21 08:38:31+02,   
--  POINT(651042.581085347 667231.762425657)@2020-04-21 08:38:38.929465+02]}

 

What you need to know about the MobilityDB query above:

  • The table busTrip has the attribute trip of type tgeompoint. It is the MobilityDB type for storing a complete trajectory. 
  • The nesting of tdwithin->atValue->getTime will return the time periods during which a bus trip has been within a distance of 30 meters to a billboard.  
  • The function atperiodset will restrict the bus trip to only these time periods.
  • The function astext converts the coordinates in the output to textual format.
  • Accordingly, the result shows the part of the bus trip that starts at 2020-04-21 08:38:12.507515+02 and ends at 08:38:38.929465+02. 

 

The MobilityDB documentation describes all of MobilityDB’s operations.

 

Now we step back, and show the creation of the busTrip table.

 

CREATE EXTENSION MobilityDB CASCADE;

CREATE TABLE busTrip(tripID, trip) AS
  SELECT tripID,tgeompointseq(array_agg(tgeompointinst(geom, t) ORDER BY t))
FROM gpsPoint
GROUP BY tripID;

--SELECT 2 
--Query returned successfully in 78 msec.


SELECT tripID, astext(trip) FROM busTrip;

1  "[POINT(651096.993815166 667028.114604598)@2020-04-21 08:37:27+02,
       POINT(651080.424535144 667123.352304597)@2020-04-21 08:37:39+02,
       POINT(651067.607438095 667173.570340437)@2020-04-21 08:38:06+02,
       POINT(651052.741845233 667213.026797244)@2020-04-21 08:38:31+02,
       POINT(651029.676773636 667255.556944161)@2020-04-21 08:38:49+02,
       POINT(651018.401101238 667271.441380755)@2020-04-21 08:39:08+02]"
2  "[POINT(651201.431447782 667089.682115196)@2020-04-21 08:38:36+02,
       POINT(651186.853162155 667091.138189286)@2020-04-21 08:38:43+02,
       POINT(651181.995412783 667077.531372716)@2020-04-21 08:38:49+02,
       POINT(651101.820139904 667041.076539663)@2020-04-21 08:38:56+02,
       POINT(651262.17004873  667119.331513367)@2020-04-21 08:39:29+02]"

 

  • The first step above is to create the MobilityDB extension in the database. In Postgres, the CASCADE option results in executing the same statement on all the dependencies. In the query above—because PostGIS is a dependency of MobilityDB—CASCADE will also create the PostGIS extension, if it has not yet been created. 

  • The second query above creates the busTrip table with two attributes (tripID int, trip tgeompoint). tgeompoint is the MobilityDB type to represent a movement trajectory. The tgeompoint attribute is constructed from a temporally sorted array of instants, each of which is a pair of a spatial point and a timestamp. This construction is expressed in the query above by the nesting of tgeompointinst -> array_agg -> tgeompointseq.  

  • The last SELECT query above shows that the busTrip table contains two tuples, corresponding to the two trips. Every trip has the format [point1@time1, point2@time2, ...]

 

Bigger than an elephant: how to query movement trajectories at scale, when a single Postgres node won’t do

 

As we now have two working solutions for measuring the billboard visibility: one in PostGIS and another one in MobilityDB, the next natural move is to apply these solutions to a big database of all bus trips in Brussels in the last year, and all billboards in Brussels. This amounts to roughly 5 million bus trips (roughly 5 billion GPS points) and a few thousand billboards. This size goes beyond what a single Postgres node can handle. Hence, we need to distribute the Postgres database.

 

This is a job for Citus, the extension to Postgres that transforms Postgres into a distributed database. Efficiently distributing the complex PostGIS query with many CTEs is a challenge we'll leave to the Citus engineering team.

 

What I want to discuss here is the distribution of the MobilityDB query. Citus does not know the types and operations of MobilityDB. So the distribution is limited by what Citus can do in general for custom types and functions. My colleague, Mohamed Bakli, has done this assessment and published it in a paper titled “Distributed moving object data management in MobilityDB” in the ACM BigSpatial workshop (preprint) and in a demo paper titled “Distributed Mobility Data Management in MobilityDB” in the IEEE MDM conference (preprint). 

 

The papers presented a solution to distribute MobilityDB using Citus. All the nodes in the Citus database cluster had PostgreSQL, PostGIS, MobilityDB, and Citus installed. The goal was to assess to what extent the spatiotemporal functions in MobilityDB can be distributed. 

 

To do this assessment, the BerlinMOD benchmark (a tool for comparing moving object databases) was used. BerlinMOD consists of a trajectory data generator, and 17 benchmark queries that assess the features of a moving object database system. It was possible to execute 13 of the 17 BerlinMOD benchmark queries on a MobilityDB database cluster that is managed by Citus, without special customization.

 

See also the illuminating blog post about using custom types with Citus & Postgres, by Nils Dijk. 

 

Back to our MobilityDB billboard visibility query, our mission is to calculate the billboard visibility for all billboards and all common transport vehicles in Brussels for an entire year. 

 

We had set up a Citus database cluster, and created the MobilityDB extension in all its nodes. Then we used the Citus `create_distributed_table` function to distribute the busTrip table across all the worker nodes in the Citus database cluster. Next we made the billboard table a Citus reference table, and copied the reference table to all the worker nodes. 

 

Here is the resulting distributed query plan:

 

EXPLAIN
SELECT atperiodset(trip, getTime(atValue(tdwithin(a.trip, b.geom, 30), TRUE)))
FROM busTrip a, billboard b
WHERE dwithin(a.trip, b.geom, 30);


Query plan
----------------------------------------------------------------------------------------
Custom Scan (Citus Adaptive)  (cost=0.00..0.00 rows=100000 width=32)
  Task Count: 32
  Tasks Shown: One of 32
  ->  Task
      Node: host=10.140.135.15 port=5432 dbname=roma
      ->  Nested Loop  (cost=0.14..41.75 rows=1 width=32)
          ->  Seq Scan on provinces_dist_102840 b (cost=0.00..7.15 rows=15 width=32)
          ->  Index Scan using spgist_bustrip_idx_102808 on bustrip_hash_tripid_102808 a     
              (cost=0.14..2.30 rows=1 width=32)
              Index Cond: (trip && st_expand(b.geom, '30'::double precision))
              Filter: _dwithin(trip, b.geom, '30'::double precision)

 

The Citus distributed query executor parallelizes the query over all workers in the Citus cluster. Every node also has the MobilityDB extension, which means we can use MobilityDB functions such as dwithin in the query and in the indexes. Here for example, we see that the SP-GiST index on the Citus worker is used to efficiently evaluate the WHERE dwithin(...) clause.

 

With this, we come to the end of this post. To sum up, this post has two main takeaways:

 

If you’re ever looking to analyze movement trajectories to understand the spatiotemporal interaction of things across space and time, you now have a few new (open source!) options in your Postgres and PostGIS toolbox:

  • MobilityDB can help you to manage and analyze geospatial (e.g. GPS, radar)  movement trajectories in PostgreSQL.

  • MobilityDB + Citus open source work together out of the box, so you can analyze geospatial movement trajectories at scale, too. Just add the two Postgres extensions (along with PostGIS) into your Postgres database, and you are ready to manage big geospatial trajectory datasets.

Footnotes

  1. Curious about the source of this data? The trajectory is for line 71 in Brussels, when it goes in front of my university campus ULB Solbosch. The public transport company in Brussels publishes an open API, where all the trajectories of their vehicles can be probed https://opendata.stib-mivb.be. The billboard location was invented by me, and the background map comes from OpenStreetMap.

  2. It remains to compute the visibility duration, i.e., the difference in seconds between the two timestamps, which can be done by another CTE and window functions. Not to further complicate the query, we skip this detail here.