aboutsummaryrefslogtreecommitdiff
path: root/doc/import/README.md
blob: ba4c23e98347cb493a101b0591cfc99877b08c64 (plain)
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
<!--
SPDX-FileCopyrightText: 2020 IN COMMON Collective <collective@incommon.cc>

SPDX-License-Identifier: CC-BY-SA-4.0
-->

## Parsing Resources
The three scripts `ParserDewey.rb`, `ParserConcertes.rb` and `ParserPotagers.rb` parse .csv and .json files containing map data from Maps Dewey, ConcertES and the Ceinture Alimentaire de Bruxelles and transforms it inot a .geojson file.
To run it:
	`ruby ParserDewey.rb`
	`ruby ParserConcertes.rb`
	`ruby ParserPotagers.rb`
Each parser creates a corresponding file:
	`LocationsDewey.rb`
	`LocationsConcertes.rb`
	`LocationsPotagers.rb`

The main files it uses are:
	Concertes/20200312_EntreprisesSignaletique.csv which contains the name of the points from the Concertes data
	Concertes/20200312_EntreprisesCoordonnees.csv which contains the coordinates of the points from the Concertes data
and
	Dewey/dewey-maps-markers.json which contains the data of the points from the Dewey data
and
	potagers_merged_spjoined_ar2_1.csv.diff


## Importing to Database

Once you have `Locations.geojson`, connect to the Rails console:

```ruby
# Load the GeoJSON into an Array
locs = JSON.parse(IO.read('doc/import/Locations.geojson'))
# Get related Agent records. You must have run `rails db:seed` beforehand.
dewey = Agent.find_by(name: 'Dewey')
concertes = Agent.find_by(name: 'ConcertES')
# Create Resource records
locs['features'].each do |f|
  agent = f['properties']['source'] == dewey['name'] ? dewey : concertes
  Resource.create(agent_id: agent.id, feature: f)
end
```
### Importing categories

We have a JSON file in French for categories in `doc/import/categories-fr.json`.

From the console:

```ruby
if Category.count == 0
  dewey_taxo = Taxonomy.first
  cats = JSON.parse(IO.read('doc/import/categories-fr.json'))
  cats.each do |cat|
    c = Category.create(name: cat['name'], dewey_id: cat['id'], taxonomy_id: dewey_taxo.id, color: cat['color'])
    cat['sections'].each do |sec|
      Section.create(name: sec['name'], category_id: c.id, dewey_id: sec['id'], color: sec['color'])
    end
  end
  # Now that we have all we need, update resources_sections...
  Resource.all.each do |res|
    sec_ids = res.feature['properties']['categories']
    next if sec_ids.empty?
    sec_ids.each do |id|
      s = Section.find_by(dewey_id: id)
      res.sections << s if s.present?
      res.save
    end
  end
end
```

It's taken into account in `rails db:seed`