Annuncio: tutti i progetti non commerciali registrati per l'utilizzo di Earth Engine prima del 15 aprile 2025 devono verificare l'idoneità non commerciale per mantenere l'accesso a Earth Engine.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Le raccolte possono essere unite in base alla posizione spaziale e ai valori delle proprietà. Per eseguire l'unione in base alla posizione spaziale, utilizza un filtro withinDistance() con i campi di join .geo specificati. Il campo .geo indica che la geometria
dell'elemento deve essere utilizzata per calcolare la metrica della distanza. Ad esempio, considera l'attività di
ricerca di tutte le
centrali elettriche nel raggio di 100 chilometri
del Parco nazionale di Yosemite, negli Stati Uniti. A questo scopo, utilizza un filtro sui campi della geometria
con la distanza massima impostata su 100 chilometri utilizzando il parametro distance:
Tieni presente che l'esempio precedente unisce un FeatureCollection a un altro
FeatureCollection. L'unione saveAll() imposta una proprietà
(points) su ogni elemento della raccolta primary che
memorizza un elenco di punti entro 100 km dall'elemento. La distanza di ogni punto dalla caratteristica viene memorizzata nella proprietà distance di ogni punto unito.
Le unioni spaziali possono essere utilizzate anche per identificare quali elementi
di una raccolta intersecano quelli di un'altra. Ad esempio, considera due raccolte di elementi: una raccolta primary contenente poligoni che rappresentano i confini degli stati degli Stati Uniti e una raccolta secondary contenente posizioni puntiformi che rappresentano centrali elettriche. Supponiamo che sia necessario determinare il numero che interseca ogni
stato. Questo può essere ottenuto con un join spaziale come segue:
Nell'esempio precedente, tieni presente che il filtro intersects() non memorizza
una distanza come fa il filtro withinDistance(). L'output dovrebbe essere simile alla Figura 1.
Figura 1. Grafico a barre che mostra il numero di centrali elettriche che intersecano ogni stato degli Stati Uniti.
[null,null,["Ultimo aggiornamento 2025-07-25 UTC."],[[["\u003cp\u003eCollections in Earth Engine can be joined based on spatial relationships like proximity or intersection using the \u003ccode\u003eee.Join\u003c/code\u003e module.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ewithinDistance()\u003c/code\u003e filter joins features within a specified distance, while the \u003ccode\u003eintersects()\u003c/code\u003e filter joins features that overlap geographically.\u003c/p\u003e\n"],["\u003cp\u003eSpatial joins can be used for various analyses, such as finding power plants within a certain distance of a park or counting power plants within each state boundary.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esaveAll()\u003c/code\u003e join method creates a new property containing a list of all matching features from the secondary collection, enabling further analysis and visualization.\u003c/p\u003e\n"]]],[],null,["# Spatial Joins\n\nCollections can be joined by spatial location as well as by property values. To join based\non spatial location, use a `withinDistance()` filter with `.geo` join\nfields specified. The `.geo` field indicates that the item's\ngeometry is to be used to compute the distance metric. For example, consider the task of\nfinding all\n[power plants](https://developers.google.com/earth-engine/datasets/catalog/WRI_GPPD_power_plants) within 100 kilometers\nof Yosemite National Park, USA. For that purpose, use a filter on the geometry\nfields, with the maximum distance set to 100 kilometers using the `distance`\nparameter:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load a primary collection: protected areas (Yosemite National Park).\nvar primary = ee.FeatureCollection(\"WCMC/WDPA/current/polygons\")\n .filter(ee.Filter.eq('NAME', 'Yosemite National Park'));\n\n// Load a secondary collection: power plants.\nvar powerPlants = ee.FeatureCollection('WRI/GPPD/power_plants');\n\n// Define a spatial filter, with distance 100 km.\nvar distFilter = ee.Filter.withinDistance({\n distance: 100000,\n leftField: '.geo',\n rightField: '.geo',\n maxError: 10\n});\n\n// Define a saveAll join.\nvar distSaveAll = ee.Join.saveAll({\n matchesKey: 'points',\n measureKey: 'distance'\n});\n\n// Apply the join.\nvar spatialJoined = distSaveAll.apply(primary, powerPlants, distFilter);\n\n// Print the result.\nprint(spatialJoined);\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Load a primary collection: protected areas (Yosemite National Park).\nprimary = ee.FeatureCollection('WCMC/WDPA/current/polygons').filter(\n ee.Filter.eq('NAME', 'Yosemite National Park')\n)\n\n# Load a secondary collection: power plants.\npower_plants = ee.FeatureCollection('WRI/GPPD/power_plants')\n\n# Define a spatial filter, with distance 100 km.\ndist_filter = ee.Filter.withinDistance(\n distance=100000, leftField='.geo', rightField='.geo', maxError=10\n)\n\n# Define a saveAll join.\ndist_save_all = ee.Join.saveAll(matchesKey='points', measureKey='distance')\n\n# Apply the join.\nspatial_joined = dist_save_all.apply(primary, power_plants, dist_filter)\n\n# Print the result.\ndisplay(spatial_joined)\n```\n\nNote that the previous example joins a `FeatureCollection` to another\n`FeatureCollection`. The `saveAll()` join sets a property\n(`points`) on each feature in the `primary` collection which\nstores a list of the points within 100 km of the feature. The distance of each point to\nthe feature is stored in the `distance` property of each joined point.\n\nSpatial joins can also be used to identify which features\nin one collection intersect those in another. For example, consider two feature\ncollections: a `primary` collection containing polygons representing the\nboundaries of US states, a `secondary` collection containing point locations\nrepresenting power plants. Suppose there is need to determine the number intersecting each\nstate. This can be accomplished with a spatial join as follows:\n\n### Code Editor (JavaScript)\n\n```javascript\n// Load the primary collection: US state boundaries.\nvar states = ee.FeatureCollection('TIGER/2018/States');\n\n// Load the secondary collection: power plants.\nvar powerPlants = ee.FeatureCollection('WRI/GPPD/power_plants');\n\n// Define a spatial filter as geometries that intersect.\nvar spatialFilter = ee.Filter.intersects({\n leftField: '.geo',\n rightField: '.geo',\n maxError: 10\n});\n\n// Define a save all join.\nvar saveAllJoin = ee.Join.saveAll({\n matchesKey: 'power_plants',\n});\n\n// Apply the join.\nvar intersectJoined = saveAllJoin.apply(states, powerPlants, spatialFilter);\n\n// Add power plant count per state as a property.\nintersectJoined = intersectJoined.map(function(state) {\n // Get \"power_plant\" intersection list, count how many intersected this state.\n var nPowerPlants = ee.List(state.get('power_plants')).size();\n // Return the state feature with a new property: power plant count.\n return state.set('n_power_plants', nPowerPlants);\n});\n\n// Make a bar chart for the number of power plants per state.\nvar chart = ui.Chart.feature.byFeature(intersectJoined, 'NAME', 'n_power_plants')\n .setChartType('ColumnChart')\n .setSeriesNames({n_power_plants: 'Power plants'})\n .setOptions({\n title: 'Power plants per state',\n hAxis: {title: 'State'},\n vAxis: {title: 'Frequency'}});\n\n// Print the chart to the console.\nprint(chart);\n```\n\nIn the previous example, note that the `intersects()` filter doesn't store\na distance as the `withinDistance()` filter does. The output should look\nsomething like Figure 1.\nFigure 1. Bar chart showing the number of power plants intersecting each US state."]]