Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- title case for spatial names
  • Loading branch information
temi committed Nov 5, 2024
1 parent 8c314f0 commit 4c32c0b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
20 changes: 18 additions & 2 deletions grails-app/services/au/org/ala/ecodata/SpatialService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import grails.core.GrailsApplication
import grails.plugin.cache.Cacheable
import groovy.json.JsonParserType
import groovy.json.JsonSlurper
import org.apache.commons.lang.WordUtils
import org.locationtech.jts.geom.*
import org.locationtech.jts.io.WKTReader

import java.util.regex.Matcher
import java.util.regex.Pattern

import static ParatooService.deepCopy
/**
* The SpatialService is responsible for:
Expand Down Expand Up @@ -449,8 +451,22 @@ class SpatialService {
synonymLookupTable[facetName] = synonymTable?.collectEntries { k, List v -> v.collectEntries { v1 -> [(v1.toLowerCase()): k] } }
}

synonymLookupTable[facetName]?.get(name) ?: WordUtils.capitalize(name)
synonymLookupTable[facetName]?.get(name) ?: titleCase(name)
}
}

String titleCase(String name) {
Pattern pattern = Pattern.compile("\\b\\w")
Matcher matcher = pattern.matcher(name.toLowerCase())
StringBuffer capitalizedName = new StringBuffer()

// Capitalize each matched letter and append it to the result
while (matcher.find()) {
matcher.appendReplacement(capitalizedName, matcher.group().toUpperCase())
}
matcher.appendTail(capitalizedName)

return capitalizedName.toString()
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/test/groovy/au/org/ala/ecodata/SpatialServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ class SpatialServiceSpec extends Specification implements ServiceUnitTest<Spatia
1 * webService.get("/ws/shapes/wkt/123") >> getShape2()
1 * webService.get("/ws/shapes/wkt/456") >> getBoundaryShape()
}

def "titleCase should capitalize the first letter of each word"() {
expect:
service.titleCase("new south wales") == "New South Wales"
service.titleCase("australian capital territory") == "Australian Capital Territory"
service.titleCase("act") == "Act"
service.titleCase("nSw") == "Nsw"
}

private Geometry getBoundaryShape() {
return GeometryUtils.geoJsonMapToGeometry(mapper.readValue('{' +
Expand Down

0 comments on commit 4c32c0b

Please sign in to comment.