create_spatial_lag
Format
create_spatial_lag(table, column, strategy='mean', result_table=None, key_column=None,
weights=None, weights_def=None, save_weights_as=None, spatial_col=None, crs=None)
Parameters
The parameters for this pre-defined function are described in the following table.
Parameter | Description |
---|---|
table |
Name of the spatial table. |
column |
The column of interest whose values are used to compute the spatial lag. |
strategy |
The strategy to be used to calculate the spatial lag; there are two possible choices: mean and median. |
result_table |
If specified, it calls the function
oml.create to store a pandas
DataFrame containing spatial lag in a table
with the specified name.
|
key_column |
If specified, the specified column is added to the
resulting DataFrame . Otherwise, a column with the
index of the DataFrame is attached to the
result.
|
weights |
Required when trying to use spatial weights already
stored in the data store. Internally it calls the function
olm.ds.load . The supported parameters are
ds_name and obj_name ,
indicating the data store name and object name,
respectively.
|
weights_def |
Required if the parameter weights
is not defined. Establishes the relationship between neighboring
locations.
This is passed as a json object specifying the type of the weights definition and its parameters. Each parameter is defined in detail in the API Reference documentation. The following lists the supported types and parameters:
|
save_weights_as |
Only used if weights_def is
defined. Specifies how the spatial weights are stored in the data
store. The value is a json file that determines the parameters of
oml.ds.save . The supported parameters are:
[ds_name, obj_name, overwrite_ds, append, overwrite_obj,
grantable, compression] . Some parameter names slightly
differ from those in the oml.ds.save function. The
parameter overwrite_obj is used to indicate whether
an already existing object should be replaced with the current
object.
|
spatial_col |
Specifies the column containing the geometries. The column can be specified in the table’s metadata. If not specified, the column name is retrieved from the table. |
crs |
Specifies the Coordinate Reference System. If not specified, it is inferred from the table. |
Example
The following code calculates the spatial lag of a specific column
according to given spatial weights. For each row, it calculates the average value of
a particular column from neighboring locations. It uses the
median_income
column and spatial weights from a datastore.
select *
from table(
pyqEval(
'{
"oml_connect": true,
"table": "oml_user.la_block_groups",
"key_column": "geoid",
"column": "median_income",
"weights": {"ds_name":"spatial", "obj_name": "la_bg_knn4"}
}',
'{ "geoid": "VARCHAR2(50)", "MEDIAN_INCOME_SLAG": "NUMBER" }',
'create_spatial_lag'
)
);
The result contains the average income from neighboring locations for
each row. Note that the index comes from the key_column
parameter,
which is the geoid
column in this case.