double Intersection(List <IntPoint> polygon, Municipality municipality)
        {
            Clipper c = new Clipper();

            c.AddPolygon(polygon, PolyType.ptSubject);
            c.AddPolygon(municipality.polygon, PolyType.ptClip);

            List <List <IntPoint> > solution = new List <List <IntPoint> >();

            c.Execute(ClipType.ctIntersection, solution);
            return(solution.Sum(s => Clipper.Area(s)));
        }
        public void Load()
        {
            string    url        = GetUrl(374000, 30000, 624000, 194000, true);
            WebClient client     = new WebClient();
            var       data       = Encoding.UTF8.GetString(client.DownloadData(url));
            JObject   jsonObject = JObject.Parse(data);

            foreach (var entity in jsonObject.Last.First.Children())
            {
                var m = new Municipality(entity);
                municipalities[m.Id] = m;
            }
        }
Example #3
0
        public MunicipalitiesView(Repository repository)
        {
            InitializeComponent();
            this.repository = repository;
            Wbitmap         = new WriteableBitmap(repository.Width, repository.Height, 96, 96, PixelFormats.Rgb24, null);
            pixels1d        = new byte[repository.Height * repository.Width * 3];

            foreach (var item in repository.Municipalities.map)
            {
                Municipality m = repository.Municipalities.municipalities[item.Value];
                FillBlock(item.Key.X, item.Key.Y, m.Color);
            }

            Int32Rect rect = new Int32Rect(0, 0, repository.Width, repository.Height);

            Wbitmap.WritePixels(rect, pixels1d, 3 * repository.Width, 0);
            mapView.imgMap.Source = Wbitmap;
        }
Example #4
0
        public async Task RunArea(int x1, int y1, int x2, int y2, Municipality m)
        {
            cancellationSource = new CancellationTokenSource();
            jobs = new ObservableCollection <Job>();
            Dictionary <TileId, Tile> tiles = new Dictionary <TileId, Tile>();

            for (int x = x1; x <= x2; x++)
            {
                for (int y = y1; y <= y2; y++)
                {
                    TileId id = new TileId(x, y);
                    if (repository.Municipalities.map[id] != m.Id)
                    {
                        continue;
                    }
                    if (repository.Tiles.ContainsKey(id))
                    {
                        tiles[id] = repository.Tiles[id];
                    }
                    else
                    {
                        var tile = repository.GenerateTile(id);
                        tiles[id] = repository.Tiles[id] = tile;
                    }
                    repository.UpdateTile(tiles[id]);
                }
            }

            waitingQueue = new BufferBlock <Tile>(new DataflowBlockOptions()
            {
                EnsureOrdered = true
            });

            workerBlock = new TransformBlock <Tile, Tile>(async t =>
            {
                t.Rescan(repository.DirectoryForTile(t.Id));
                var job = Job.NextJob(t);
                if (job == null)
                {
                    return(t);
                }
                JobStarted.Invoke(this, job);
                await job.Run(cancellationSource.Token, Cleanup);
                t.Rescan(repository.DirectoryForTile(t.Id));
                JobFinished.Invoke(this, job);
                byte[] csvLine = job.CsvBytes;
                repository.logFile.Write(csvLine, 0, csvLine.Length);
                repository.logFile.Flush();
                return(t);
            }, new ExecutionDataflowBlockOptions()
            {
                CancellationToken      = cancellationSource.Token,
                MaxDegreeOfParallelism = workers,
            });

            //ActionBlock<Tile> mapUpdater = new ActionBlock<Tile>(t => repository.UpdateTile(t));
            TaskCompletionSource <int> finishTask = new TaskCompletionSource <int>();
            int finishedCounter             = tiles.Count;
            ActionBlock <Tile> tileFinished = new ActionBlock <Tile>(t => {
                finishedCounter--;
                if (finishedCounter == 0)
                {
                    finishTask.SetResult(finishedCounter);
                }
            });

            waitingQueue.LinkTo(workerBlock);
            link = workerBlock.LinkTo(waitingQueue, t => Job.NextJob(t) != null);
            workerBlock.LinkTo(tileFinished, t => Job.NextJob(t) == null);
            //workerBlock.LinkTo(mapUpdater);

            foreach (var pair in tiles)
            {
                await waitingQueue.SendAsync(pair.Value);
            }

            await finishTask.Task;
        }