Skip to content

Commit

Permalink
fix: bug in clusterByDistance() when nrow(tbl) < maxClusters
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathancallahan committed Aug 15, 2024
1 parent a4d2246 commit b835236
Show file tree
Hide file tree
Showing 64 changed files with 125 additions and 77 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Type: Package
Package: MazamaLocationUtils
Version: 0.4.3
Version: 0.4.4
Title: Manage Spatial Metadata for Known Locations
Authors@R: c(
person("Jonathan", "Callahan", email="[email protected]", role=c("aut","cre")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# MazamaLocationUtils 0.4.4

* Fixed bug in `clusterByDistance()` which could fail when incoming tibbles
had very few rows.

# MazamaLocationUtils 0.4.3

* Updated `location_getSingleElevation_USGS()` to use new USGS API.
Expand Down
23 changes: 20 additions & 3 deletions R/clusterByDistance.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,14 @@ clusterByDistance <- function(

# Perform clustering
for ( clusterCount in 1:maxClusters ) {
if ( nrow(tbl) < 2000 ) {
clusterObj <- cluster::pam(tbl[,c(lonVar,latVar)],clusterCount)

if ( nrow(tbl) <= clusterCount ) {
# NOTE: k = positive integer specifying the number of clusters, less than the number of observations.
clusterObj <- cluster::pam(tbl[,c(lonVar,latVar)], nrow(tbl) - 1)
} else if ( nrow(tbl) < 2000 ) {
clusterObj <- cluster::pam(tbl[,c(lonVar,latVar)], clusterCount)
} else {
clusterObj <- cluster::clara(tbl[,c(lonVar,latVar)],clusterCount, samples = 50)
clusterObj <- cluster::clara(tbl[,c(lonVar,latVar)], clusterCount, samples = 50)
}
clusterLats <- clusterObj$medoids[,latVar]
diameters <- 2 * clusterObj$clusinfo[,'max_diss'] # decimal degrees
Expand All @@ -141,6 +145,19 @@ clusterByDistance <- function(
radianClusterLats <- clusterLats * pi/180
meters <- diameters * (1 + cos(radianClusterLats))/2 * 111320
if ( max(meters) < clusterDiameter ) break

}

# NOTE: If we only had a very small table with widely separate locations,
# NOTE: we can end up never satisfying max(meters) < clusterDiameter. In this
# NOTE: case, every location represents a separate cluster
if ( nrow(tbl) <= clusterCount ) {
for ( i in 1:seq_len(nrow(tbl)) ) {
tbl$clusterLon <- as.numeric(tbl[[lonVar]][i])
tbl$clusterLat <- as.numeric(tbl[[latVar]][i])
tbl$clusterID <- i
}
return(tbl)
}

# Create the vector of deployment identifiers
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/Developer_Style_Guide.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/articles/MazamaLocationUtils.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pandoc: 3.1.2
pandoc: 3.1.1
pkgdown: 2.0.7
pkgdown_sha: ~
articles:
Developer_Style_Guide: Developer_Style_Guide.html
MazamaLocationUtils: MazamaLocationUtils.html
last_built: 2024-01-23T19:14Z
last_built: 2024-08-15T02:46Z

2 changes: 1 addition & 1 deletion docs/reference/LocationDataDir.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/MazamaLocationUtils.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/apiKeys.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified docs/reference/clusterByDistance-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/reference/clusterByDistance.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/coreMetadataNames.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/getLocationDataDir.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/reference/id_monitors_500.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/initializeMazamaSpatialUtils.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/location_createID.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/location_getCensusBlock.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/location_getOpenCageInfo.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/location_getSingleAddress_Photon.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/location_getSingleAddress_TexasAM.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/location_getSingleElevation_USGS.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/location_initialize.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b835236

Please sign in to comment.