Skip to content

Commit

Permalink
lots of testing, very small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Kaufman committed Jul 23, 2018
1 parent b850fc3 commit 12a8c68
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
12 changes: 6 additions & 6 deletions R/feature_helpers.R
Expand Up @@ -352,7 +352,7 @@ get_one_bound_feature = function(xy){
polsby = (4 * pi * dist_area)/(dist_perim * dist_perim)

if(dist_area > hull_area | dist_area > bbox_area){
dist_area = correct_for_holes(orig_xy)
dist_area = correct_for_holes(orig_xy, dist_area)
}

return(c(hull = dist_area/hull_area, bbox = dist_area/bbox_area,
Expand All @@ -362,19 +362,19 @@ get_one_bound_feature = function(xy){
orig_area = dist_area, district_perim = dist_perim))
}

correct_for_holes = function(orig_xy){ # this input is a list of xy coords, one for each polygon
correct_for_holes = function(orig_xy, dist_area){ # this input is a list of xy coords, one for each polygon
# expand.grid on the list of polygons
exgrid = expand.grid(1:length(orig_xy), 1:length(orig_xy))
exgrid = exgrid[exgrid$Var1 != exgrid$Var2,]

# figure out which, if any, is a subset of any of the others using gContains
idx = which(sapply(1:length(exgrid),
FUN=function(x) gContains(SpatialPoints(orig_xy[[exgrid[i,1]]], CRS("+proj=longlat +datum=WGS84")),
SpatialPoints(orig_xy[[exgrid[i,2]]], CRS("+proj=longlat +datum=WGS84")))))
FUN=function(x) gContains(SpatialPolygons(list(Polygons(list(Polygon(orig_xy[[exgrid[x,1]]])),1))),
SpatialPolygons(list(Polygons(list(Polygon(orig_xy[[exgrid[x,2]]])),1))))))
#print(paste0("Fixing ", length(idx), " orphaned holes."))
# subtract that area
if(len(idx) != 0){
dist_area_new = sum(sapply(xy, FUN=function(x) geosphere::areaPolygon(x)/1000000)) - 2*geosphere::areaPolygon(xy[[idx]])
if(length(idx) != 0){
dist_area_new = sum(sapply(orig_xy, FUN=function(x) geosphere::areaPolygon(x)/1000000)) - 2*geosphere::areaPolygon(orig_xy[[idx]])/1000000
} else {
dist_area_new = dist_area
}
Expand Down
7 changes: 3 additions & 4 deletions vignettes/build_models.R
@@ -1,7 +1,7 @@
## This script generates the models used in the application
## First it generates features, then it builds models

setwd("D:/GitHub/compactness_software/compactness/data")
setwd("D:/GitHub/compactness_software/data")

## Required libraries
library(sp)
Expand All @@ -21,11 +21,10 @@ library(randomForest)
library(e1071)

source("../R/generate_features.R")
source("../R/bounding_geometry.R")
source("../R/read_shapefiles.R")
source("../R/harris_variations.R")
source("../R/feature_helpers.R")

load("D:/GitHub/compactness_software/compactness/data/training_data.RData")
load("D:/GitHub/compactness_software/data/training_data.RData")

# Load training labels with old features
df = do.call(rbind, mylist)
Expand Down

0 comments on commit 12a8c68

Please sign in to comment.