Class: URBANopt::GeoJSON::GeoFile

Inherits:
Core::FeatureFile
  • Object
show all
Defined in:
lib/urbanopt/geojson/geo_file.rb

Constant Summary collapse

@@geojson_schema =
get_geojson_schema(strict)
@@schema_file_lock =
Mutex.new
@@building_schema =
get_building_schema(strict)
@@district_system_schema =
get_district_system_schema(strict)
@@region_schema =
get_region_schema(strict)
@@electrical_connector_schema =
get_electrical_connector_schema(strict)
@@electrical_junction_schema =
get_electrical_junction_schema(strict)
@@thermal_connector_schema =
get_thermal_connector_schema(strict)
@@thermal_junction_schema =
get_thermal_junction_schema(strict)
@@site_schema =
get_site_schema(strict)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(geojson_file, path = nil) ⇒ GeoFile

Initialize GeoJSON file and path.

Parameters
  • path - Type:String GeoJSON File path.

  • data - Type:Hash Contains the GeoJSON File.



26
27
28
29
# File 'lib/urbanopt/geojson/geo_file.rb', line 26

def initialize(geojson_file, path = nil)
  @path = path
  @geojson_file = geojson_file
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



130
131
132
# File 'lib/urbanopt/geojson/geo_file.rb', line 130

def path
  @path
end

Class Method Details

.from_file(path) ⇒ Object

Parameters

Used to check the GeoJSON file path.

  • path - Type:String - GeoJSON file path.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/urbanopt/geojson/geo_file.rb', line 36

def self.from_file(path)
  if path.nil? || path.empty?
    raise "GeoJSON file '#{path}' could not be found"
  end

  if !File.exist?(path)
    raise "GeoJSON file '#{path}' does not exist"
  end

  geojson_file = JSON.parse(
    File.open(path, 'r', &:read),
    symbolize_names: true
  )


  # validate geojson file against schema
  geojson_errors = validate(@@geojson_schema, geojson_file)
  unless geojson_errors.empty?
    raise "GeoJSON file does not adhere to the schema: \n #{geojson_errors.join('\n  ')}"
  end

  # initialize @@logger
  @@logger ||= URBANopt::GeoJSON.logger

  # validate project section first
  if geojson_file.key?(:project)
    errors = validate(@@site_schema, geojson_file[:project])

    unless errors.empty?
      raise "Project section does not adhere to schema: \n #{errors.join('\n  ')}"
    end
  end

  # validate each feature against schema
  geojson_file[:features].each do |feature|
    properties = feature[:properties]
    type = properties[:type]

    errors = []

    case type
    when 'Building'
      # In case detailed_model_filename present check for fewer properties
      if feature[:properties][:detailed_model_filename]
        if feature[:properties][:id].nil?
          raise('No id found for Building Feature')
        end
        if feature[:properties][:name].nil?
          raise('No name found for Building Feature')
        end
        if feature[:properties][:number_of_stories].nil?
          @@logger.warn("Number of stories is required to calculate shading using the UrbanGeometryCreation measure.\n" \
            "Not validating #{feature[:properties][:id]} against schema and ignoring in shading calculations")
        end
        feature[:additionalProperties] = true
      # In case hpxml_directory present check for fewer properties
      elsif feature[:properties][:hpxml_directory]
        if feature[:properties][:id].nil?
          raise('No id found for Building Feature')
        end
        if feature[:properties][:name].nil?
          raise('No name found for Building Feature')
        end
        @@logger.warn("OS-HPXML files may not conform to schema, which is usually ok.\n" \
          "Not validating #{feature[:properties][:id]} against schema")
      # Else validate for all required properties in the schema
      else
        errors = validate(@@building_schema, properties)
      end
    when 'District System'
      errors = validate(@@district_system_schema, properties)
    when 'Region'
      error = validate(@@district_system_schema, properties)
    when 'ElectricalJunction'
      errors = validate(@@electrical_junction_schema, properties)
    when 'ElectricalConnector'
      errors = validate(@@electrical_connector_schema, properties)
    when 'ThermalJunction'
      errors = validate(@@thermal_junction_schema, properties)
    when 'ThermalConnector'
      errors = validate(@@thermal_connector_schema, properties)
    end

    unless errors.empty?
      raise "#{type} does not adhere to schema: \n #{errors.join('\n  ')}"
    end
  end
  return new(geojson_file, path)
end

.get_building_schema(strict) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/urbanopt/geojson/geo_file.rb', line 237

def self.get_building_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/building_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.get_district_system_schema(strict) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/urbanopt/geojson/geo_file.rb', line 250

def self.get_district_system_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/district_system_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.get_electrical_connector_schema(strict) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/urbanopt/geojson/geo_file.rb', line 289

def self.get_electrical_connector_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/electrical_connector_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.get_electrical_junction_schema(strict) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/urbanopt/geojson/geo_file.rb', line 302

def self.get_electrical_junction_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/electrical_junction_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.get_geojson_schema(strict) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/urbanopt/geojson/geo_file.rb', line 225

