Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: finding nearby points of interest (POIs) for location-based ser… #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions example/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
open Rtree
open Printf
(* Sample Point of Interest data structure *)
type poi = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making this it's own module:

module Poi = struct
  type t = {...}
end

name : string;
latitude : float;
longitude : float;
}


module Rtree = struct

(* Function to insert POIs into the R-tree *)
let insert_pois rtree pois =
List.iter (fun poi -> Rtree.insert rtree (poi.latitude, poi.longitude, poi)) pois

(* Function to load geospatial data from a CSV file *)
let load_geospatial_data_from_csv filename =
let ic = open_in filename in
let csv = Csv.of_channel ~has_header:true ic in
let pois = ref [] in
Csv.iter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this more functional I think we should use fold_left instead of iter + mutable state :))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @patricoferris, will do that

(fun row ->
let name = row.(0) in
let latitude = float_of_string row.(1) in
let longitude = float_of_string row.(2) in
pois := { name; latitude; longitude } :: !pois
)
csv;
close_in ic;
List.rev !pois

(* Example usage with geospatial data from a CSV file *)
let () =
let rtree = Rtree.create () in

(* Load geospatial data from external csv file *)
let csv =
List.map (fun name -> name, Csv.load name)
[ "example/pois.csv"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not valid OCaml as we need an in afterwards.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok @patricoferris , I will have to check and correct that



(* Perform real-world spatial queries *)
let nearby_pois = find_nearby_pois rtree 42.3601 (-71.0589) 1.0 in
List.iter (fun poi -> Printf.printf("Nearby: %s\n" poi.name)) nearby_pois
4 changes: 4 additions & 0 deletions example/pois.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name,latitude,longitude
Restaurant A,42.3601,-71.0589
Cafe B,42.3605,-71.0587
Park C,42.3609,-71.0585