Analisi di prossimità

Avvertimento

This tutorial is now obsolete. A new and updated version is available at Nearest Neighbor Analysis (QGIS3)

Il GIS è molto utile nell’analizzare le relazioni spaziali tra le geometrie. Un’analisi di questo genere consiste nello scoprire quali geometrie sono più vicine a un dato elemento. QGIS dispone di uno strumento chiamato «Matrice di distanza» che è di grande aiuto in questo tipo di indagini. In questo tutorial useremo 2 dataset e scopriremo quali punti in un primo layer sono i più vicini a determinati punti presenti in secondo layer.

Descrizione dell’esercizio

Data la localizzazione di tutti i più importanti terremoti conosciuti, andremo ad individuare i luoghi popolati più vicini a ciascuno dei punti in cui si sono verificati i terremoti.

Altri aspetti che avremo modo di apprendere nel corso dell’esercizio.

  • Come realizzare delle operazioni di unione tabllare in QGIS. (Vedete anche Realizzare unioni tabellari qualora vi occorrano spiegazioni dettagliate).

  • Using Query Builder to show a subset of features from a layer.

  • Using MMQGIS plugin to create hub lines to visualize the nearest neighbors.

Ottenere i dati necessari

We will use NOAA’s National Geophysical Data Center’s Significant Earthquake Database as our layer representing all major earthquakes. Download the tab-delimited earthquake data.

Natural Earth fornisce un prezioso dataset dei luoghi abitati. Scaricate il simple (less columns) dataset

For convenience, you may directly download a copy of both the datasets from the links below:

signif.txt

ne_10m_populated_places_simple.zip

Data Sources: [NCEI] [NATURALEARTH]

Procedimento

  1. Open Layer ‣ Add Layer ‣ Add Delimited Text Layer and browse to the downloaded signif.txt file.

../_images/1194.png
  1. Dal momento che si tratta di tab-delimited file, scegliete Tab alla voce formato file. I campi X e Y saranno compilati automaticamente. Click su OK.

Nota

Vedrete comparire dei messaggi di errore mentre QGIS sta importando il file. Si tratta di errori di incolonnamento e alcune righe del file non saranno importate. Per quelli che sono gli scopi di questo esercizio questi errori possono essere trascurati senza conseguenze. Chiudete.

../_images/2158.png
  1. As the earthquake dataset has Latitude/Longitude coordinates, it will be imported with the default CRS of EPSG: 4326. Verify that is the case in the bottom-right corner. Let’s also open the Populated Places layer. Go to Layer ‣ Add Layer ‣ Add Vector Layer.

../_images/399.png
  1. Browse to the downloaded ne_10m_populated_places_simple.zip file and click Open.

../_images/463.png
  1. Fate degli zoom ed esplorate entrambi i dataset. Ciascun punto viola indica il luogo in cui si è verificato un importante terremoto e ciascun punto blu rappresenta un luogo fortemente popolato. Noi cerchiamo un modo per individuare il punto appartenente al layer dei luoghi abitati che sia il più vicino a uno dei punti del layer dei terremoti.

../_images/556.png
  1. Go to Vettore ‣ Strumenti di analisi ‣ Matrice di distanza.

../_images/654.png
  1. Here select the earthquake layer signif as the Input point layer and the populated places ne_10m_populated_places_simple as the target layer. You also need to select a unique field from each of these layers which is how your results will be displayed. In this analysis, we are looking to get only 1 nearest point, so check the Use only the nearest(k) target points, and enter 1. Name your output file matrix.csv, and click OK. Once the processing finishes, click Close.

Nota

Un’osservazione di una certa utilità è quella che eventualmente potreste anche realizzare questa analisi con un solo layer. Selezionando lo stesso layer come vettore di punti in input e vettore di punti in output. Il risultato saranno i punti più vicini nello stessa layer invece dei punti più vicini in due layer diversi.

../_images/753.png
  1. Once the processing finishes, click the Close button in the Distance Matrix dialog. You can now view the matrix.csv file in Notepad or any text editor. QGIS can import CSV files as well, so we will add it to QGIS and view it there. Go to Layer ‣ Add Layer ‣ Add Delimited Text Layer….

../_images/852.png
  1. Individuate il file appena creato matrix.csv. Dal momento che si tratta di un file composto da colonne di solo testo, spuntate la casella Nessuna geometria (solo tabella degli attributi) nella riga Definizione geometria. Click su OK.

