Skip to content

Commit

Permalink
add upload method
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Hardy committed Sep 15, 2016
1 parent 6d819e5 commit e760d27
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 207 deletions.
10 changes: 5 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ begin
namespace :spec do
desc 'Run unit tests'
RSpec::Core::RakeTask.new(:unit) do |t|
t.pattern = 'spec/unit/**/*_spec.rb'
t.pattern = 'spec/{unit,utils}/**/*_spec.rb'
end

desc 'Run functional tests which requires GeoServer running'
Expand All @@ -33,17 +33,17 @@ begin
end

# desc 'Run integration tests which requires GeoServer running and preloaded'
# RSpec::Core::RakeTask.new(:integration, :jetty_home, :jetty_port, :java_opts) do |t, args|
# RSpec::Core::RakeTask.new(:integration, :jetty_home, :jetty_port, :java_opts) do |t, args|
# t.pattern = 'spec/integration/**/*_spec.rb'
# require 'jettywrapper'
# jetty_params = {
# :jetty_home => args.jetty_home,
# :java_opts => [args.java_opts],
# :jetty_port => args.jetty_port,
# :java_opts => [args.java_opts],
# :jetty_port => args.jetty_port,
# :quiet => true,
# :startup_wait => 20
# }
#
#
# fail if Jettywrapper.wrap(jetty_params) do
# Rake::Task['spec:integration'].invoke
# end
Expand Down
97 changes: 70 additions & 27 deletions lib/rgeoserver/coveragestore.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@

require 'pathname'

module RGeoServer
# A coverage store is a source of spatial data that is raster based.
class CoverageStore < ResourceInfo
class CoverageStoreAlreadyExists < StandardError
def initialize(name)
@name = name
end

def message
"The CoverageStore '#{@name}' already exists and can not be replaced."
end
end

class DataTypeNotExpected < StandardError
def initialize(data_type)
@data_type = data_type
end

def message
"The CoverageStore does not not accept the data type '#{@data_type}'."
end
end

OBJ_ATTRIBUTES = {
:catalog => 'catalog',
:workspace => 'workspace',
:url => 'url',
:data_type => 'type',
:name => 'name',
:enabled => 'enabled',
:catalog => 'catalog',
:workspace => 'workspace',
:url => 'url',
:data_type => 'type',
:name => 'name',
:enabled => 'enabled',
:description => 'description'
}
}
OBJ_DEFAULT_ATTRIBUTES = {
:catalog => nil,
:workspace => nil,
:url => '',
:data_type => 'GeoTIFF',
:name => nil,
:enabled => 'true',
:catalog => nil,
:workspace => nil,
:url => '',
:data_type => 'GeoTIFF',
:name => nil,
:enabled => 'true',
:description=>nil
}
}
define_attribute_methods OBJ_ATTRIBUTES.keys
update_attribute_accessors OBJ_ATTRIBUTES

Expand All @@ -45,33 +66,33 @@ def self.member_xpath
end

def route
@@route % @workspace.name
@@route % @workspace.name
end

def update_params name_route = @name
def update_params name_route = @name
{ :name => name_route, :workspace => @workspace.name }
end

def message
builder = Nokogiri::XML::Builder.new do |xml|
xml.coverageStore {
xml.name @name
xml.name @name
xml.workspace {
xml.name @workspace.name
}
xml.enabled @enabled
xml.type_ @data_type if (data_type_changed? || new?)
xml.description @description if (description_changed? || new?)
xml.url @url if (url_changed? || new?)
xml.url @url if (url_changed? || new?) && !@url.nil
}
end
@message = builder.doc.to_xml
@message = builder.doc.to_xml
end

# @param [RGeoServer::Catalog] catalog
# @param [RGeoServer::Workspace|String] workspace
# @param [String] name
def initialize catalog, options
def initialize catalog, options
super(catalog)
_run_initialize_callbacks do
workspace = options[:workspace] || 'default'
Expand All @@ -84,11 +105,11 @@ def initialize catalog, options
end
@name = options[:name].strip
@route = route
end
end
end

def coverages
yield self.class.list Coverage, @catalog, profile['coverages'], {:workspace => @workspace, :coverage_store => self}, true
def coverages &block
self.class.list Coverage, @catalog, profile['coverages'] || [], {:workspace => @workspace, :coverage_store => self}, true, &block
end

