## 3 Materials and Methods
### GeoSpark System Overview
#### 3.6.4 \(k\)-Anonymity Set in GeoSpark  
Algorithm 1 presents the steps of a spatial \(k\)-NN in GeoSpark. It takes as input a non-indexed or indexed \(SRDD\), a query center point as well as a parameter \(k\), which indicates the number of nearest neighbors. The algorithm is separated in two phases, namely selection and sorting phase [33], and will be utilized in the simulations.  
```
1:input The number of \(k\) nearest neighbors
2:input A query center object \(A\)
3:input A Spatial RDD \(B\)
4:output A list of \(k\) spatial objects
5:Step 1: Selection phase
6:for all partition \(\in\) SRDD \(B\)do
7:if an index exists then
8: Return \(k\)-neighbors of \(A\) by querying the index of this partition
9:else
10:for all object \(\in\) this partition do
11: Check all the distance between the object \(A\) and Spatial RDD \(B\)
12:endfor
13:endif
14:endfor
15:Maintain a priority queue that stores the top \(k\) nearest neighbors
16:Step 2: Sorting phase
17:Sort the spatial objects in the intermediate Spatial RDD based on their distances to \(A\)
18:Return top \(k\) objects in \(C\)
```  
**Algorithm 1** Spatial \(k\)-NN Query  
Our aim is to exploit the above mentioned structures and Algorithm 1 so as to formulate the \(k\)-anonymity set for a number of moving objects based on their trajectory (spatio-temporal) data. From the trajectory points of a number of mobile objects at different timestamps, a collection of spatial BikesRDD is constructed. The goal of this paper is to provide an efficient and scalable framework for robust continuous \(k\)-NN querying of spatial objects in GeoSpark. A Scala RDD API for creating the desired PointRDD from a csv file is introduced in following Algorithm 2.  
```
1:Define csv file location in pointRDDInputLocation
2:Define the attributes start Column equal to 0 in pointRDDOffset
3:Define pointRDDSplitter as FileDataSplitter.csv
4:Define rest attributes in carryOtherAttributes = true
5:Create PointRDD: PointRDD = new PointRDD(sc, pointRDDInputLocation, pointRDDOffset, pointRDDSplitter, carryOtherAttributes)
```  
**Algorithm 2** Spatial PointRDD Creation  
An iterative application of a typical spatial \(k\)-NN query, which was presented in Algorithm 1, is introduced with use of Scala RDD API of Geospark in following Algorithm 3.  
```
1:input \(k\), usingIndex, number of timestamps \(L\)
2:for timestamp \(t=1\) to \(L\)do
3: Create the Dataframe \(df\) with the selected features in \(t\)
4: Save \(df\) as csv file \(f\)
5: Create from csv a PointRDD
6:if usingIndex = true then
7: Build \(R\)-Tree index on PointRDD
8:endif
9:for all lines \(\in\) csv file \(f\)do
10: Read the selected features \((f_{1},f_{2})\)
11: Create querypoint
12: val fact = new GeometryFactory()
13: val querypoint = fact.createPoint(new Coordinate(\(f_{1}\),\(f_{2}\)))
14: Run Spatial \(k\)-NN Query to return \(k\) geometry points
15: val queryResultList = KNNQuery.SpatialKnnQuery(PointRDD, querypoint, \(k\), usingIndex)
16:endfor
17:endfor
```  
**Algorithm 3** Iterative Spatial \(k\)-NN Query  
It is worth mentioning the fact that the output format of the spatial \(k\)-NN query (indexed or not) is a list of geometries as in Table 5, where the \(id\) of the point geometry is related to the mobile object \(id\). In addition, the list holds the top-\(k\) geometry objects. In the following, the above methodology and algorithms will be considered for the performance evaluation of the proposed \(k\)-anonymity method based on spatial \(k\)-NN.