Improve Geonames support.
[gpl/argeo-suite.git] / knowledge / org.argeo.support.geonames / src / org / argeo / support / geonames / ImportGeonamesAdmin.java
1 package org.argeo.support.geonames;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.nio.charset.StandardCharsets;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.argeo.util.CsvParser;
14 import org.argeo.util.CsvWriter;
15
16 /** Import GeoNames administrative division from the main table. */
17 public class ImportGeonamesAdmin {
18         // private Log log = LogFactory.getLog(ImportGeonamesAdmin.class);
19         private Map<Long, GeonamesAdm> geonamesAdms = new HashMap<>();
20
21         /** Loads the data. */
22         public void parse(InputStream in) {
23                 Map<String, Long> countryGeonameIds = new HashMap<>();
24                 Map<String, Long> admin1GeonameIds = new HashMap<>();
25                 CsvParser csvParser = new CsvParser() {
26
27                         @Override
28                         protected void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
29                                 if (!"A".equals(tokens.get(6)))
30                                         return;
31                                 GeonamesAdm geonamesAdm = new GeonamesAdm(tokens);
32                                 geonamesAdms.put(geonamesAdm.getGeonameId(), geonamesAdm);
33                                 if (geonamesAdm.getAdmLevel().equals("PCLI"))
34                                         countryGeonameIds.put(geonamesAdm.getCountryCode(), geonamesAdm.getGeonameId());
35                                 if (geonamesAdm.getAdmLevel().equals("ADM1"))
36                                         admin1GeonameIds.put(geonamesAdm.getAdminCode1(), geonamesAdm.getGeonameId());
37                         }
38                 };
39                 csvParser.setSeparator('\t');
40                 csvParser.setNoHeader(true);
41                 csvParser.parse(in, StandardCharsets.UTF_8);
42
43                 // fill upper levels
44                 for (GeonamesAdm adm : geonamesAdms.values()) {
45                         adm.getUpperLevelIds()[0] = countryGeonameIds.get(adm.getCountryCode());
46                         if (adm.getLevel() > 0)
47                                 adm.getUpperLevelIds()[1] = admin1GeonameIds.get(adm.getAdminCode1());
48                         adm.mapUpperLevels(geonamesAdms);
49                 }
50
51         }
52
53         public Map<Long, GeonamesAdm> getGeonamesAdms() {
54                 return geonamesAdms;
55         }
56
57         /**
58          * Copies only the Geonames of feature class 'A' (administrative subdivisions).
59          */
60         public static void filterGeonamesAdm(InputStream in, OutputStream out) {
61                 CsvWriter csvWriter = new CsvWriter(out, StandardCharsets.UTF_8);
62                 csvWriter.setSeparator('\t');
63                 CsvParser csvParser = new CsvParser() {
64
65                         @Override
66                         protected void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
67                                 if (tokens.size() < 7 || !"A".equals(tokens.get(6)))
68                                         return;
69                                 csvWriter.writeLine(tokens);
70                         }
71                 };
72                 csvParser.setSeparator('\t');
73                 csvParser.setNoHeader(true);
74                 csvParser.parse(in, StandardCharsets.UTF_8);
75         }
76
77         public static void main(String[] args) throws IOException {
78 //              String country = "allCountries";
79                 String country = "CI";
80 //              try (InputStream in = Files
81 //                              .newInputStream(Paths.get(System.getProperty("user.home") + "/gis/data/geonames/" + country + ".txt"));
82 //                              OutputStream out = Files.newOutputStream(
83 //                                              Paths.get(System.getProperty("user.home") + "/gis/data/geonames/" + country + "-adm.txt"))) {
84 //                      ImportGeonamesAdmin.filterGeonamesAdm(in, out);
85 //              }
86                 try (InputStream in = Files.newInputStream(
87                                 Paths.get(System.getProperty("user.home") + "/gis/data/geonames/" + country + "-adm.txt"))) {
88                         ImportGeonamesAdmin importGeonamesAdmin = new ImportGeonamesAdmin();
89                         importGeonamesAdmin.parse(in);
90                 }
91         }
92
93 }