-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Edit wms datasets #66
Open
chrowe
wants to merge
6
commits into
main
Choose a base branch
from
edit-wms-datasets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−5
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1392dfb
Code to delete test dataset
chrowe 1e281e8
New script to edit wms datasets and associated layers
chrowe 60cb5c7
clone_children=True + new line at end of file
chrowe 896603c
Use one env variable + update deletion code
chrowe 1e39e32
Add jupyter extentions to devcontainer
chrowe 80c49cb
Keep original app id
chrowe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import os | ||
import requests | ||
import json | ||
|
||
API_TOKEN = os.getenv('RW_API_KEY') | ||
|
||
def create_headers(): | ||
return { | ||
'content-type': "application/json", | ||
'authorization': "{}".format(os.getenv('apiToken')), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the same format as the other update? |
||
} | ||
|
||
# Start with a dataset you cloned to make sure everything works | ||
cloned_dataset = [''] | ||
cloned_layer = [''] | ||
|
||
# Add your real datasets here. Don't uncomment until you have tested. | ||
datasets = ['', ''] | ||
|
||
# Choose the field you want to find and replace in, the value to fine, and what to replace it with | ||
dataset_field = 'connectorUrl' | ||
layer_field = 'layerConfig' | ||
find = '' | ||
replace = '' | ||
|
||
# Don't actually to anything yet | ||
dry_run = True | ||
|
||
# Use test dataset | ||
test_run = True | ||
|
||
if test_run: | ||
datasets = cloned_dataset | ||
|
||
# Print out what we are doing | ||
print(f'Replacing "{find}" with "{replace}" in `{dataset_field}`.') | ||
|
||
|
||
# Loop through datasets to find and replace values | ||
for dataset_id in datasets: | ||
current_url = f'https://api.resourcewatch.org/v1/dataset/{dataset_id}/?includes=layer' | ||
print(f'DATASET URL: {current_url}') | ||
with requests.get(current_url) as r: | ||
if r.ok: | ||
ds = json.loads(r.content) | ||
else: | ||
raise ValueError(f'API request failed: {current_url}') | ||
assert 'data' in ds | ||
|
||
name = ds['data']['attributes']['name'] | ||
print(f'DATASET NAME: {name}') | ||
|
||
old_value = ds['data']['attributes'][dataset_field] | ||
new_value = old_value.replace(find, replace) | ||
|
||
find_count = old_value.count(find) | ||
replace_count = new_value.count(replace) | ||
|
||
print(f'\nOLD VALUE: {old_value}') | ||
print(f'\nNEW VALUE: {new_value}') | ||
|
||
print(f'\nStarted with {find_count} instances of "{find}" and ended up with {replace_count} instances of "{replace}".') | ||
|
||
update = json.dumps({dataset_field: new_value}) | ||
|
||
if dry_run == False: | ||
with requests.patch(current_url, headers=create_headers(), data=update) as r: | ||
if r.ok: | ||
print('\nDONE') | ||
else: | ||
raise ValueError(f'API request failed: {current_url}') | ||
assert 'data' in ds | ||
else: | ||
print('\nDRY RUN') | ||
|
||
|
||
|
||
|
||
# Layers | ||
layers = [item["id"] for item in ds['data']['attributes']['layer']] | ||
if test_run: | ||
layers = cloned_layer | ||
|
||
for layer_id in layers: | ||
current_url = f'https://api.resourcewatch.org/v1/dataset/{dataset_id}/layer/{layer_id}' | ||
print(f'LAYER URL: {current_url}') | ||
with requests.get(current_url) as r: | ||
if r.ok: | ||
ly = json.loads(r.content) | ||
else: | ||
raise ValueError(f'API request failed: {current_url}') | ||
assert 'data' in ly | ||
|
||
name = ly['data']['attributes']['name'] | ||
print(f'LAYER NAME: {name}') | ||
|
||
old_value = ly['data']['attributes']['layerConfig'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should replace |
||
|
||
#Convert to string and replace | ||
old_str = json.dumps(old_value) | ||
find_count = old_str.count(find) | ||
|
||
new_str = old_str.replace(find, replace) | ||
replace_count = new_str.count(replace) | ||
|
||
#Get obj back with replacement | ||
new_value = json.loads(new_str) | ||
|
||
print(f'\nOLD VALUE: {old_value}') | ||
print(f'\nNEW VALUE: {new_value}') | ||
|
||
print(f'\nStarted with {find_count} instances of "{find}" and ended up with {replace_count} instances of "{replace}".') | ||
|
||
update = json.dumps({layer_field: new_value}) | ||
|
||
if dry_run == False: | ||
with requests.patch(current_url, headers=create_headers(), data=update) as r: | ||
if r.ok: | ||
print('\nDONE') | ||
else: | ||
raise ValueError(f'API request failed: {current_url}') | ||
assert 'data' in ds | ||
else: | ||
print('\nDRY RUN') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This edit_dataset script is not on the same level as the other edit_layer scripts. It should be the same for WMS/GEE/Carto to update any dataset fields. The differences are in the layer fields. I'm thinking about creating two scripts, one for updating dataset fields (could be used for all datasets), and the other for updating WMS layer fields. What do you think?