../_images/951.png
  1. Vedrete il file CSV caricato in forma di tabella. Click sul tasto destro del layer tabellare alla voce Apri Tabella degli Attributi.

../_images/1059.png
  1. A questo punto dovreste essere in grado di esaminare il contenuto dei vostri risultati. Il campo InputID contiene il campo nome del layer dei terremoti. Il campo TargetID contiene il nome degli elementi provenienti dal layer dei luoghi abitati che sono più vicini ai punti che indicano i terremoti. Il campo Distance contiene la distanza tra i 2 punti.

Nota

Ricordate che il calcolo della distanza viene ottenuto usando il Sistema di Riferimento dei layer. Qui la distanza è misurata in unità di gradi decimali perché le coordinate del nostro layer sorgente sono in gradi. Qualora desideraste avere le distanze misurate in metri dovreste riproiettare i layer prima di utilizzare lo strumento di analisi.

../_images/1195.png
  1. Questo dato si avvicina abbastanza al risultato che stavamo cercando. Per alcuni utenti questa tabella può essere considerata sufficiente. Tuttavia, noi intendiamo anche integrare i risultati nel nostro layer dei terremoti usando una Unione Tabellare. Click con il tasto destro del layer dei terremoti e dal menu selezionate Proprietà.

../_images/1261.png
  1. Spostatevi sulla scheda Join e fate click sul pulsante + .

../_images/1455.png
  1. We want to join the data from our analysis result to this layer. We need to select a field from each of the layers that has the same values. Select matrix as the Join layer` and InputID as the Join field. The Target field would be I_D. Leave other options to their default values and click OK.

../_images/1455.png
  1. Vedrete apparire la scheda Join. Click su OK.

../_images/1551.png
  1. Now open the attribute table of the signif layer by right-clicking and selecting Open Attribute Table.

../_images/1648.png
  1. Vedrete che, per ogni punto dei terremoti, abbiamo adesso un attributo che è il punto vicino più prossimo (cioè il posto abitato più vicino) e la distanza da questo punto.

../_images/1745.png
  1. We will now explore a way to visualize these results. First, we need to make the table join permanent by saving it to a new layer. Right-click the signif layer and select Save As….

../_images/1842.png
  1. Click the Browse button next to Save as label and name the output layer as earthquake_with_places.shp. Make sure the Add saved file to map box is checked and click OK.

../_images/1933.png
  1. Once the new layer is loaded, you can turn off the visibility of the signif layer. As our dataset is quite large, we can run our visualization analysis on a subset of the data. QGIS has a neat feature where you can load a subset of features from a layer without having to export it to a new layer. Right-click the earthquake_with_places layer and select Properties.

../_images/2028.png
  1. In the General tab, scroll down to the Feature subset section. Click Query Builder.

../_images/2159.png
  1. For this tutorial, we will visualize the earthquakes and their nearest populated places for Mexico. Enter the following expression in the Query Builder dialog.

"COUNTRY" = 'MEXICO'
../_images/2229.png
  1. You will see that only the points falling within Mexico will be visible in the canvas. Let’s do the same for the populated places layer. Right-click on the ne_10m_populated_places_simple layer and select Properties.

../_images/2326.png
  1. Open the Query Builder dialog from the General tab. Enter the following expression.

"adm0name" = 'Mexico'
../_images/2426.png
  1. Now we are ready to create our visualization. We will use a plugin named MMQGIS. Find and install the plugin. See Usare i Plugins for more details on how to work with plugins. Once you have the plugin installed, go to MMQGIS ‣ Create ‣ Hub Lines.

../_images/2524.png
  1. Select ne_10m_populated_places_simple as the Hub Point Layer and name as the Hub ID Attribute. Similarly, select earthquake_with_places as the Spoke Point Layer and matrix_Tar as the Spoke Hub ID Attribute. The hub lines algorithm will go through each of earthquake points and create a line that will join it to the populated place which matches the attribute we specified. Click Browse and name the Output Shapefile as earthquake_hub_lines.shp. Click OK to start the processing.

../_images/2622.png
  1. The processing may take a few minutes. You can see the progress on the bottom-left corner of the QGIS window.

../_images/2721.png
  1. Once the processing is done, you will see the earthquake_hub_lines layer loaded in QGIS. You can see that each earthquake point now has a line that connects it to the nearest populated place.

../_images/2820.png

If you want to give feedback or share your experience with this tutorial, please comment below. (requires GitHub account)