Skip to content
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

sbtools get #272

Merged
merged 9 commits into from
Jul 18, 2017
Merged

sbtools get #272

merged 9 commits into from
Jul 18, 2017

Conversation

lindsayplatt
Copy link
Contributor

Fixes for #114.

@lindsayplatt
Copy link
Contributor Author

lindsayplatt commented Jul 11, 2017

Still working on item_exists, identifier_exists, as.sbitem, and item_get_wfs.

Copy link
Member

@aappling-usgs aappling-usgs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggested a few edits. i created a new issue for figuring out item_get_wfs...you can wait for some work on that one or merge this one in the meantime; just leave the lesson issue open.

credList <- readRDS(sbCreds)
un <- rawToChar(credList$username)
pw <- rawToChar(credList$password)
sbtools::authenticate_sb(un, pw)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like a good solution already, but is secrets the way of the future? you mentioned that you and JW got it working for you...are you ready to switch over here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's ready thanks to the windows complications. I'll make an issue for later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #277

is_logged_in()
```

Each user has a specific ScienceBase id associated with their account. The user ids can be used to inspect what top-level items saved under your account (discussed in next section). To determine your user id, use the function `user_id` in an authenticated session. No arguments are necessary.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know the function is user_id, but it bothers me to call a user's top level item an id. what about home item?

Each user has a home ScienceBase item associated with their account. You can inspect the items and
 files attached to your home item (discussed in next section) and even add new items and files
 (discussed in next lesson). To determine the ScienceBase ID of your home item, use the function
 `user_id` in an authenticated session. No arguments are necessary.

or maybe personal if you don't like home?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think home item works, and that really helps clear up some of the initial confusion I had with working on this function. I like it 👍

user_id()
```

When you're done with your session, you can actively logout using the `session_logout`. No arguments are required. If you do not do this, you will be automatically logged out after a certain amount of time or when you close R.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the session_logout" -> "session_logout" or "the session_logout function"


## Inspect and download items

The first inspection step for ScienceBase items is to determine if the item even exists. To do this, use the function `identifier_exists`. The only required argument is `sb_id` which can be either a character string of the item id or an `sbitem`. It will return a logical to indicate if the item exists or not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on http://www.quickanddirtytips.com/education/grammar/if-versus-whether, i'd recommend "It will return a logical to indicate whether the item exists or not."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📖 learning so much!


ScienceBase items can be described by alternative identifiers, e.g. digital object identifiers, IPDS codes, etc. They are defined on ScienceBase with a scheme, type, and key. For examples of identifiers, see the "Additional Information | Identifiers" section of [Differential Heating](https://www.sciencebase.gov/catalog/item/580587a2e4b0824b2d1c1f23).

You can use the function `item_exists` to check whether or not a scheme-type-key tuple already exists. The function has three required arguments - `scheme`, `type`, and `key`. Note that the table of alternative identifiers on ScienceBase is in a different order than this function accepts. On ScienceBase: type, scheme, key. For `item_exists`: scheme, type, key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good tip!

item_exists(scheme = "State Inventory", type = "UniqueKey", key = "1234")
```

You can create sbitems from just the ScienceBase id. To do this use `as.sbitem`. *why you would use it*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you could safely leave out as.sbitem entirely and just show item_get here instead. see comment on item_get below.

ex_id_parent$title
```

Now, let's see if this item has any children by using the `item_list_children` function. Notice that this function says "list" and not "get" as the previous one did. Functions with "list" only return a few fields associated with each item. Functions with "get" are pulling down all available information, including files, associated with an item.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i really like that you've attempted a high-level explanation here. that said, i don't think item_get_parent and item_get_fields actually pull down files, do they? maybe only item_get_wfs (if and when we get that one to work).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hmmm good point. I think I was going for the fact that there is more info. What's your take? Any higher-level difference here worth noting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well...hmm...no, i'm not seeing any consistent patterns here...

  • item_list_children returns a list of sbitems
  • item_list_files returns a data.frame
  • item_get_parent returns a single sbitem
  • item_get_fields returns a list of info or drops from list to vector
  • item_get_wfs returns an sp object, e.g., SpatialPolygonsDataFrame
  • item_get returns a single sbitem

you could almost claim that item_get* expects one of each of whatever it's looking for, except that item_get_fields(x, 'identifiers') can return more than one set of identifiers, so...still nope.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll just leave this out completely.

xml2::read_xml(ex_id_files$url[1])
```

You can also inspect specific metadata fields of ScienceBase items. To do this, use the `item_get_fields` function. This function requires a second argument to the item id called `fields` that is a character vector of the fields you want to retrieve. See the [developer documentation for a SB item model](https://my.usgs.gov/confluence/display/sciencebase/ScienceBase+Item+Core+Model) for a list of potential fields. You can also use the argument `drop` to indicate that if only one field is requested, the object returned remains a list (`drop=FALSE`) or becomes a vector (`drop=TRUE`). The default is `drop=TRUE`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 for the link

If a field is empty, it will return `NULL`.

```{r sbtools-fields-empty}
# request a nonexistent fields
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"request a nonexistent fields" -> "request nonexistent fields"

names(ex_id_item)
```

## Web feature services to visualize spatial data???
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah...i'll work on this next

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, @lindsaycarr, my training-oriented notes are at #276 and i made a new sbtools issue at DOI-USGS/sbtools#244. it won't be super impressive, but I did identify three sb_ids whose WFSes can be retrieved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per luke's comments on DOI-USGS/sbtools#244, you could introduce item_get_wfs sorta like this: "the developers thought this could be a cool feature. here's what it might look like. the developers didn't want to invest too much if there wouldn't be demand, but if you'd use it a lot, submit or thumbs-up a GitHub issue"

@lindsayplatt
Copy link
Contributor Author

@aappling-usgs could you just take a quick look at my stab for content on item_get_wfs. I think that's good enough for now, and we can update later with #276

## Web feature services to visualize spatial data???
## Web feature services to visualize spatial data

This function allows you to pull down web feature services (WFS) data from ScienceBase. Note that this is not the most robust function. The developers thought this could be a cool feature, but didn't want to invest too much time if there wouldn't be demand. If you'd use it a lot, visit the [`sbtools` GitHub page](https://github.com/USGS-R/sbtools/issues) and let the developers know through a new issue or "thumbs-up" an existing, related issue.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@aappling-usgs aappling-usgs merged commit 0885ce9 into USGS-R:master Jul 18, 2017
@lindsayplatt lindsayplatt deleted the sbtools-get branch August 9, 2017 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants