Skip to content

Commit f3a3a0b

Browse files
committed
feature class works without specified schema
1 parent c5c3585 commit f3a3a0b

4 files changed

Lines changed: 92 additions & 100 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
.bundle
33
Gemfile.lock
44
.rbenv-version
5+
.rvmrc
56
.DS_Store
67
target/

lib/geoscript/feature/feature.rb

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,50 @@
22
ogFeature = org.opengis.feature.Feature
33

44
module GeoScript
5-
module Feature
6-
class Feature
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'
5+
class Feature
6+
def initialize(attrs = nil, id = nil, schema = nil, feature = nil)
7+
if attrs
8+
unless schema
9+
if attrs.instance_of? Hash
10+
schema_attrs = []
11+
attrs.each do |k, v|
12+
schema_attrs << {name: k, type: v.to_java.java_class}
1813
end
19-
else
20-
@schema = schema
14+
@schema = GeoScript::Schema.new('feature', schema_attrs)
15+
elsif attrs.instance_of? Array
16+
raise 'Attributes may only be given as Array if schema is specified'
2117
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)
3518
else
36-
raise 'No attributes specified for feature'
19+
@schema = schema
3720
end
38-
end
3921

40-
def get_id
41-
@feature.identifier.to_s
42-
end
22+
if attrs.instance_of? Array
23+
attrs_hash = {}
24+
attrs.each_with_index {|v, i| attrs_hash[@schema.fields[i].name] = v}
25+
attrs = attrs_hash
26+
end
4327

44-
def get_geom
45-
@feature.default_geometry
28+
sfb = SimpleFeatureBuilder.new @schema.feature_type
29+
attrs.each {|k, v| sfb.set(k, v)}
30+
@feature = sfb.build_feature id
31+
elsif feature
32+
@feature = feature
33+
@schema = schema ? schema : GeoScript::Schema.new(feature.feature_type)
34+
else
35+
raise 'No attributes specified for feature'
4636
end
37+
end
4738

48-
def set_geom(geom)
49-
@feature.default_geometry = geom
50-
end
39+
def id
40+
@feature.identifier.to_s
41+
end
42+
43+
def geom
44+
@feature.default_geometry
45+
end
46+
47+
def set_geom(geom)
48+
@feature.default_geometry = geom
5149
end
5250
end
5351
end

lib/geoscript/feature/field.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module GeoScript
2-
module Feature
3-
Field = Struct.new(:name, :type, :proj)
4-
end
2+
Field = Struct.new(:name, :type, :proj)
53
end

lib/geoscript/feature/schema.rb

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,74 @@
44
java_import org.geotools.feature.simple.SimpleFeatureTypeBuilder
55

66
module GeoScript
7-
module Feature
8-
class Schema
9-
include GeoScript::Feature
7+
class Schema
8+
attr_accessor :feature_type
109

11-
attr_accessor :feature_type
12-
13-
def initialize(name = nil, fields = [], type = nil, uri = 'http://geoscript.org/feature')
14-
if name && !fields.empty?
15-
type_builder = SimpleFeatureTypeBuilder.new
16-
type_builder.set_name NameImpl.new(name)
17-
type_builder.setNamespaceURI uri
18-
fields.each do |field|
19-
if field.instance_of? GeoScript::Feature::Field
20-
name, type, proj = field.name, field.type, field.proj
21-
elsif field.instance_of? Hash
22-
if field[:name] && field[:type]
23-
name, type = field[:name], field[:type]
24-
else
25-
name, type = field.keys.first, field.values.first
26-
end
10+
def initialize(name = nil, fields = [], type = nil, uri = 'http://geoscript.org/feature')
11+
if name && !fields.empty?
12+
type_builder = SimpleFeatureTypeBuilder.new
13+
type_builder.set_name NameImpl.new(name)
14+
type_builder.setNamespaceURI uri
15+
fields.each do |field|
16+
if field.instance_of? GeoScript::Field
17+
name, type, proj = field.name, field.type, field.proj
18+
elsif field.instance_of? Hash
19+
if field[:name] && field[:type]
20+
name, type = field[:name], field[:type]
21+
else
22+
name, type = field.keys.first, field.values.first
23+
end
2724

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]
25+
if type.name.match /GeoScript::Geom/
26+
proj = GeoScript::Projection.new(field[:proj]) if field[:proj]
27+
end
28+
elsif field.instance_of? Array
29+
name, type = field[0], field[1]
3330

34-
if type.name.match /GeoScript::Geom/
35-
proj = GeoScript::Projection.new(field[2]) if field[2]
36-
end
31+
if type.name.match /GeoScript::Geom/
32+
proj = GeoScript::Projection.new(field[2]) if field[2]
3733
end
38-
type_builder.crs proj if proj
39-
type_builder.add name, type.to_java.java_class
40-
p type_builder.attributes
41-
end
42-
@feature_type = type_builder.build_feature_type
43-
else
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'
5034
end
35+
type_builder.crs proj if proj
36+
type_builder.add name, type.to_java.java_class
37+
end
38+
@feature_type = type_builder.build_feature_type
39+
else
40+
if !name && fields.empty?
41+
raise "No name specified & No fields specified for type: #{type}"
42+
elsif fields.empty?
43+
raise "No fields specified for type: #{type}" if fields.empty?
44+
elsif !name
45+
raise 'No name specified'
5146
end
5247
end
48+
end
5349

54-
def name
55-
@feature_type.name.local_part
56-
end
57-
58-
def uri
59-
@feature_type.name.namespaceURI
60-
end
50+
def name
51+
@feature_type.name.local_part
52+
end
6153

62-
def proj
63-
crs = @feature_type.coordinate_reference_system
64-
GeoScript::Projection.new crs if crs
65-
end
54+
def uri
55+
@feature_type.name.namespaceURI
56+
end
6657

67-
def fields
68-
fields = []
58+
def proj
59+
crs = @feature_type.coordinate_reference_system
60+
GeoScript::Projection.new crs if crs
61+
end
6962

70-
@feature_type.attribute_descriptors.each do |ad|
71-
fields << self.get(ad.local_name)
72-
end
63+
def fields
64+
fields = []
7365

74-
fields
66+
@feature_type.attribute_descriptors.each do |ad|
67+
fields << self.get(ad.local_name)
7568
end
7669

77-
def get(name)
78-
@feature_type.get_descriptor name
79-
end
70+
fields
71+
end
72+
73+
def get(name)
74+
@feature_type.get_descriptor name
8075
end
8176
end
8277
end

0 commit comments

Comments
 (0)