コード例 #1
0
        public Location GetLocation(object data)
        {
            var item = data as ItemCache;
            var l = new Location(item.Latitude, item.Longitude);

            logger.Info("Location: {0}, [{1}]", l, l.PlaceName(Location.PlaceNameFilter.None));

            return l;
        }
コード例 #2
0
        private void AddExifInfo(string filename, Document doc, JToken info)
        {
            string latitude = null;
            string longitude = null;
            string latRef = null;
            string lonRef = null;
            DateTime? createdDate = null;

            foreach (var child in info.Children())
            {
                var prop = child as JProperty;
                if (prop != null)
                {
                    foreach (var grandchild in child.Children())
                    {
                        var obj = grandchild as JObject;
                        if (obj != null)
                        {
                            foreach (var kv in obj)
                            {
                                var val = ToString(kv.Value);
                                var path = string.Format("{0}.{1}", prop.Name, kv.Key);

                                // Remove leading & trailing spaces in keywords. I suspect a better way is to
                                // create a more sophisticated Analyzer, but I haven't worked that out correctly.
                                // It's important to keep single keyword phrases with spaces together: "mount rushmore"
                                if (path == "IPTC.Keywords" || path == "XMP.Subject")
                                {
                                    var tokens = val.Split(',');
                                    for (int idx = 0; idx < tokens.Length; ++idx)
                                    {
                                        tokens[idx] = tokens[idx].Trim();
                                    }
                                    val = String.Join(",", tokens);

                                    if (path == "IPTC.Keywords" && tokens.Length > 0)
                                    {
                                        foreach (var k in tokens)
                                        {
                                            var trimmedKeyword = k.Trim();
                                            if (!String.IsNullOrWhiteSpace(trimmedKeyword))
                                                doc.add(new FacetField(FacetNames.Keywords, trimmedKeyword));
                                        }
                                    }
                                }

                                switch (path)
                                {
                                    case "EXIF.GPSLatitudeRef":
                                        latRef = val;
                                        break;
                                    case "EXIF.GPSLongitudeRef":
                                        lonRef = val;
                                        break;
                                    case "EXIF.GPSLatitude":
                                        latitude = val;
                                        break;
                                    case "EXIF.GPSLongitude":
                                        longitude = val;
                                        break;
                                    case "EXIF.DateTimeOriginal":
                                    case "EXIF.CreateDate":
                                        if (createdDate == null)
                                        {
                                            DateTime temp;
                                            if (DateTime.TryParseExact(val, "yyyy:MM:dd HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out temp))
                                            {
                                                createdDate = temp;
                                            }
                                        }
                                        break;
                                }

                                string docKey;
                                if (StoredValues.TryGetValue(path, out docKey))
                                {
                                    doc.add(new TextField(docKey.ToLower(), val, Field.Store.YES));
                                }
                            }
                        }
                    }
                }
            }

            if (createdDate == null)
            {
                createdDate = new FileInfo(filename).CreationTime;
            }

            doc.add(new TextField(FieldName.Date, createdDate.Value.ToString("yyyyMMdd"), Field.Store.NO));
            doc.add(new TextField(FieldName.CreatedDate, createdDate.Value.ToString("o"), Field.Store.YES));
            doc.add(new TextField(FieldName.Day, createdDate.Value.AllDayNames(), Field.Store.NO));
            doc.add(new TextField(FieldName.Month, createdDate.Value.AllMonthNames(), Field.Store.NO));

            var facetDate = new []
            {
                createdDate.Value.Year.ToString(),
                createdDate.Value.Month.ToString(),
                createdDate.Value.Day.ToString(),
            };
            doc.add(new FacetField(FacetNames.Date, facetDate));

            doc.add(new TextField(FieldName.HasExifInfo, "t", Field.Store.NO));

            double latDouble, lonDouble;
            if (ConvertLocationRef(latitude, latRef, longitude, lonRef, out latDouble, out lonDouble))
            {
                doc.add(new DoubleField(FieldName.Latitude, latDouble, Field.Store.YES));
                doc.add(new DoubleField(FieldName.Longitude, lonDouble, Field.Store.YES));

                var location = new Location(latDouble, lonDouble);
                var placeName = location.PlaceName(Location.PlaceNameFilter.None);
                if (placeName != null)
                {
                    if (String.IsNullOrWhiteSpace(placeName))
                    {
                        logger.Warn("Unable to get a proper placename for {0} ({1})", filename, location);
                    }
                    
                    var city = location.City;
                    if (city != null)
                    {
                        doc.add(new TextField(FieldName.LocationCity, city, Field.Store.YES));
                    }

                    if (location.Country != null)
                    {
                        doc.add(new TextField(FieldName.LocationCountry, location.Country, Field.Store.YES));
                    }

                    var site = location.SiteName;
                    if (site != null)
                    {
                        doc.add(new TextField(FieldName.LocationSite, site, Field.Store.YES));
                    }


                    var placeTokens = location.PlaceNameParts(Location.PlaceNameFilter.Detailed, true);
                    if (placeTokens.Length > 0)
                        doc.add(new FacetField(FacetNames.PlaceName, placeTokens));
                    doc.add(new TextField(FieldName.LocationPlaceName, placeName, Field.Store.YES));
                    doc.add(new TextField(FieldName.LocationStandardName, location.PlaceName(Location.PlaceNameFilter.Standard), Field.Store.YES));
                }
                else
                {
                    logger.Warn("Unable to get placename for {0} ({1})", filename, location);
                }
            }

            thumbnailIndexer.CheckThumbnail(filename, doc.get(FieldName.MimeType), doc.get(FieldName.Size));
        }