Skip to content

Commit b6a962c

Browse files
committed
Support file names or string urls when loading Rasters
1 parent 1131eac commit b6a962c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/main/groovy/geoscript/layer/Format.groovy

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,27 @@ class Format {
230230
* @return A Format
231231
*/
232232
static Format getFormat(Object input) {
233+
// Support file names and string urls
234+
if (input instanceof String) {
235+
String str = input as String
236+
boolean isFileOrUrl = ['tif','png','jpg','jpeg','gif','arx','sid','asc','nc'].find { String ext ->
237+
str.endsWith(ext)
238+
}
239+
if (isFileOrUrl) {
240+
boolean isUrl = false
241+
try {
242+
URL url = new URL(str)
243+
isUrl = true
244+
} catch(MalformedURLException ex) {
245+
// Do nothing, just means that it must be a file
246+
}
247+
if (isUrl) {
248+
input = new URL(str)
249+
} else {
250+
input = new File(str)
251+
}
252+
}
253+
}
233254
if(input instanceof File) {
234255
File file = input as File
235256
if (file.exists()) {
@@ -280,7 +301,7 @@ class Format {
280301
AbstractGridFormat format = GridFormatFinder.findFormat(input);
281302
if (format == null || format instanceof UnknownFormat) {
282303
return null
283-
}else{
304+
} else {
284305
return new Format(format, input)
285306
}
286307
}

src/test/groovy/geoscript/layer/FormatTestCase.groovy

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package geoscript.layer
22

3+
import geoscript.geom.Geometry
34
import org.junit.Test
45
import static org.junit.Assert.*
56

@@ -24,13 +25,26 @@ class FormatTestCase {
2425
assertNotNull(format.stream)
2526
assertTrue(format instanceof NetCDF)
2627

27-
// New file
28+
// New png file
2829
file = new File("doesnotexist.png")
2930
format = Format.getFormat(file)
3031
assertNotNull(format)
3132
assertNotNull(format.stream)
3233
assertTrue(format instanceof WorldImage)
3334

35+
// New tif File
36+
file = new File("doesnotexist.tif")
37+
format = Format.getFormat(file)
38+
assertNotNull(format)
39+
assertNotNull(format.stream)
40+
assertTrue(format instanceof GeoTIFF)
41+
42+
// New tif File name
43+
format = Format.getFormat("doesnotexist.tif")
44+
assertNotNull(format)
45+
assertNotNull(format.stream)
46+
assertTrue(format instanceof GeoTIFF)
47+
3448
// Not a Raster form a new file
3549
file = new File("states.shp")
3650
format = Format.getFormat(file)

0 commit comments

Comments
 (0)