Class: RGeoServer::DataStore

Inherits:
ResourceInfo show all
Defined in:
lib/rgeoserver/datastore.rb

Overview

A data store is a source of spatial data that is vector based. It can be a file in the case of a Shapefile, a database in the case of PostGIS, or a server in the case of a remote Web Feature Service.

Defined Under Namespace

Classes: DataStoreAlreadyExists, DataTypeNotExpected

Constant Summary collapse

OBJ_ATTRIBUTES =
{
  :workspace => 'workspace',
  :connection_parameters => "connection_parameters",
  :name => 'name',
  :data_type => 'type',
  :enabled => 'enabled',
  :description => 'description'
}
OBJ_DEFAULT_ATTRIBUTES =
{
  :workspace => nil,
  :connection_parameters => {},
  :name => nil,
  :data_type => :shapefile,
  :enabled => true,
  :description => nil
}
@@route =
"workspaces/%s/datastores"
@@root =
"dataStores"
@@resource_name =
"dataStore"

Instance Attribute Summary collapse

Attributes inherited from ResourceInfo

#catalog

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ResourceInfo

#clear, #create_method, #delete, list, #new?, #profile, #profile=, #profile_xml_to_ng, #refresh, #save, #to_s, update_attribute_accessors, #update_method, #update_params

Constructor Details

#initialize(catalog, options) ⇒ DataStore

Returns a new instance of DataStore.

Parameters:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rgeoserver/datastore.rb', line 99

def initialize catalog, options
  super({})
  _run_initialize_callbacks do
    @catalog = catalog
    workspace = options[:workspace] || 'default'
    if workspace.instance_of? String
      @workspace = catalog.get_workspace(workspace)
    elsif workspace.instance_of? Workspace
      @workspace = workspace
    else
      raise ArgumentError, "Not a valid workspace: #{workspace}"
    end

    @name = options[:name].strip
    @route = route
  end
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



46
47
48
# File 'lib/rgeoserver/datastore.rb', line 46

def message
  @message
end

Class Method Details

.member_xpathObject



64
65
66
# File 'lib/rgeoserver/datastore.rb', line 64

def self.member_xpath
  "//#{resource_name}"
end

.resource_nameObject



56
57
58
# File 'lib/rgeoserver/datastore.rb', line 56

def self.resource_name
  @@resource_name
end

.rootObject



52
53
54
# File 'lib/rgeoserver/datastore.rb', line 52

def self.root
  @@root
end

.root_xpathObject



60
61
62
# File 'lib/rgeoserver/datastore.rb', line 60

def self.root_xpath
  "//#{root}/#{resource_name}"
end

Instance Method Details

#featuretypes(&block) ⇒ Object



117
118
119
# File 'lib/rgeoserver/datastore.rb', line 117

def featuretypes &block
  self.class.list FeatureType, catalog, profile['featureTypes'] || [], {:workspace => @workspace, :data_store => self}, true, &block
end

#profile_xml_to_hash(profile_xml) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rgeoserver/datastore.rb', line 163

def profile_xml_to_hash profile_xml
  doc = profile_xml_to_ng profile_xml
  h = {
    "name" => doc.at_xpath('//name').text.strip,
    "description" => doc.at_xpath('//description/text()').to_s,
    "enabled" => doc.at_xpath('//enabled/text()').to_s,
    'type' => doc.at_xpath('//type/text()').to_s,
    "connection_parameters" => doc.xpath('//connectionParameters/entry').inject({}){ |x, e| x.merge(e['key']=> e.text.to_s) }
  }
  # XXX: assume that we know the workspace for <workspace>...</workspace>
  doc.xpath('//featureTypes/atom:link[@rel="alternate"]/@href',
            "xmlns:atom"=>"http://www.w3.org/2005/Atom" ).each do |l|
    h["featureTypes"] = begin
                          response = catalog.do_url l.text
                          # lazy loading: only loads featuretype names
                          Nokogiri::XML(response).xpath('//name/text()').collect{ |a| a.text.strip }
                        rescue RestClient::ResourceNotFound
                          []
                        end.freeze
  end
  h
end

#routeObject



68
69
70
# File 'lib/rgeoserver/datastore.rb', line 68

def route
  @@route % @workspace.name
end

#update_routeObject



72
73
74
# File 'lib/rgeoserver/datastore.rb', line 72

def update_route
  "#{route}/#{@name}"
end

#upload(path, upload_method = :file, data_type = :shapefile, publish = false) ⇒ Object

Parameters:

  • path (String)
    • location of upload data

  • upload_method (Symbol) (defaults to: :file)

    – flag for :file, :url, or :external

  • data_type (Symbol) (defaults to: :shapefile)

    – currently only :shapefile

  • publish (Boolean) (defaults to: false)

    – only valid for :file

Raises:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rgeoserver/datastore.rb', line 136

def upload path, upload_method = :file, data_type = :shapefile, publish = false
  ap({ :path => path, :upload_method => upload_method, :data_type => data_type, :publish => publish, :self => self}) if $DEBUG

  raise DataStoreAlreadyExists, @name unless new?
  raise DataTypeNotExpected, data_type unless [:shapefile].include? data_type

  ext = 'shp'
  case upload_method
  when :file then # local file that we post
    local_file = File.expand_path(path)
    unless local_file =~ %r{\.zip$} and File.exist? local_file
      raise ArgumentError, "Shapefile upload must be ZIP file: #{local_file}"
    end
    puts "Uploading #{File.size(local_file)} bytes from file #{local_file}..."

    catalog.client["#{route}/#{name}/file.#{ext}"].put File.read(local_file), :content_type => 'application/zip'
    refresh
  when :external then # remote file that we reference
    catalog.client["#{route}/#{name}/external.#{ext}"].put path, :content_type => 'text/plain'
  when :url then
    catalog.client["#{route}/#{name}/url.#{ext}"].put path, :content_type => 'text/plain'
  else
    raise NotImplementedError, "Unsupported upload method #{upload_method}"
  end
  self
end

#upload_external(remote_file, publish = {}) ⇒ Object



124
125
126
127
# File 'lib/rgeoserver/datastore.rb', line 124

def upload_external remote_file, publish = {}
  puts "Uploading external file #{remote_file} #{publish}"
  upload remote_file, :external, data_type, publish
end

#upload_file(local_file, publish = {}) ⇒ Object



121
122
123
# File 'lib/rgeoserver/datastore.rb', line 121

def upload_file local_file, publish = {}
  upload local_file, :file, data_type, publish
end

#upload_url(url, publish = {}) ⇒ Object



128
129
130
# File 'lib/rgeoserver/datastore.rb', line 128

def upload_url url, publish = {}
  upload url, :url, data_type, publish
end