labelingsystem-server  Version 0.1.0.0
Retcat_GeoNames Class Reference

Static Public Member Functions

static Map< String, SuggestionItem > query (String searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
 
static JSONObject info (String url) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, RetcatException
 

Member Function Documentation

◆ info()

static JSONObject info ( String  url) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, RetcatException
static
77  {
78  try {
79  String outputUrl = url;
80  url = url.replace("http://sws.geonames.org/", "");
81  url = "http://api.geonames.org/get?geonameId=" + url + "&username=" + ConfigProperties.getPropertyParam("geonames");
82  // query for xml
83  URL obj = new URL(url);
84  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
85  con.setRequestMethod("POST");
86  String urlParameters = "";
87  con.setDoOutput(true);
88  DataOutputStream wr = new DataOutputStream(con.getOutputStream());
89  wr.writeBytes(urlParameters);
90  wr.flush();
91  wr.close();
92  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
93  String inputLine;
94  StringBuilder response = new StringBuilder();
95  while ((inputLine = in.readLine()) != null) {
96  response.append(inputLine);
97  }
98  in.close();
99  // parse xml
100  String name = "";
101  int startTagName = response.indexOf("<name>");
102  int endTagName = response.indexOf("</name>");
103  if (startTagName != -1 && endTagName != -1) {
104  name = response.substring(startTagName, endTagName).replace("<name>", "");
105  }
106  int startTagAN1 = response.indexOf("<adminName1>");
107  int endTagAN1 = response.indexOf("</adminName1>");
108  String an1 = "";
109  if (startTagAN1 != -1 && endTagAN1 != -1) {
110  an1 = response.substring(startTagAN1, endTagAN1).replace("<adminName1>", "");
111  }
112  int startTagCN = response.indexOf("<countryName>");
113  int endTagCN = response.indexOf("</countryName>");
114  String cn = "";
115  if (startTagCN != -1 && endTagCN != -1) {
116  cn = response.substring(startTagCN, endTagCN).replace("<countryName>", "");
117  }
118  String desc = an1 + ", " + cn;
119  // output
120  JSONObject jsonOut = new JSONObject();
121  jsonOut.put("label", name);
122  jsonOut.put("lang", "");
123  // get retcat info
124  String type = "geonames";
125  String quality = "";
126  String group = "";
127  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
128  if (item.getType().equals(type)) {
129  quality = item.getQuality();
130  group = item.getGroup();
131  }
132  }
133  jsonOut.put("type", type);
134  jsonOut.put("quality", quality);
135  jsonOut.put("group", group);
136  jsonOut.put("uri", outputUrl);
137  jsonOut.put("scheme", "GeoNames");
138  JSONArray broader = new JSONArray();
139  JSONArray narrower = new JSONArray();
140  jsonOut.put("broaderTerms", broader);
141  jsonOut.put("narrowerTerms", narrower);
142  jsonOut.put("description", desc);
143  if (jsonOut.get("label") != null && !jsonOut.get("label").equals("")) {
144  return jsonOut;
145  } else {
146  throw new RetcatException("no label for this uri available");
147  }
148  } catch (Exception e) {
149  return new JSONObject();
150  }
151  }

References LocalRetcatItems.getLocalCatalogue(), and ConfigProperties.getPropertyParam().

Referenced by RetcatResource.getInfoGeoNames(), and RetcatResource.getQueryResultsGEONAMES().

◆ query()

static Map<String, SuggestionItem> query ( String  searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
static
29  {
30  searchword = GeneralFunctions.encodeURIComponent(searchword);
31  String url_string = "http://api.geonames.org/searchJSON?q=" + searchword + "&maxRows=" + RetcatResource.getLimit() + "&username=" + ConfigProperties.getPropertyParam("geonames");
32  URL url = new URL(url_string);
33  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
34  conn.setRequestProperty("Accept", "application/json");
35  BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
36  String inputLine;
37  StringBuilder response = new StringBuilder();
38  while ((inputLine = br.readLine()) != null) {
39  response.append(inputLine);
40  }
41  br.close();
42  // fill objects
43  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
44  JSONArray resultsArray = (JSONArray) jsonObject.get("geonames");
45  Map<String, SuggestionItem> autosuggests = new HashMap<String, SuggestionItem>();
46  for (Object element : resultsArray) {
47  JSONObject tmpElement = (JSONObject) element;
48  Long uriValue = (Long) tmpElement.get("geonameId");
49  String uri = "http://sws.geonames.org/" + uriValue;
50  autosuggests.put(uri, new SuggestionItem(uri));
51  SuggestionItem tmpAutosuggest = autosuggests.get(uri);
52  String labelValue = (String) tmpElement.get("name");
53  tmpAutosuggest.setLabel(labelValue);
54  String adminName1 = (String) tmpElement.get("adminName1");
55  String countryName = (String) tmpElement.get("countryName");
56  String lat = (String) tmpElement.get("lat");
57  String lon = (String) tmpElement.get("lng");
58  tmpAutosuggest.setDescription(adminName1 + ", " + countryName);
59  tmpAutosuggest.setSchemeTitle("GeoNames");
60  // get retcat info
61  String type = "geonames";
62  String quality = "";
63  String group = "";
64  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
65  if (item.getType().equals(type)) {
66  quality = item.getQuality();
67  group = item.getGroup();
68  }
69  }
70  tmpAutosuggest.setType(type);
71  tmpAutosuggest.setQuality(quality);
72  tmpAutosuggest.setGroup(group);
73  }
74  return autosuggests;
75  }

References GeneralFunctions.encodeURIComponent(), RetcatResource.getLimit(), LocalRetcatItems.getLocalCatalogue(), and ConfigProperties.getPropertyParam().

Referenced by RetcatResource.getQueryResultsGEONAMES().

Exception