Skip to content

Commit

Permalink
Npoint split h (MobilityDB#633)
Browse files Browse the repository at this point in the history
* Enable npoint in MEOS

* New files meos_geo and meos_npoint
  • Loading branch information
estebanzimanyi authored Oct 16, 2024
1 parent f760a03 commit ed0cd7d
Show file tree
Hide file tree
Showing 40 changed files with 2,312 additions and 301 deletions.
22 changes: 8 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,17 @@ option(MEOS

# Option for including geoposes
option(POSE
"Set ON|OFF (default=OFF) to include geoposes
"
OFF
"Set ON|OFF (default=OFF) to include geoposes
"
OFF
)

if(NOT MEOS)
# Option for including network points
option(NPOINT
"Set ON|OFF (default=ON) to include network points
"
ON
# Option for including network points
option(NPOINT
"Set ON|OFF (default=ON) to include network points
"
ON
)
else()
# Set NPOINT option for excluding support for network points, which require a
# table named `ways` containing the geometries of the road network
set(NPOINT OFF)
endif()

if(NPOINT)
message(STATUS "Including network points")
Expand Down
102 changes: 102 additions & 0 deletions meos/examples/data/ways.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
gid,the_geom
0,01020000202C1600000200000000006093698A4D400000D2C083555640888B3EA302AC4D403F04BE9E12B75240
1,01020000202C160000030000000000B2F61FD254400000CE00C885554000004A712A4551400000EC55524D54400000A01EF4182B40000022CC2EF45140
2,01020000202C160000020000000000C0A15B402B400000C0E836625840E867B6B03A2B424051BDFA9FE6195040
3,01020000202C160000050000000000F0705D5A394000006C2CF23750400000783A066A34400000786BA0F557400000348550A95540000000AA30940D400000466C687B54400000FCECF5DC4B4000003CDBF339554000001AF086DE5740
4,01020000202C1600000200000000003069430F5440000020249334224082BD8674579D4D406AD2F44597872F40
5,01020000202C16000002000000000040B6365108400000C8CBF6B95540749AADBE159A2F40839D7FA6C12F3C40
6,01020000202C160000040000000000C0BF48D72E40000000F4D06056400000FA8B38BB574000005A812CEA57400000845863A75840000030DE64003440D833542E0FC05740D1BD9D49E3C63540
7,01020000202C1600000200000000007E94119D56400000A04B523F1E409E00B3B0FA694C4064DD188AB1564C40
8,01020000202C1600000200000000004A90FBEA51400000AC0500FE4D400000FC3FBE5B434000003094456D5040
9,01020000202C160000040000000000EC6F90114B40000060102D9C4B400000E05F1D4B36400000A072E9E54840000068C1F3EA4A400000081FC12545400000D662340352400000900837B02640
10,01020000202C160000020000000000F4AD126C4B400000804431A91A409DE1E7132E9755401E32B7F0AABA4140
11,01020000202C1600000200000000007869E67C5040000008959E824E40E3A29AA539C14F404C9C842579CB4740
12,01020000202C160000020000000000FE1691CF51400000CCB9539E444041E1AE59E85540409C453B9357212D40
13,01020000202C1600000300000000008067B4CEF23F00003C9730C04D400000DA0FDE245340000030C53A6B5440000028B8A98C41400000D46BC90F5440
14,01020000202C16000003000000000080F8E37E4A400000AC55D2FB4B4000009C9D6AD84E400000204F9EA545400000D24012A25540000070C348122C40
15,01020000202C160000040000000000A0AF46E34C4000000CEBE5A24B400000B42B975B4440000000095C134E4000009E0AA83D57400000A8208566444000005410146B4C400000806A8BB50040
16,01020000202C160000020000000000524560C9514000004C1316025040EA05E48170AD5440B0B7779C82F24A40
17,01020000202C1600000200000000008036F2C914400000B8A61C7D364035196BAE521B3040B579E34639B04340
18,01020000202C1600000300000000002CCF34F254400000AEC1B69655400000E09A39E73840000060292A774B404E116D6B86903D40DCDBBFFFBD654740
19,01020000202C1600000300000000007C01CEAD404000009AA148B353400000E0E3EEA04040000096FF44F85840C5C75E0EFECE4240C3CD04D1BA0A4B40
20,01020000202C160000020000000000F84F1FAB3F400000A0630FD621403C35EE7422D84B407088D1CC995B4940
21,01020000202C160000040000000000E007731C4A400000587E793848400000B81768253B4000005224F3D456400000845453B955400000645ECCC1484000003024A6364A4000000635D8755240
22,01020000202C16000002000000000028CC44C748400000D4FAAA794140F37AC1AB475C4B40130FFF63B0BD4140
23,01020000202C160000020000000000BA28D85354400000603331EB33400000F46324A253400000C4759D1F5840
24,01020000202C16000005000000000058A077C13E400000E069B477464000001E593B9552400000A0314415364000009826B6D43F400000402186D837400000801EA4A052400000C0EA5BB01640000080B7D4FE1240000018131A113840
25,01020000202C160000020000000000E6649BF554400000243535074F401314CD5CD7595240DAE7DDB3999D5140
26,01020000202C160000030000000000508A28C64D400000C027145E3940000080821DD747400000C6C6472A50400000A02AD3B51A4000004A0E5BE65040
27,01020000202C1600000300000000000014555F09400000D00BE3912D400000D012CA7C5240000020BBA90D48405BAA366B59BE5240B9174A15FEAB4940
28,01020000202C160000030000000000B0C205A757400000A8888D3B3A40000000DE7CC8F83F000084BA40554C400000CA61D57253400000468FF8665640
29,01020000202C160000040000000000F2F8A5FD514000003E4ECCB3564000005CED78F9514000008A62EB5350400000B631F43F514000002C67696457400000C89EFEDB38400000A63B79975740
30,01020000202C160000020000000000786E47615640000080A09BB5F03F5FCA2CDAF62822407481C14DC94F4040
31,01020000202C160000040000000000885782B6394000005609584F52400000B808C2DE31400000E8484EB64540000006CC5DF650400000B0C8270D46400000D82F927C4640000020A5F8833840
32,01020000202C160000030000000000644E834A41400000F49992F64A400000463F0F7757400000C0D537643940000058E908D339400000A043F5954D40
33,01020000202C1600000200000000000886AE64334000008249FA01554032233908A0454C4017FBEB0A4AE05140
34,01020000202C160000020000000000183D835B4C4000004C406B4944409EB51D76787745408AF62BA520EA4B40
35,01020000202C160000020000000000D86D7B744A400000E0DC8D042540FEB1DFA6DB2E50405C9C965E1ACB3A40
36,01020000202C160000020000000000209290D3214000009C4E920C42402568915F60963C4034F4443A8B804F40
37,01020000202C160000020000000000B080A22B204000003855481F4A40785E853B608C2E40BEC9C3D3562E4E40
38,01020000202C160000020000000000E4D56FD757400000F4D9D6435240EBA73179F12651403B14E339ABE45340
39,01020000202C1600000400000000002EAF86DE524000008E154C1257400000A0A890B04140000044FFFAE354400000B880E04346400000502B709B4940BE3B12A2AE444640C5F9FF316A844440
40,01020000202C160000020000000000B2C425EB584000006A22E0BE50406571D8BFAB6C5540F080C235C8D35040
41,01020000202C160000030000000000488C234B32400000E0E1C1CB2E4000009EE9943151400000B0D2EB6C4F400000088C79AE36400000D0D9B6582E40
42,01020000202C16000003000000000006C5921658400000B8825F8448400000A8663140514000003846F7F546400000627DB89053400000161104EF5740
43,01020000202C16000002000000000062C3EDE758400000900FD4EE4F40000060259C5228400000CCE5CDA65040
44,01020000202C1600000200000000008018B6425440000040FAB7D82B4013C61B4E823A4E40B9B41E3D70F22840
45,01020000202C16000004000000000034454BEC5740000000388488D03F00002686FDD457400000840DF65C46400000E0EAB950514000008452F29F4440E655EF8C28684340BF894ECCEC6F4C40
46,01020000202C160000030000000000302F05D0464000005833D7C939400000E027297612400000FC878AE74340000010B927923D40000060B4B6605040
47,01020000202C16000006000000000088E5CDD249400000FC2406E744400000A85C3209404000009CC17349424000003CA7DB6344400000E8CE91C33B40000070CA846A4240000088B3D60D424000007C7A5F1F524000008001C55F184000000683E45653400000A0FE92DE5740
48,01020000202C16000002000000000008B12D183F4000006019D1163240ECFEAD6090784340A6435FB9E1143B40
49,01020000202C1600000400000000002856CB034C400000345459CA574000001CBCE06E54400000A0E0C9B41E40000064CDB87B424000008053CF9B2B400000B0FD0C2046400000823ACA6F5340
50,01020000202C16000005000000000010E448455440000030A2ACB52B40000030C5EE1D2C4000005A3445CB554000007E179ACD56400000C0BB8612234000003401FF214D400000000F964E36400000407714A03B40000030A1C31B4D40
51,01020000202C160000030000000000F422517741400000BA81C15551400000C07DC39D4F4000004464A4B04640000010ECAC2B2C400000B45D8FF34F40
52,01020000202C1600000700000000004011C5D211400000DC6B23975640000000A07BD3B03F0000AC91505141400000E02D1232474000001C338ECE57400000C813245E30400000F469AB084B4000000034D5184040000088BD0B2C3E40000004345C945840000010A4A6E123400000BE4D61EF514000001418A5D95340
53,01020000202C16000003000000000064261F73554000006E4CEAA450400000300224042E400000DCFD6FD5574000002034A7D738400000700854A84240
54,01020000202C160000030000000000D29DCBC354400000A8C4AA4E4C400000B05A3BF24C400000183F1A334A400000D084DCD83340000040EFFB2D0A40
55,01020000202C1600000400000000000ED0FEEF54400000C01E026C52400000540E2FBC51400000EA3B228752400000A8FD367052400000A072754D1640BAA783B788665040C69A439203902E40
56,01020000202C160000030000000000A0A804254A40000080109E744140000090FE1D4F23400000F882B2DD324000007011A7F548400000E0DA3BF51340
57,01020000202C160000030000000000F08CFDCE55400000306623F33B400000282F88C43A40000088DEA5EC454063EB1EB4C21249406E86D0BABBE94C40
58,01020000202C160000050000000000E0DA8397304000008872AC8E354000008C051F47424000008837E12A364000005CDFAC0745400000C03B4B2516400000FC53310E46400000D0DE5DA13040E9791A9A64524140C456F54BB3634240
59,01020000202C160000020000000000F03A91BC3D400000FC5147564E40B544FE52AF7D3C40D8D7FD1980BD4D40
60,01020000202C160000050000000000586DFAFB4B4000000C39DE7046400000008858D6DB3F0000642DDE235240000028391A9C504000009CB6CB7E42400000787B96AE57400000B04685FF20400000781E754345400000EC4A5E6B4340
61,01020000202C160000040000000000DC1B7E9E48400000483656884140000060A5E64B2D4000004A2C28C4504000008054D7924B400000D8B9673A4C400000984B81CD33400000FCF88F385540
62,01020000202C160000030000000000E808B075364000006016209C334000004874468335400000F0F3A9B328400000C0EB489254400000A47FDB544A40
63,01020000202C1600000500000000003063C1CD3E400000341C727D4F4000000C4C6FC345400000362CEE1B514000001813D9F2324000008CC2261A56400000BE84C437514000002A7294E056400000CCF7816C4B400000E003A6721740
64,01020000202C160000020000000000784963AC4F400000F4DEC9314840C82299979CF74840B0F1BCD309384F40
65,01020000202C160000030000000000C06C388A0040000020B482714940000056275F59504000006438846F404000005041D3BB2B400000E06AB6D82140
66,01020000202C160000020000000000E0596168404000001A903D555840000058CD748B51400000307D14533840
67,01020000202C160000020000000000962D9F02534000007067801F2540ED706D7D749B4040B2F8174353603440
68,01020000202C16000003000000000010CB077A26400000F0099AB545400000F83FB98C53400000204427053F40661D325820604D402F523913EF985040
69,01020000202C160000020000000000888D05B942400000BC5BA4C44F405151DCEEAADE41401FFE37B7441E4D40
70,01020000202C16000003000000000020C78CF731400000A02C73D13B40000034A6170F4D400000F2719C0A5840000060384D6B4D4000005072ACF05840
71,01020000202C1600000200000000009E3042FB56400000966B8E125740A4E12E105FD65340EC3697FA4A035440
72,01020000202C1600000200000000004E4AF4B156400000C0A5544D4640CCB9CDDD31D34640B08258B8099F3440
73,01020000202C160000020000000000BC2BEBE94540000060FBD45020404BC40E4BB1414140B62EAA6DCAD03A40
74,01020000202C160000020000000000E028EFDD3A40000048A0BD5330402954AA72CE6D3940687A7777D13D3540
75,01020000202C16000003000000000070E703DE3B4000005A09C97C5040000088E2D2F23F400000101B71E24C4010D21F3992454B4030B886C628204C40
76,01020000202C160000030000000000C06469BC24400000EC08BF274540000078654A044A400000ECD126FF46403766C5D3835B4C40711BB24B02284C40
77,01020000202C160000070000000000A8378F3030400000385207753240000054C2FDA340400000180DB1114840000016627A0E53400000802DE98E1B4000008035C1DE08400000B06937A5334000008081B23F014000008AF7949750400000D093E5A94640000006B97243574000000869A6B23C4000009C8313785840
78,01020000202C1600000200000000004C0E445D584000001451324C4240000050632F3F4F40000060B1134D1F40
79,01020000202C1600000200000000002023E70D2D4000001848C9E84A409FC59D0A078240405B96CD3EA9AE5240
80,01020000202C16000002000000000044A72E3E41400000E4FC76C44F40491D1B85FF634A40ACAA9ADC90AC3840
81,01020000202C16000002000000000090874E3D36400000A4A7A82C4140383588BEE05C534063D609BD32984D40
82,01020000202C160000020000000000926925A650400000C828A60C3B4060BA415AEDB7524049136D3805604240
83,01020000202C160000030000000000F8F5510D47400000380DD8BC31400000E61BE34C54400000E8EE42475240000000F021894E40000000D5A25EF03F
84,01020000202C160000020000000000E499E4D3544000004CBE9DAF414000002AF17D26584000002C6717C54740
85,01020000202C16000002000000000080231C8A1A400000B44F188B47405557FA898CB9344057DD1C18A0124E40
86,01020000202C16000002000000000014FFAB364E4000005E3B65BC574000003EE4EAEB504000001815A93E5340
87,01020000202C1600000200000000005E42CEE356400000184148153740000010D83DAE37400000445EB1305240
88,01020000202C160000020000000000046E20305540000098E92B725240D60DB360112E4E400F6B285E5F035240
89,01020000202C16000002000000000000327E9905400000BCDBEB2941407855DFD37351364059E5875693D44F40
90,01020000202C160000030000000000E0710F19474000004447D0EB5740000000BFC20E2A4000003813D5C94A409B79440A920F4040483205098A734140
91,01020000202C160000020000000000BE7DBCB35240000000C76B84EB3F9D50583178225140803793B5B7C13F40
92,01020000202C160000030000000000F05763063240000020F8F69C424000007CCF1F184D400000C261D88658400000BAA102A055400000E00A7A891140
93,01020000202C1600000300000000009AF9DAE158400000F85404FB4C400000A8A72B154A400000304E988221403C3596EC070B4D40FCBA7AEBA2023540
94,01020000202C160000040000000000887EC5AB4E400000E0E767DC36400000C6FA83785540000098BDA3394D400000D86EC6CC41400000801E9A430F40CE6632C99FC25240360FBFA131754F40
95,01020000202C160000050000000000B4460A7141400000E0E260085040000040F177040B400000466A801B58400000B01A02382940000084EA32C74E400000F8B40B3B314000003426504C4340000030E11B764B400000D029802A4540
96,01020000202C1600000500000000007603625F55400000D05E947057400000D09FBC9C33400000D885896A47400000E863B99E30400000C02E32FB15400000801B6A2115400000CC20231C4A400000009F7FCE22400000004AFEB1EE3F
97,01020000202C160000050000000000B03429D6264000005CF9B3C85440000034DF1E1746400000402830E61C400000F0EFDE355040000000205A1CD43F00008C642D994B40000094E0B9A64B4000006477A62F47400000A0D51B855640
98,01020000202C16000003000000000078F6EDDF48400000E63F9CA055400000002D7C05404000008085839C26400000C0AAE5EB0D4000009A29E6A85140
99,01020000202C1600000200000000007C843E0947400000C0487DBF2440503D8D602824494056ABECDF4E2B4040
100,01020000202C160000030000000000FCA907B758400000482294C34F400000E85EA5624A4000001C0424CB5840B84A737D6AB34740E767DCBBB0335540
197 changes: 197 additions & 0 deletions meos/examples/geom_npoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*****************************************************************************
*
* This MobilityDB code is provided under The PostgreSQL License.
* Copyright (c) 2016-2024, Université libre de Bruxelles and MobilityDB
* contributors
*
* MobilityDB includes portions of PostGIS version 3 source code released
* under the GNU General Public License (GPLv2 or later).
* Copyright (c) 2001-2024, PostGIS contributors
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without a written
* agreement is hereby granted, provided that the above copyright notice and
* this paragraph and the following two paragraphs appear in all copies.
*
* IN NO EVENT SHALL UNIVERSITE LIBRE DE BRUXELLES BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF UNIVERSITE LIBRE DE BRUXELLES HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* UNIVERSITE LIBRE DE BRUXELLES SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
* AN "AS IS" BASIS, AND UNIVERSITE LIBRE DE BRUXELLES HAS NO OBLIGATIONS TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
*****************************************************************************/

/**
* @brief A simple program that get the SRID of the geometries stored in the
* file `ways.csv` that has the content of the PostgreSQL table ways defined
* as follows
* @code
* CREATE TABLE public.ways (
* gid bigint NOT NULL,
* the_geom public.geometry NOT NULL,
* length double precision NOT NULL
* );
* @endcode
*
* The program can be build as follows
* @code
* gcc -Wall -g -I/usr/local/include -o geom_npoint geom_npoint.c -L/usr/local/lib -lmeos
* @endcode
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <float.h>
#include <meos.h>

/* Number of ways in a batch for printing a marker */
#define NO_WAYS_BATCH 10
/* Maximum length in characters of a header record in the input CSV file */
#define MAX_LENGTH_HEADER 1024
/* Maximum length in characters of a geometry in the input data */
#define MAX_LENGTH_GEOM 100001

typedef struct
{
long int gid;
GSERIALIZED *the_geom;
double length;
} ways_record;

/* Main program */
int main(void)
{
/* Initialize MEOS */
meos_initialize();

/* Get start time */
clock_t t;
t = clock();

/* Distances */
double dist, min_dist = DBL_MAX;
/* Position in the geometry with the shortest distance */
double pos;
/* Return value */
int return_value = 0;

/* Substitute the full file path in the first argument of fopen */
FILE *file = fopen("data/ways.csv", "r");

if (! file)
{
printf("Error opening input file\n");
meos_finalize();
return 1;
}

ways_record rec;
int no_records = 0;
int no_nulls = 0;
char header_buffer[MAX_LENGTH_HEADER];
char geo_buffer[MAX_LENGTH_GEOM];
const char *geo_str = "SRID=5676;POINT(72.94967061684646 25.156720715884354)";
GSERIALIZED *point = geom_in(geo_str, -1);

/* Read the first line of the file with the headers */
fscanf(file, "%1023s\n", header_buffer);

printf("Reading the instants (one '*' marker every %d instants)\n",
NO_WAYS_BATCH);

/* Continue reading the file */
do
{
int read = fscanf(file, "%ld,%100000[^\n]\n",
&rec.gid, geo_buffer);

if (ferror(file))
{
printf("Error reading input file");
return_value = 1;
goto cleanup;
}

if (read != 3 && ! feof(file))
{
printf("Record with missing values ignored\n");
no_nulls++;
continue;
}

no_records++;
if (no_records % NO_WAYS_BATCH == 0)
{
printf("*");
fflush(stdout);
}

/* Transform the string representing the geometry into a geometry value */
rec.the_geom = geom_in(geo_buffer, -1);
if (geo_is_empty(rec.the_geom))
{
printf("The geometry is empty");
return_value = 1;
goto cleanup;
}

/* We need to implement the following SQL query for a given geo
* SELECT npoint(gid, ST_LineLocatePoint(the_geom, geo))
* FROM public.ways WHERE ST_DWithin(the_geom, geo, DIST_EPSILON)
* ORDER BY ST_Distance(the_geom, geo) LIMIT 1;
*/

pos = line_locate_point(rec.the_geom, point);
if (pos < 0)
{
free(rec.the_geom);
continue;
}

dist = geom_distance2d(rec.the_geom, point);
if (dist < min_dist)
min_dist = dist;
} while (! feof(file));

/* Close the input file */
fclose(file);

printf("\n%d records read.\n%d incomplete records ignored.\n",
no_records, no_nulls);

/* If the point was not found */
if (! point)
{
printf("The geometry point cannot be transformed into a network point");
return_value = 1;
goto cleanup;
}

Npoint *np = npoint_make(rec.gid, pos);
char *np_str = npoint_out(np, 3);
printf("Network point: %s\n", np_str);
free(np); free(np_str);

/* Calculate the elapsed time */
t = clock() - t;
double time_taken = ((double) t) / CLOCKS_PER_SEC;
printf("The program took %f seconds to execute\n", time_taken);

/* Clean up */
cleanup:

/* Free memory */
free(point);

/* Finalize MEOS */
meos_finalize();

return return_value;
}
Loading

0 comments on commit ed0cd7d

Please sign in to comment.