最近接(Near) (解析)

ライセンス レベル:BasicStandardAdvanced

サマリ

検索範囲内で、入力フィーチャの各フィーチャから近接フィーチャの最近接フィーチャまでの距離を特定します。

ジオプロセシング ツールでの近接性の計算方法に関する詳細

Finding near feature by geometry type.

使用法

構文

Near_analysis (in_features, near_features, {search_radius}, {location}, {angle})
パラメータ説明データ タイプ
in_features

ポイント、ポリライン、ポリゴン、またはマルチポイント タイプの入力フィーチャ。

Feature Layer
near_features
[near_features,...]

入力フィーチャから最近接フィーチャを検出するために使用する値です。近接フィーチャを 1 つ以上入力できます。各エントリは、ポイント、ポリライン、ポリゴン、またはマルチポイント タイプになります。近接フィーチャを複数指定した場合、最近接フィーチャを含むソース フィーチャクラスのパスを格納する新しいフィールド NEAR_FC が入力テーブルに追加されます。

Feature Layer
search_radius
(オプション)

近接フィーチャを検索するための半径を指定します。この半径内に存在する近接フィーチャから最近接フィーチャが求められます。値を何も指定しない場合は、デフォルト(空の)半径が使用されます。つまり、すべての近接フィーチャが計算対象となります。デフォルトの検索範囲の単位は入力フィーチャの座標系の単位です。この単位は他の単位に変更できます。ただし、これは入力フィーチャの座標系の単位に基づいて設定される NEAR_DIST の単位に影響を与えません。

Linear unit
location
(オプション)

最近接フィーチャの位置を表す X 座標と Y 座標を、それぞれ新しい NEAR_X フィールドと NEAR_Y フィールドに書き込むかどうかを指定します。

  • NO_LOCATION最近接位置の X 座標と Y 座標が書き込まれません。これがデフォルトです。
  • LOCATION最近接位置の X 座標と Y 座標が NEAR_X フィールドと NEAR_Y フィールドに書き込まれます。
Boolean
angle
(オプション)

近接する角度値(10 進表記)が計算され、新しい NEAR_ANGLE フィールドに書き込まれます。近接角度は、X 軸(水平軸)から、入力フィーチャとその最近接フィーチャをつなぐ直線までの角度を指しており、0 ~ 180 度または 0 ~ -180 度の範囲になります。0 度が東、90 度が北、180(-180)度が西、-90 度が南です。

  • NO_ANGLE近接角度値が書き込まれません。これがデフォルトです。
  • ANGLE近接角度値が NEAR_ANGLE フィールドに書き込まれます。
Boolean

コードのサンプル

Near(最近接)の例 1(Python ウィンドウ)

次の Python ウィンドウ スクリプトは、イミディエイト モードで Near(最近接)関数を使用する方法を示しています。

import arcpy
arcpy.env.workspace = "C:/data/city.gdb" 

## find the nearest road from each house
arcpy.Near_analysis('houses', 'roads')
Near(最近接)の例 2(スタンドアロン Python スクリプト)

次の Python スクリプトは、スタンドアロン スクリプトで最近接関数を使用する方法を示しています。

# Name: Near.py
# Description: Finds nearest features from input feature class to near feature class.

import arcpy
from arcpy import env

# Set workspace environment
env.workspace = "C:/data/city.gdb"

try:
    # set local variables
    in_features = "houses"
    near_features = "parks"
    
    # find features only within search radius
    search_radius = "5000 Meters"
    
    # find location nearest features
    location = "LOCATION"
    
    # avoid getting angle of neares features
    angle = "NO_ANGLE"
    
    # execute the function
    arcpy.Near_analysis(in_features, near_features, search_radius, location, angle)
    
    # get geoprocessing messages
    print arcpy.GetMessages()

except arcpy.ExecuteError:
    print arcpy.GetMessages(2)
    
except Exception as ex:
    print ex.args[0]
Near(最近接)の例 3(スタンドアロン Python スクリプト)

次の Python スクリプトは、近接角度を方位角に変換する方法を示しています。

# Name: near_angle_to_azimuth.py

import arcpy

# Near tool does not calculate angle in azimuths
# This script, using the output of the tool, converts the angles
# to azimuth angles.

in_table = r"C:/data/city.gdb/near_table"
    
angles = arcpy.da.SearchCursor(in_table, ["NEAR_ANGLE"])

azimuth_angles = []

with angles as rows:
    for row in rows:
        angle = row[0]
        if angle <= 180 and angle > 90:
            azimuth_angles.append(360.0 - (angle - 90))
        else:
            azimuth_angles.append(abs(angle - 90))

# Use these azimuth angles as necessary.
Near(最近接)の例 4(スタンドアロン Python スクリプト)

最近接(Near)ツールの派生出力の後処理を示しています。このスクリプトは、最近接フィーチャごとに、最も近い入力フィーチャを検出します。

# Name: features_closest_to_input.py

"""
    This script finds, for each input feature, a list of near feature it is closest to.
    If near features 6, 7, 10 are closest to input feature 3 then the 
    resulting dictionary will have a list [6, 7, 10] as value for key 3
"""

import os
import arcpy

in_fc = r"C:\data\cities.gdb\cities_many"

# create a dictionary to hold the list of near ids for each input id
nearest_dict = dict()

with arcpy.da.SearchCursor(in_fc, ["OID@", "NEAR_FID"]) as rows:
    for row in rows:
        nearest_id = row[0]  # get OID value for that row
        input_id = row[1]    # get NEAR_FID value 
        
        if input_id in nearest_dict:
            # if a dict key already exists, append near id to value list for that key
            nearest_dict[input_id].append(nearest_id)
        else:
            # if the key does not exist then create a new list with near id
            # and add it to the dictionary
            nearest_dict[input_id] = [nearest_id]
            
print nearest_dict

# output will look like:
# {1: [13, 28], 2: [2, 9, 14, 20, 22], 3: [11, 12, 24, 25]}

環境

関連トピック

ライセンス情報

ArcGIS for Desktop Basic: ×
ArcGIS for Desktop Standard: ×
ArcGIS for Desktop Advanced: ○
9/14/2013