# <coverageStore>
Expand All @@ -113,15 +134,15 @@ def coverages
def profile_xml_to_hash profile_xml
doc = profile_xml_to_ng profile_xml
h = {
'name' => doc.at_xpath('//name').text.strip,
'name' => doc.at_xpath('//name').text.strip,
'description' => doc.at_xpath('//description/text()').to_s,
'type' => doc.at_xpath('//type/text()').to_s,
'enabled' => doc.at_xpath('//enabled/text()').to_s,
'url' => doc.at_xpath('//url/text()').to_s,
'workspace' => @workspace.name # Assume correct workspace
}
doc.xpath('//coverages/atom:link[@rel="alternate"]/@href',
"xmlns:atom"=>"http://www.w3.org/2005/Atom" ).each{ |l|
doc.xpath('//coverages/atom:link[@rel="alternate"]/@href',
"xmlns:atom"=>"http://www.w3.org/2005/Atom" ).each{ |l|
h['coverages'] = begin
response = @catalog.do_url l.text
Nokogiri::XML(response).xpath('//name/text()').collect{ |a| a.text.strip }
Expand All @@ -132,5 +153,27 @@ def profile_xml_to_hash profile_xml
h
end

# @param [String] path - location of upload data
# @param [Symbol] upload_method -- only valid for :file
# @param [Symbol] data_type -- currently only supported for :geotiff
def upload path, upload_method = :file, data_type = :geotiff
raise CoverageStoreAlreadyExists, @name unless new?
raise DataTypeNotExpected, data_type unless [:geotiff].include? data_type

case upload_method
when :file then # local file that we post
local_file = Pathname.new(File.expand_path(path))
unless local_file.extname == '.tif' && local_file.exist?
raise ArgumentError, "GeoTIFF upload must be .tif file: #{local_file}"
end
puts "Uploading #{local_file.size} bytes from file #{local_file}..."

catalog.client["#{route}/#{name}/file.geotiff"].put local_file.read, :content_type => 'image/tiff'
refresh
else
raise NotImplementedError, "Unsupported upload method #{upload_method}"
end
self
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rgeoserver/utils/shapefile_info.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rgeo'
require 'rgeo/shapefile'
require 'zip/zip'
require 'zip'

module RGeoServer
class ShapefileInfo
Expand Down
1 change: 1 addition & 0 deletions rgeoserver.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |s|
s.add_dependency 'rgeo-shapefile', '~> 0.4.1'
s.add_dependency 'rubyzip'

s.add_development_dependency 'awesome_print'
s.add_development_dependency 'bundler'
s.add_development_dependency 'equivalent-xml'
s.add_development_dependency 'jettywrapper'
Expand Down
60 changes: 29 additions & 31 deletions spec/functional/catalog_spec.rb
Original file line number Diff line number Diff line change
@@ -1,71 +1,69 @@
require 'spec_helper'
require 'awesome_print'

describe RGeoServer::GeoServerUrlHelpers do

before(:each) do
@c = RGeoServer::Catalog.new
@w = @c.get_workspace 'druid'
@w_default = @c.get_default_workspace
ap({ :catalog => @c, :workspace_druid => @w, :workspace_default => @w_default }) if $DEBUG
end

describe RGeoServer::Catalog do
describe "Init" do
it "catalog" do
@c.config.include?(:url).should == true
@c.headers.include?(:content_type).should == true
end

it "workspace" do
@w.name.should == 'druid'
@w_default.name.should == @w.name
subject.config.include?(:url).should == true
subject.headers.include?(:content_type).should == true
end
end

describe "#url_for" do
it "simple" do
@c.respond_to?('url_for').should == true
subject.respond_to?('url_for').should == true
end
end

describe "Workspace" do
before(:each) do
@w = subject.get_workspace 'druid'
@w_default = subject.get_default_workspace
ap({ :catalog => subject, :workspace_druid => @w, :workspace_default => @w_default }) if $DEBUG
end

it "workspace" do
@w.name.should == 'druid'
@w_default.name.should == @w.name
end

it "#get_workspaces as array" do
@w_all = @c.get_workspaces
@w_all = subject.get_workspaces
@w_all.size.should > 0
end

it "#get_workspaces as block" do
@c.get_workspaces do |w|
subject.get_workspaces do |w|
w.name.length.should > 0
end
end
end

describe "Layers" do
it "#get_layers" do
@c.get_layers.size.should > 0
@c.get_layers.each do |l|
it "#each_layer" do
subject.each_layer.to_a.size.should > 0
subject.each_layer do |l|
# ap l.resource
end
end
end

describe "DataStore" do
it "#get_data_stores" do
@c.get_data_stores.size.should > 0
subject.get_data_stores.size.should > 0
end
end

describe "CoverageStore" do
it "#get_coverage_stores" do
@c.get_coverage_stores.size.should > 0
subject.get_coverage_stores.size.should > 0
end
end

describe "WMSStore" do
it "#get_wms_stores" do
@c.get_wms_stores.size.should > 0
subject.get_wms_stores.size.should > 0
end
end
end

end
Loading

0 comments on commit e760d27

Please sign in to comment.