Created
September 11, 2013 20:12
-
-
Save herskinduk/6529132 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Sitecore.ContentSearch.ComputedFields; | |
using Sitecore.ContentSearch; | |
using Spatial4n.Core.Context; | |
using Lucene.Net.Spatial.Prefix.Tree; | |
using Lucene.Net.Spatial.Prefix; | |
namespace Sitecore.Contrib.ContentSearch.Spatial | |
{ | |
public class SpatialComputedFields : IComputedIndexField | |
{ | |
public object ComputeFieldValue(Sitecore.ContentSearch.IIndexable indexable) | |
{ | |
var indexableItem = indexable as SitecoreIndexableItem; | |
if (indexableItem == null) | |
return null; | |
var item = (Sitecore.Data.Items.Item)indexableItem; | |
if (item == null) | |
return null; | |
var latLon = item[FieldName]; | |
if (latLon == "") | |
return null; | |
var spatialContext = SpatialContext.GEO; | |
var geohashTree = new GeohashPrefixTree(spatialContext, 10); | |
var strategy = new RecursivePrefixTreeStrategy(geohashTree, FieldName); | |
var shape = spatialContext.ReadShape(latLon); | |
var grid = strategy.GetGrid(); | |
int levelForDistance = grid.GetLevelForDistance(strategy.DistErrPct); | |
IList<Node> list = grid.GetNodes(shape, levelForDistance, true); | |
return list.Select(node => node.GetTokenString()); | |
} | |
public string FieldName | |
{ | |
get; | |
set; | |
} | |
public string ReturnType | |
{ | |
get; | |
set; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment