Skip to content

Commit c5c3585

Browse files
committed
add basic feature object
1 parent 855f54a commit c5c3585

3 files changed

Lines changed: 87 additions & 19 deletions

File tree

lib/geoscript/feature.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require 'feature/field'
2-
require 'feature/schema'
2+
require 'feature/schema'
3+
require 'feature/feature'

lib/geoscript/feature/feature.rb

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,53 @@
1+
java_import org.geotools.feature.simple.SimpleFeatureBuilder
2+
ogFeature = org.opengis.feature.Feature
3+
14
module GeoScript
25
module Feature
36
class Feature
4-
7+
def initialize(attrs = nil, id = nil, schema = nil, feature = nil)
8+
if attrs
9+
unless schema
10+
if attrs.instance_of? Hash
11+
schema_attrs = []
12+
attrs.each do |k, v|
13+
schema_attrs << {name: k, type: v.to_java.java_class}
14+
end
15+
@schema = Schema.new('feature', schema_attrs)
16+
elsif attrs.instance_of? Array
17+
raise 'Attributes may only be given as Array if schema is specified'
18+
end
19+
else
20+
@schema = schema
21+
end
22+
23+
if attrs.instance_of? Array
24+
attrs_hash = {}
25+
attrs.each_with_index {|v, i| attrs_hash[@schema.fields[i].name] = v}
26+
attrs = attrs_hash
27+
end
28+
29+
sfb = SimpleFeatureBuilder.new @schema.feature_type
30+
attrs.each {|k, v| sfb.set(k, v)}
31+
@feature = sfb.build_feature id
32+
elsif feature
33+
@feature = feature
34+
@schema = schema ? schema : GeoScript::Feature::Schema.new(feature.feature_type)
35+
else
36+
raise 'No attributes specified for feature'
37+
end
38+
end
39+
40+
def get_id
41+
@feature.identifier.to_s
42+
end
43+
44+
def get_geom
45+
@feature.default_geometry
46+
end
47+
48+
def set_geom(geom)
49+
@feature.default_geometry = geom
50+
end
551
end
652
end
753
end

lib/geoscript/feature/schema.rb

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,64 @@ def initialize(name = nil, fields = [], type = nil, uri = 'http://geoscript.org/
1818
fields.each do |field|
1919
if field.instance_of? GeoScript::Feature::Field
2020
name, type, proj = field.name, field.type, field.proj
21-
else
22-
if field.instance_of? Hash
21+
elsif field.instance_of? Hash
22+
if field[:name] && field[:type]
23+
name, type = field[:name], field[:type]
24+
else
2325
name, type = field.keys.first, field.values.first
24-
elsif field.instance_of? Array
25-
name, type = field[0], field[1]
26-
27-
if type.kind_of? GeoScript::Geom
28-
proj = GeoScript::Projection.new(field[2]) if field[2]
29-
else
30-
raise 'Invalid type specified. Must be of type GeoScript::Geom::*'
31-
end
26+
end
27+
28+
if type.name.match /GeoScript::Geom/
29+
proj = GeoScript::Projection.new(field[:proj]) if field[:proj]
30+
end
31+
elsif field.instance_of? Array
32+
name, type = field[0], field[1]
33+
34+
if type.name.match /GeoScript::Geom/
35+
proj = GeoScript::Projection.new(field[2]) if field[2]
3236
end
3337
end
3438
type_builder.crs proj if proj
35-
type_builder.add name, type.java_class
36-
@feature_type = type_builder.build_feature_type
39+
type_builder.add name, type.to_java.java_class
40+
p type_builder.attributes
3741
end
42+
@feature_type = type_builder.build_feature_type
3843
else
39-
raise "No fields specified for feature type: #{type}"
44+
if !name && fields.empty?
45+
raise "No name specified & No fields specified for type: #{type}"
46+
elsif fields.empty?
47+
raise "No fields specified for type: #{type}" if fields.empty?
48+
elsif !name
49+
raise 'No name specified'
50+
end
4051
end
4152
end
4253

43-
def get_name
54+
def name
4455
@feature_type.name.local_part
4556
end
4657

47-
def get_uri
58+
def uri
4859
@feature_type.name.namespaceURI
4960
end
5061

51-
def get_proj
62+
def proj
5263
crs = @feature_type.coordinate_reference_system
5364
GeoScript::Projection.new crs if crs
5465
end
5566

67+
def fields
68+
fields = []
69+
70+
@feature_type.attribute_descriptors.each do |ad|
71+
fields << self.get(ad.local_name)
72+
end
73+
74+
fields
75+
end
76+
5677
def get(name)
57-
ad = @feature_type.get_descriptor name
78+
@feature_type.get_descriptor name
5879
end
5980
end
6081
end

0 commit comments

Comments
 (0)