# Neighborhood analysis in Web GIS: a Covid-19 Case

It’s easier than you might think

# A brief introduction

In this article, we will look at how to develop Web GIS applications that can perform neighborhood analysis. Virtually, all mapping applications can be considered Web GIS applications, as they are an information system and deal with spatial data. However, a true GIS usually consists of 4 basic subsystems — data entry, processing and management, analysis, and presentation. The analysis part is often missing from most web GIS applications, in which the main goal is to present geographical data in an aesthetically pleasing way. But, as we will see here, spatial analysis is not something difficult with today’s tools.

# What are the spatial neighbors?

Spatial neighbors are entities that have some spatial connection to the entity under study. The spatial connection can be interpreted in several ways. The simplest form is that of the distance between them. For example, we can define a distance threshold above which spatial entities will not be considered neighbors. It is important here to understand that neighbors are always about an entity in question. When we assign a value to an entity based on its neighbors, we do not also give the neighbors a value, but in turn, the neighbors will get a value based on their own neighbors.

Regarding distance, it is also important to note that this is often given as an exponential function to include the distance decay effect. Distance decay is based on Tobler’s first law of geography and practically means that the furthest we are from a location the less likely it is to interact with it (based on an exponential function that can vary depending on the subject).

Another well-known neighborhood method is the k-nearest neighbors. This method gives the k-nearest neighbors regardless of distance. This is a very common method in image analysis applications, in which neighboring pixels are examined. Finally, some consider neighborhoods within a network (e.g. the road network). There are many more methods of spatial connection, each one with its pros and cons depending on what we want to do.

# Neighborhood analysis, why is it useful?

Consider a timely example. Let’s say we have countries with daily cases of Covid-19. The relationship between neighboring countries may help to understand how the virus moves. Of course, this may not be applicable in all cases. Still, there is no doubt that neighboring countries have greater interaction with each other than the more distant ones. Especially, in matters related to personal hygiene. An examination of a country’s state and its neighbors can provide insight into the origin of the virus. For example, if neighboring countries have a lower infection rate compared to the country in question, it is more likely that the transmission came from air travel. If all neighboring countries have high infection rates, it is more likely that the transmission came from border interaction. Finally, if the country has a low infection rate compared to its neighbors, then they should increase border testing.

Another application of neighborhood analysis is clustering. It is possible to identify groups of countries that are in a similar state and act accordingly. The results of a spatial analysis could eventually be used for proper decision-making.

# Ok, neighborhood analysis is important, how do I do it in a web GIS application?

Finding neighbors is a process that requires many resources. For example, to create a table with the distances between countries (as polygons) may take several hours or even days. This is not something we can do in real-time in a web application. Additionally, it would not be good practice to load the server with such processes by hundreds or thousands of users at a time.

But there is an important parameter that we can consider. Spatial data in most applications do not change over time. Take the countries in this example. How often do countries change spatially? But even in applications where we can have frequent changes in spatial data (let’s say soil samples), the spatial proximity can be calculated on the server at regular intervals.

What does this mean in practice? That we can calculate the spatial neighbors in advance and feed them to the application as static data, without requiring the user to run the spatial neighborhood algorithms each time. Calculating spatial neighbors is a simple process in most GIS software. For example, in QGIS there is the distance matrix tool with which we can calculate the distance of all spatial entities between them. In the example with the countries the table would look something like this:

Having this distance matrix, it is now easy to calculate the neighbors by any method. For example, if we want the 4 closest neighbors, a simple application is the following:

CountryObject = {

ISO: "",

Name: "",

Value: "",

Neighbors: []

}countriesLoop:

for (var c = 0; c < countries.length; c++) {

var neighbors = [];neighborsLoop:

for (var n = 0; n < neighborsTable.length; n++) {

if (neighborsTable[n].fromCountryISO === countries[c].ISO) {

neighbors.push(neighborsTable[n])

if (neighbors.length === 4) {

countries[c].neighbors = neighbors;

break neighborsLoop;

}

}

}

}

The double loop may not be the most efficient way to perform the calculation. You can try different methods to achieve the same result. If we wanted to find the neighbors based on the distance we could use the distance in the if statement, something like this:

`If (neighborsTable[n].distance < thresholdDistance) { … }`

# Time for spatial calculations

Once we have found the neighbors of our spatial entities, it is time to calculate the spatial neighborhood models. There are many models that we can calculate from the spatial neighborhood. Here, we will give two relatively simple examples, the spatial moving average, and the spatial similarity index.

The spatial moving average is like the moving average in statistics, in which we calculate the average value of subsets of the data. Respectively, in the spatial moving average, we calculate the average value of a subset of the spatial entities, i.e. the neighbors. In this way, we can find spatial clusters in which the value is high or low. In the example of Covid-19 cases, we can identify clusters of high values in Europe and low values in Africa.

The spatial similarity index is an index that tells us how much uniformity the spatial entity under examination has with its neighbors. In this index, the value of the spatial entity under study is compared to the average value of its neighbors. As shown in the picture, we can see which countries are different from their neighbors. They should be especially careful if they have a low value compared to their neighbors.

To calculate these two indicators the process is relatively simple. From the table with the countries and their neighbors:

countriesLoop:for (var c = 0; c < countries.length; c++) {neighborsLoop:for (var n = 0; n < countries[c].neighbors.length; n++) {var SumNeighborsValue = countries[c].neighbors[n].Value++}var meanNeighborsValue = SumNeighborsValue / countries[c].neighbors.lengthcountries[c].SMAValue = (SumNeighborsValue + countries[c].Value) / countries[c].neighbors.length + 1 // Spatial Moving Average, the sum of neighbors' values plus current spatial entity’s value to the number of neighbors plus the current spatial entitycountries[c].SSIValue = countries[c].Value / meanNeighborsValue // Spatial Similarity Index, the current spatial entity’s value to the mean value of neighbors}

In the SMA method, each entity takes the average value of the neighborhood, while in SSI, its value to the average value of the neighborhood.

The last thing you need to do is to present the results on a map. The best way is with a color gradation that shows the differentiation of the neighbors compared to the examined spatial entity. Before that, we need to group the values into some classes so that the visualization is better. There are many ways to sort a series of values and get breakpoints and classes, like the Jenks optimization method.

To sum it up:

- Create a neighbors' table of the spatial entities (e.g. with distance) on the appropriate software.
- Feed the neighbors’ table to the app. It could be through REST calls to a database or a JSON file.
- On the app, get the neighbors and perform the calculation that you want.
- Classify the values and provide a color for each class to the spatial entities.

You can see an example from the above here.

Note that the website does not update very often because this is a pilot project done for educational purposes.