private void OnDatabaseToggleChange(bool isOn, Transform topicsContainer, WordCloudDatabase database) { if (isOn) { topicsContainer.gameObject.SetActive(true); if (topicsContainer.childCount > 0) { topicsContainer.GetChild(0).GetComponent <Toggle>().isOn = true; } } else { topicsContainer.GetComponent <ToggleGroup>().SetAllTogglesOff(); topicsContainer.gameObject.SetActive(false); } }
private IEnumerator LoadData(string siteName) { ClearUI(); var databases = new List <WordCloudDatabase>(); message.text = Translator.Get("Loading data") + " ..."; message.gameObject.SetActive(true); yield return(null); var wordCloudPath = Paths.Data + "WordCloud"; var path = wordCloudPath + Path.DirectorySeparatorChar + siteName; if (Directory.Exists(path)) { var files = Directory.GetFiles(path, "*.csv"); for (int i = 0; i < files.Length; ++i) { yield return(SurveyConfig.Load(files[i], (s) => { if (s != null) { s.name = Path.GetFileNameWithoutExtension(files[i]); databases.Add(s); } })); } } // connecting to API var configAPIPath = Paths.Data + "api.csv"; API[] apis = null; yield return(APIConfig.Load(configAPIPath, (a) => apis = a)); if (apis != null) { for (int i = 0; i < apis.Length; ++i) { // check if site name is as currect or empty what is mark for global data if (apis[i].site.Contains(siteName) || apis[i].site == "*") { yield return(WordCloudDatabase.LoadDataFromAPI(apis[i], (db) => databases.Add(db))); } } } if (databases.Count > 0) { message.gameObject.SetActive(false); groupToggle.interactable = true; CreateDataUI(databases); } else { groupToggle.interactable = false; message.text = Translator.Get("There's no data available for this site"); if (Directory.Exists(wordCloudPath)) { var dataManager = ComponentManager.Instance.Get <DataManager>(); var dirs = Directory.GetDirectories(wordCloudPath); var examples = "\n" + Translator.Get("The folowing sites have data") + ": "; bool haveExamples = false; foreach (var dir in dirs) { var dirName = dir.Substring(wordCloudPath.Length + 1); if (dataManager.HasSite(dirName)) { examples += dirName + ", "; haveExamples = true; } } if (haveExamples) { message.text += examples.Substring(0, examples.Length - 2); } } } loadCR = null; }
private static WordCloudDatabase ParseDataFromAPI(string text, API setup) { var entriesDict = new Dictionary <string, List <GeolocatedValue> >(); // Init Dictionary foreach (var value in setup.valueFields) { entriesDict.Add(value, new List <GeolocatedValue>()); } var root = JSONNode.Parse(text); int missingCoords = 0; int invalidCoords = 0; int oobCoords = 0; bool hasValidValues = false; // Get coordinates from each node and pass the values for header from setup foreach (var nodes in root) { var nodesDict = nodes.Value; // Check if long and lat names are in API data if (!nodesDict.HasKey(setup.latitudeField) || !nodesDict.HasKey(setup.longitudeField)) { missingCoords++; continue; } var nodeLon = nodesDict.GetValueOrDefault(setup.longitudeField, null); var nodeLat = nodesDict.GetValueOrDefault(setup.latitudeField, null); if (!TryGetDouble(nodeLon, out double lon) || !TryGetDouble(nodeLat, out double lat)) { invalidCoords++; continue; } if (lon > GeoCalculator.MaxLongitude || lon < GeoCalculator.MinLatitude || lat > GeoCalculator.MaxLatitude || lat < GeoCalculator.MinLatitude) { oobCoords++; continue; } var coord = new Coordinate { Longitude = lon, Latitude = lat }; foreach (var valueField in setup.valueFields) { var node = nodesDict.GetValueOrDefault(valueField, null); if (node != null && !string.IsNullOrWhiteSpace(node.Value)) { hasValidValues = true; entriesDict[valueField].Add(new GeolocatedValue { value = node.Value, coord = coord }); } } } if (missingCoords > 0) { Debug.LogError(missingCoords + " missing coordinates in API: " + setup.name); return(null); } if (invalidCoords > 0) { Debug.LogWarning(invalidCoords + " invalid coordinates in API: " + setup.name); } if (oobCoords > 0) { Debug.LogWarning(oobCoords + " out-of-bound coordinates in API: " + setup.name); } if (!hasValidValues) { return(null); } // Create new database var database = new WordCloudDatabase(); database.name = setup.name; // Create topic items from entries foreach (var pair in entriesDict) { var topic = new WordCloudTopic(); topic.name = pair.Key; foreach (var entry in pair.Value) { topic.topicItems.Add(new TopicItem { coord = entry.coord, stringLabel = entry.value }); } database.topics.Add(topic); } return(database); }
private static WordCloudDatabase Parse(StreamReader sr, string filename) { WordCloudDatabase database2 = new WordCloudDatabase(); int LatColumn = -1; int LongColumn = -1; int range = 5; string marker = "*"; System.Random rnd = new System.Random(1); string line = sr.ReadLine(); MatchCollection matches = CsvHelper.regex.Matches(line); int headerCount = matches.Count; WordCloudTopic[] topics = new WordCloudTopic[headerCount]; for (int i = 0; i < headerCount; ++i) { string headerCell = matches[i].Groups[2].Value.Trim(); bool ignore = headerCell.StartsWith(marker); if (ignore) { headerCell = headerCell.Substring(1); } // Check for column position of Latitude and Longtitude if (headerCell.EqualsIgnoreCase("latitude") || headerCell.EqualsIgnoreCase("lat") || headerCell.EqualsIgnoreCase("USULAN_LAT")) { LatColumn = i; } else if (headerCell.EqualsIgnoreCase("longitude") || headerCell.EqualsIgnoreCase("long") || headerCell.EqualsIgnoreCase("lng") || headerCell.EqualsIgnoreCase("USULAN_LONG")) { LongColumn = i; } else if (!ignore) { var topic = new WordCloudTopic(); topic.name = headerCell; topics[i] = topic; database2.topics.Add(topic); } } if (LatColumn == -1 || LongColumn == -1) { Debug.LogError("File " + filename + " doesn't have longitude/latitude headers"); return(null); } while ((line = sr.ReadLine()) != null) { matches = CsvHelper.regex.Matches(line); while (matches.Count < headerCount) { string extraLine = sr.ReadLine(); if (extraLine == null) { break; } line += extraLine; matches = CsvHelper.regex.Matches(line); } if (string.IsNullOrEmpty(matches[0].Groups[2].Value)) { continue; } double latitude = 0; double longtitude = 0; if (!double.TryParse(matches[LatColumn].Groups[2].Value, out latitude) || !double.TryParse(matches[LongColumn].Groups[2].Value, out longtitude)) { continue; } double randomLat = rnd.Next(-range, range) * 0.0001; double randomLong = rnd.Next(-range, range) * 0.0001; int count = matches.Count; for (int i = 0; i < count; ++i) { var topic = topics[i]; if (topic == null) { continue; } // split if multiple answers in survey separated by ',' or by new line var cell = matches[i].Groups[2].Value; var item = cell.Split(new string[] { "\n", "\r\n", ",", "#" }, System.StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < item.Length; ++j) { var text = item[j].Trim(); if (!string.IsNullOrEmpty(text)) { TopicItem topicItem = new TopicItem(); // Randomize position to protect privacy and avoid same position topicItem.coord.Latitude = latitude + randomLat; topicItem.coord.Longitude = longtitude + randomLong; topicItem.stringLabel = text; topicItem.size = 1; topic.topicItems.Add(topicItem); } } } } // Assing id based on string similarity Dictionary <string, int> idCheck = new Dictionary <string, int>(); foreach (var topic in database2.topics) { int id = 0; foreach (var dataItemA in topic.topicItems) { string idHelp = dataItemA.stringLabel; if (idCheck.ContainsKey(idHelp)) { dataItemA.stringId = idCheck[idHelp]; } else { dataItemA.stringId = ++id; idCheck.Add(idHelp, dataItemA.stringId); } } } return(database2); }