public void TestInitialize() { _pointsOfInterestProvider = Substitute.For <IPointsOfInterestProvider>(); _pointsOfInterestAggregatorService = Substitute.For <IPointsOfInterestAggregatorService>(); _tagHelper = Substitute.For <ITagsHelper>(); _wikimediaCommonGateway = Substitute.For <IWikimediaCommonGateway>(); _osmGateway = Substitute.For <IOsmGateway>(); var cache = new LruCache <string, TokenAndSecret>(Substitute.For <IOptions <ConfigurationData> >(), Substitute.For <ILogger>()); var factory = Substitute.For <IHttpGatewayFactory>(); factory.CreateOsmGateway(Arg.Any <TokenAndSecret>()).Returns(_osmGateway); _controller = new PointsOfInterestController(factory, _tagHelper, _wikimediaCommonGateway, _pointsOfInterestProvider, _pointsOfInterestAggregatorService, new Base64ImageStringToFileConverter(), cache); }
/// <summary> /// Service's constructor /// </summary> /// <param name="factory"></param> /// <param name="elasticSearchGateway"></param> /// <param name="osmGeoJsonPreprocessorExecutor"></param> /// <param name="tagsHelper"></param> /// <param name="osmRepository"></param> /// <param name="adapters"></param> /// <param name="logger"></param> public OsmElasticSearchUpdaterService(IHttpGatewayFactory factory, IElasticSearchGateway elasticSearchGateway, IOsmGeoJsonPreprocessorExecutor osmGeoJsonPreprocessorExecutor, ITagsHelper tagsHelper, IOsmRepository osmRepository, IEnumerable <IPointsOfInterestAdapter> adapters, ILogger logger) { _elasticSearchGateway = elasticSearchGateway; _osmGeoJsonPreprocessorExecutor = osmGeoJsonPreprocessorExecutor; _tagsHelper = tagsHelper; _osmRepository = osmRepository; _adapters = adapters; _logger = logger; _osmGateway = factory.CreateOsmGateway(new TokenAndSecret("", "")); }
public void TestInitialize() { var options = new ConfigurationData() { OsmConfiguraion = new OsmConfiguraionData { BaseAddress = "http://api06.dev.openstreetmap.org", ConsumerKey = "uR7K7PcxOyFG2FnTdTuEqAmlq6hTWPDmF4xknWxQ", ConsumerSecret = "hd8WnRpQQtzS04HeFMLUHN2JQtPWzQLOmA6OeE9l" } }; var optionsProvider = Substitute.For <IOptions <ConfigurationData> >(); optionsProvider.Value.Returns(options); _gateway = new OsmGateway(new TokenAndSecret("IwrfBMSqyuq3haudBUgfrjflXnvhAcbTvqVBa47l", "eBY4iWlGNMrvERH56vp0jjU8RsVhsroQIns5MQGz"), optionsProvider, new TraceLogger()); }
private void SetupHighway(int wayId, Coordinate[] coordinates, IOsmGateway osmGateway) { var table = new AttributesTable { { FeatureAttributes.ID, wayId.ToString() } }; _elasticSearchGateway.GetHighways(Arg.Any <Coordinate>(), Arg.Any <Coordinate>()).Returns(new List <Feature> { new Feature(new LineString(coordinates), table) }); var osmCompleteWay = new CompleteWay { Id = wayId }; var id = 1; osmCompleteWay.Nodes = coordinates.Select(coordinate => new Node { Id = id++, Latitude = coordinate.Y, Longitude = coordinate.X }).ToArray(); osmGateway.GetCompleteWay(wayId.ToString()).Returns(osmCompleteWay); }
/// <summary> /// Service's constructor /// </summary> /// <param name="factory"></param> /// <param name="elasticSearchGateway"></param> /// <param name="osmGeoJsonPreprocessorExecutor"></param> /// <param name="tagsHelper"></param> /// <param name="osmRepository"></param> /// <param name="pointsOfInterestAdapterFactory"></param> /// <param name="featuresMergeExecutor"></param> /// <param name="latestFileFetcherExecutor"></param> /// <param name="graphHopperGateway"></param> /// <param name="logger"></param> public ElasticSearchUpdaterService(IHttpGatewayFactory factory, IElasticSearchGateway elasticSearchGateway, IOsmGeoJsonPreprocessorExecutor osmGeoJsonPreprocessorExecutor, ITagsHelper tagsHelper, IOsmRepository osmRepository, IPointsOfInterestAdapterFactory pointsOfInterestAdapterFactory, IFeaturesMergeExecutor featuresMergeExecutor, IOsmLatestFileFetcherExecutor latestFileFetcherExecutor, IGraphHopperGateway graphHopperGateway, ILogger logger) { _elasticSearchGateway = elasticSearchGateway; _osmGeoJsonPreprocessorExecutor = osmGeoJsonPreprocessorExecutor; _tagsHelper = tagsHelper; _osmRepository = osmRepository; _pointsOfInterestAdapterFactory = pointsOfInterestAdapterFactory; _logger = logger; _featuresMergeExecutor = featuresMergeExecutor; _latestFileFetcherExecutor = latestFileFetcherExecutor; _graphHopperGateway = graphHopperGateway; _osmGateway = factory.CreateOsmGateway(new TokenAndSecret("", "")); }
/// <inheritdoc/> public async Task Add(LineString line, Dictionary <string, string> tags, TokenAndSecret tokenAndSecret) { _osmGateway = _httpGatewayFactory.CreateOsmGateway(tokenAndSecret); var createdElements = new List <OsmGeo>(); var modifiedElement = new List <OsmGeo>(); int generatedId = -1; var newWayNodeIds = new List <string>(); var highways = await GetHighwaysInArea(line); var itmHighways = highways.Select(ToItmLineString).ToList(); var waysToUpdateIds = new List <long?>(); for (int coordinateIndex = 0; coordinateIndex < line.Coordinates.Length; coordinateIndex++) { var coordinate = line.Coordinates[coordinateIndex]; if (coordinateIndex > 0) { var previousCoordinate = line.Coordinates[coordinateIndex - 1]; AddIntersectingNodes(previousCoordinate, coordinate, newWayNodeIds, itmHighways, highways); } var closetHighway = GetClosetHighway(coordinate, itmHighways, highways); if (closetHighway == null) { // no close highways, adding a new node var node = new Node { Id = generatedId--, Latitude = coordinate.Y, Longitude = coordinate.X }; createdElements.Add(node); newWayNodeIds.Add(node.Id.ToString()); continue; } var itmPoint = GetItmCoordinate(coordinate); var closestItmHighway = itmHighways.First(hw => hw.GetOsmId() == closetHighway.GetOsmId()); var closestItmPointInWay = closestItmHighway.Coordinates.OrderBy(c => c.Distance(itmPoint.Coordinate)).First(); var indexOnWay = closestItmHighway.Coordinates.ToList().IndexOf(closestItmPointInWay); var closestNodeId = ((List <object>)closetHighway.Attributes[FeatureAttributes.OSM_NODES])[indexOnWay].ToString(); if (!CanAddNewNode(newWayNodeIds, closestNodeId)) { continue; } if (closestItmPointInWay.Distance(itmPoint.Coordinate) <= _options.MaxDistanceToExisitngLineForMerge) { // close highway, adding the node id from that highway newWayNodeIds.Add(closestNodeId); continue; } // need to add a new node to existing highway var newNode = new Node { Id = generatedId--, Latitude = coordinate.Y, Longitude = coordinate.X }; createdElements.Add(newNode); newWayNodeIds.Add(newNode.Id.ToString()); var indexToInsert = GetIndexToInsert(indexOnWay, closestItmHighway, itmPoint); if (modifiedElement.FirstOrDefault(w => w.Id.ToString() == closestItmHighway.GetOsmId()) is Way modifiedWay && modifiedWay.Nodes[indexToInsert] < 0) { // a new node was added to this highway - no reason to add a new one to the same location continue; } var simpleWay = await AddNewNodeToExistingWay(newNode.Id.ToString(), closestItmHighway.GetOsmId(), indexToInsert); modifiedElement.Add(simpleWay); waysToUpdateIds.Add(simpleWay.Id); } var newWay = CreateWay(newWayNodeIds, tags, generatedId--); createdElements.Add(newWay); waysToUpdateIds.Add(newWay.Id); var changes = new OsmChange { Create = createdElements.ToArray(), Modify = modifiedElement.ToArray(), Delete = new OsmGeo[0] }; var changesetId = await _osmGateway.CreateChangeset(CreateCommentFromTags(tags)); try { var diffResult = await _osmGateway.UploadChangeset(changesetId, changes); waysToUpdateIds = waysToUpdateIds.Select(id => { var newIdResult = diffResult.Results.FirstOrDefault(r => r.OldId.Equals(id)); return(newIdResult?.NewId ?? id); }).ToList(); await AddWaysToElasticSearch(waysToUpdateIds); } finally { await _osmGateway.CloseChangeset(changesetId); } }
/// <inheritdoc/> public async Task Add(LineString line, Dictionary <string, string> tags, TokenAndSecret tokenAndSecret) { _osmGateway = _httpGatewayFactory.CreateOsmGateway(tokenAndSecret); var changesetId = await _osmGateway.CreateChangeset(CreateCommentFromTags(tags)); try { var nodeIds = new List <string>(); var highways = await GetHighwaysInArea(line); var itmHighways = highways.Select(ToItmLineString).ToList(); var waysToUpdateIds = new List <string>(); for (int coordinateIndex = 0; coordinateIndex < line.Coordinates.Length; coordinateIndex++) { var coordinate = line.Coordinates[coordinateIndex]; if (coordinateIndex > 0) { var previousCoordinate = line.Coordinates[coordinateIndex - 1]; await AddIntersectingNodes(previousCoordinate, coordinate, nodeIds, itmHighways); } var closestCompleteWay = await GetClosetHighway(coordinate, itmHighways); if (closestCompleteWay == null) { // no close highways, adding a new node nodeIds.Add(await _osmGateway.CreateElement(changesetId, new Node { Id = 0, Latitude = coordinate.Y, Longitude = coordinate.X })); continue; } var itmPoint = GetItmCoordinate(coordinate); var closestItmHighway = itmHighways.First(hw => hw.GetOsmId() == closestCompleteWay.Id.ToString()); var closestItmPointInWay = closestItmHighway.Coordinates.OrderBy(c => c.Distance(itmPoint.Coordinate)).First(); var indexOnWay = closestItmHighway.Coordinates.ToList().IndexOf(closestItmPointInWay); var closestNodeId = closestCompleteWay.Nodes[indexOnWay].Id.ToString(); if (nodeIds.Any() && nodeIds.Last() == closestNodeId) { continue; } if (closestItmPointInWay.Distance(itmPoint.Coordinate) <= _options.DistanceToExisitngLineMergeThreshold) { // close hihgway, adding the node id from that highway nodeIds.Add(closestNodeId); continue; } // need to add a new node to existing highway var newNodeId = await _osmGateway.CreateElement(changesetId, new Node { Id = 0, Latitude = coordinate.Y, Longitude = coordinate.X }); nodeIds.Add(newNodeId); var simpleWay = AddNewNodeToExistingWay(newNodeId, closestCompleteWay, closestItmHighway, indexOnWay, itmPoint); await _osmGateway.UpdateElement(changesetId, simpleWay); waysToUpdateIds.Add(simpleWay.Id.ToString()); } var newWayId = await AddWayToOsm(nodeIds, tags, changesetId); waysToUpdateIds.Add(newWayId); await AddWaysToElasticSearch(waysToUpdateIds); } finally { await _osmGateway.CloseChangeset(changesetId); } }