-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson_to_kuro.rb
131 lines (108 loc) · 2.84 KB
/
geojson_to_kuro.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
125
126
127
128
129
130
131
#!/usr/bin/env jruby
require 'json'
result = []
class Shopentry
def initialize(name, lon, lat, addr)
@name = name
@latitude = lat
@longitude = lon
@products = []
@address = addr
end
def to_s
s = StringIO.new
s << @name
s << " "
s << @latitude
s << " "
s << @longitude
s << " "
s << @address
s.string
end
def to_hash
{
name: @name,
latitude: @latitude,
longitude: @longitude,
products: [],
address: @address
}
end
def to_json(options = {})
to_hash.to_json
end
end
class Address
def initialize(postalCode, street, town, housenumber)
s = StringIO.new
s << street
s << " "
s << housenumber
@postalCode = postalCode
@address = s.string
@address2 = ""
@district = ""
@city = town
end
def to_s
s = StringIO.new
s << @address
s << " "
s << @city
s.string
end
def to_hash
{
postalCode: @postalCode,
address: @address,
address2: "",
district: "",
city: @city
}
end
def to_json(options = {})
to_hash.to_json
end
end
t1 = Time.now
puts "Started at: " << t1.to_s
file = File.open("./export.geojson")
file_data = file.read
json_data = JSON.parse(file_data)
for obj in json_data['features'] do
shop_name = obj['properties']['name']
if(shop_name == nil)
next;
end
shop_lat = nil
shop_lon = nil
case obj['geometry']['type']
when "Point"
shop_lat = obj['geometry']['coordinates'][0]
shop_lon = obj['geometry']['coordinates'][1]
when "LineString"
shop_lat = obj['geometry']['coordinates'][0][0]
shop_lon = obj['geometry']['coordinates'][0][1]
when "Polygon"
shop_lat = obj['geometry']['coordinates'][0][0][0]
shop_lon = obj['geometry']['coordinates'][0][0][1]
when "MultiPolygon"
shop_lat = obj['geometry']['coordinates'][0][0][0][0]
shop_lon = obj['geometry']['coordinates'][0][0][0][1]
end
shop_address_postalcode = obj['properties']['addr:postcode']
shop_address_street = obj['properties']['addr:street']
shop_address_town = obj['properties']['addr:city']
shop_address_housenumber = obj['properties']['addr:housenumber']
sa = Address.new(shop_address_postalcode, shop_address_street, shop_address_town, shop_address_housenumber)
se = Shopentry.new(shop_name, shop_lat, shop_lon, sa)
result << se;
end
f = File.new("./result.json", "w")
str = JSON.dump(result)
f.write(str)
f.close
t2 = Time.now
puts "Ended at: " << t2.to_s
puts "It took summa sumarum: " << (t2-t1).to_s << " ms."