def self.get_geojson_schema(strict)
  result = nil
  if @@geojson_schema.nil?
    @@schema_file_lock.synchronize do
      File.open(File.dirname(__FILE__) + '/schema/geojson_schema.json') do |f|
        result = JSON.parse(f.read, symbolize_names: true)
      end
    end
  end
  return result
end

.get_region_schema(strict) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/urbanopt/geojson/geo_file.rb', line 263

def self.get_region_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/region_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.get_site_schema(strict) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/urbanopt/geojson/geo_file.rb', line 276

def self.get_site_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/site_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.get_thermal_connector_schema(strict) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/urbanopt/geojson/geo_file.rb', line 315

def self.get_thermal_connector_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/thermal_connector_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.get_thermal_junction_schema(strict) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/urbanopt/geojson/geo_file.rb', line 328

def self.get_thermal_junction_schema(strict)
  result = nil
  File.open(File.dirname(__FILE__) + '/schema/thermal_junction_properties.json') do |f|
    result = JSON.parse(f.read)
  end
  if strict
    result['additionalProperties'] = true
  else
    result['additionalProperties'] = false
  end
  return result
end

.validate(schema_json, data) ⇒ Object

Validate GeoJSON against schema.

Parameters
  • data - + - Type:Hash - Input GeoJSON file



220
221
222
223
# File 'lib/urbanopt/geojson/geo_file.rb', line 220

def self.validate(schema_json, data)
  errors = JSON::Validator.fully_validate(schema_json, data, errors_as_objects: true)
  return errors
end

Instance Method Details

#featuresObject

This method loops through all the features in the GeoJSON file, creates new Buildings or District Systems based on the feature type, and returns the features.



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/urbanopt/geojson/geo_file.rb', line 136

def features
  result = []
  @geojson_file[:features].each do |f|
    if f[:properties] && f[:properties][:type] == 'Building'
      result << URBANopt::GeoJSON::Building.new(f)
    elsif f[:properties] && f[:properties][:type] == 'District System'
      result << URBANopt::GeoJSON::DistrictSystem.new(f)
    end
  end
  return result
end

#get_feature_by_id(feature_id) ⇒ Object

Returns feature object by feature_id from specified GeoJSON file and creates a new URBANopt::GeoJSON::Building or URBANopt::GeoJSON::DistrictSystem based on the feature type. Before returning the feature, merge ‘Site Origin’ properties into the feature

Parameters
  • feature_id - Type:String/Number - Id affiliated with feature object.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/urbanopt/geojson/geo_file.rb', line 155

def get_feature_by_id(feature_id)
  @geojson_file[:features].each do |f|
    if f[:properties] && f[:properties][:id] == feature_id
      # merge site origin properties
      f = merge_site_properties(f)
      if f[:properties][:type] == 'Building'

        return URBANopt::GeoJSON::Building.new(f)
      elsif f[:properties] && f[:properties][:type] == 'District System'
        return URBANopt::GeoJSON::DistrictSystem.new(f)
      end
    end
  end
  return nil
end

#jsonObject



126
127
128
# File 'lib/urbanopt/geojson/geo_file.rb', line 126

def json
  @geojson_file
end

#merge_site_properties(feature) ⇒ Object

Merge Site Properties in Feature. Returns feature with site properties added to its properties section. Does not overwrite existing properties.

Parameters

feature - Type:Hash - feature object.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/urbanopt/geojson/geo_file.rb', line 176

def merge_site_properties(feature)
  project = {}
  if @geojson_file.key?(:project)
    project = @geojson_file[:project]
  end

  # this maps site properties to building/district system properties.
  add_props = [
    { site: :surface_elevation, feature: :surface_elevation },
    { site: :timesteps_per_hour, feature: :timesteps_per_hour },
    { site: :begin_date, feature: :begin_date },
    { site: :end_date, feature: :end_date },
    { site: :cec_climate_zone, feature: :cec_climate_zone },
    { site: :climate_zone, feature: :climate_zone },
    { site: :default_template, feature: :template },
    { site: :weather_filename, feature: :weather_filename },
    { site: :tariff_filename, feature: :tariff_filename },
    { site: :emissions, feature: :emissions },
    { site: :electricity_emissions_future_subregion, feature: :electricity_emissions_future_subregion },
    { site: :electricity_emissions_hourly_historical_subregion, feature: :electricity_emissions_hourly_historical_subregion },
    { site: :electricity_emissions_annual_historical_subregion, feature: :electricity_emissions_annual_historical_subregion },
    { site: :electricity_emissions_future_year, feature: :electricity_emissions_future_year },
    { site: :electricity_emissions_hourly_historical_year, feature: :electricity_emissions_hourly_historical_year },
    { site: :electricity_emissions_annual_historical_year, feature: :electricity_emissions_annual_historical_year }
  ]

  add_props.each do |prop|
    if project.key?(prop[:site]) && project[prop[:site]]
      # property exists in site
      if !feature[:properties].key?(prop[:feature]) || feature[:properties][prop[:feature]].nil? || feature[:properties][prop[:feature]].to_s.empty?
        # property does not exist in feature or is nil: add site property (don't overwrite)
        feature[:properties][prop[:feature]] = project[prop[:site]]
      end
    end
  end

  return feature
end