Exemplo n.º 1
0
        private void DoRequest()
        {
            Plugin.IsLoading = true;
            var bagResult = Plugin.CreatePoiTypeResult("BAG", Colors.CornflowerBlue);
            bagResult.Style.InnerTextColor = Colors.Black;
            bagResult.AddMetaInfo("Adres", "Adres");
            bagResult.AddMetaInfo("Postcode", "Postcode");
            bagResult.AddMetaInfo("Woonplaats", "Woonplaats");
            bagResult.AddMetaInfo("Gemeente", "Gemeente");
            bagResult.AddMetaInfo("Provincie", "Provincie");
            Plugin.SearchService.PoITypes.Add(bagResult);

            ThreadPool.QueueUserWorkItem(delegate
            {
                try
                {
                    string cmdString;
                    if (ZipCodeAndHouseNumberRegex.IsMatch(Key))
                    {
                        // Search for a zip code
                        var ms = ZipCodeAndHouseNumberRegex.Match(Key);

                        var zipNumbers  = ms.Groups["zipNumber"]       .Value.Trim();
                        var zipLetters  = ms.Groups["zipLetter"]       .Value.Trim().ToUpper();
                        var houseNumber = ms.Groups["houseNumberStart"].Value.Trim();

                        if (string.IsNullOrEmpty(houseNumber)) houseNumber = ms.Groups["houseNumberEnd"].Value.Trim();
                        cmdString = string.IsNullOrEmpty(houseNumber)
                            ? string.Format(ZipCodeLookupCommand,          zipNumbers, zipLetters)
                            : string.Format(ZipCodeAndNumberLookupCommand, zipNumbers, zipLetters, houseNumber);
                    }
                    else
                    {
                        // Search for a generic address
                        var input = substitutions.Keys.Aggregate(Key, (current, key) => current.Replace(key, substitutions[key]));
                        var keywords = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        cmdString = string.Format(AddresLookupCommand, string.Join("&", keywords));
                    }

                    var pois = new ContentList();
                    using (var conn = new NpgsqlConnection(ConnectionString))
                    {
                        conn.Open();

                        using (var command = new NpgsqlCommand(cmdString, conn))
                        {
                            using (var dr = command.ExecuteReader())
                            {
                                var count = 0;
                                while (dr.Read())
                                {
                                    count++;

                                    var position = ConvertPointZToPosition(dr.GetString(dr.GetOrdinal("location")));

                                    var p = new PoI
                                    {
                                        Service = Plugin.SearchService,
                                        InnerText = count.ToString(CultureInfo.InvariantCulture),
                                        PoiTypeId = bagResult.ContentId,
                                        PoiType = bagResult,
                                        Position = position
                                    };
                                    AddAddress(p, dr);
                                    p.UpdateEffectiveStyle();
                                    pois.Add(p);
                                }
                            }
                        }
                    }

                    Application.Current.Dispatcher.Invoke(
                        delegate
                        {
                            lock (Plugin.ServiceLock)
                            {
                                pois.ForEach(p => Plugin.SearchService.PoIs.Add(p));
                            }
                            Plugin.IsLoading = false;
                        });
                }
                catch (NpgsqlException e)
                {
                    Logger.Log("BAG Geocoding", "Error finding location", e.Message, Logger.Level.Error, true);
                    Plugin.IsLoading = false;
                }
            });
        }