private void ShapeAoi(IPolygon poly = null)
        {
            this.query = string.Empty;

            var geometries = new List <IGeometry>();

            if (poly == null)
            {
                geometries = Jarvis.GetSelectedGeometries(ArcMap.Document.FocusMap);
            }
            else
            {
                // project the geometry to WGS84 only projection compatible on the backend
                var projectedPoly = Jarvis.ProjectToWGS84(poly);
                geometries.Add(projectedPoly);
            }

            // check to see if features were selected
            if (geometries.Count == 0)
            {
                MessageBox.Show(GbdxResources.noFeaturesSelected);
                return;
            }

            this.Aoi = Jarvis.CreateGeometryCollectionGeoJson(geometries);

            this.treeView1.CheckBoxes = false;
            this.treeView1.Nodes.Clear();
            var searchingNode = new VectorIndexSourceNode {
                Text = GbdxResources.SearchingText
            };

            this.treeView1.Nodes.Add(searchingNode);

            this.currentApplicationState = this.applicationStateGenerator.Next();

            if (this.textBoxSearch.Text.Equals(GbdxResources.EnterSearchTerms) ||
                this.textBoxSearch.Text == string.Empty)
            {
                this.query = string.Empty;
                this.GetSources(this.currentApplicationState);
            }
            else
            {
                this.query = this.textBoxSearch.Text;
                this.GetGeometries(searchingNode, this.currentApplicationState);
            }
        }
        private void UpdateTreeViewWithSources(IRestResponse <SourceTypeResponseObject> resp)
        {
            // An error occurred
            if (resp.Data == null || resp.Data.Data == null)
            {
                var newItem = new VectorIndexSourceNode {
                    Text = GbdxResources.Source_ErrorMessage
                };
                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(newItem);
                this.treeView1.CheckBoxes = false;

                // A error occured so stop processing
                return;
            }

            // If no sources found.
            if (resp.Data.Data.Count == 0)
            {
                var newItem = new VectorIndexSourceNode {
                    Text = GbdxResources.NoDataFound
                };
                this.treeView1.Nodes.Add(newItem);
                this.treeView1.CheckBoxes = false;

                // No sources were found so stop processing
                return;
            }

            // Before populating the tree lets remove the empty node i created and turn checkboxes back on
            this.treeView1.Nodes.Clear();
            this.treeView1.CheckBoxes = true;

            foreach (var item in resp.Data.Data)
            {
                var newItem = new VectorIndexSourceNode
                {
                    Text   = string.Format("{0} ({1})", item.Name, item.Count),
                    Source = item
                };

                this.treeView1.Nodes.Add(newItem);
            }

            this.treeView1.Sort();
        }
        private void ProcessGeometries(
            IRestResponse <SourceTypeResponseObject> resp,
            VectorIndexSourceNode source,
            int applicationState,
            int attempts)
        {
            Jarvis.Logger.Info(resp.ResponseUri.ToString());

            if ((resp.Data == null || resp.StatusCode != HttpStatusCode.OK) && attempts <= MaxAttempts)
            {
                this.GetGeometries(source, applicationState, attempts + 1);
            }

            if (applicationState == this.currentApplicationState)
            {
                this.Invoke(new UpdateTreeGeometries(this.UpdateTreeviewWithGeometry), resp, source);
            }
        }
        private void GetGeometries(VectorIndexSourceNode source, int applicationState, int attempts = 0)
        {
            var client     = new RestClient(GbdxHelper.GetEndpointBase(Settings.Default));
            var addressUrl = string.Empty;

            if (string.IsNullOrEmpty(this.query))
            {
                addressUrl = string.Format("/insight-vector/api/shape/{0}/geometries", source.Source.Name);
            }
            else
            {
                addressUrl = "/insight-vector/api/shape/query/geometries?q=" + this.query;
            }

            var request = new RestRequest(addressUrl, Method.POST);

            request.AddHeader("Authorization", "Bearer  " + this.token);
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", this.Aoi, ParameterType.RequestBody);
            attempts++;
            client.ExecuteAsync <SourceTypeResponseObject>(
                request,
                resp => this.ProcessGeometries(resp, source, applicationState, attempts));
        }
        private void UpdateTreeviewWithGeometry(
            IRestResponse <SourceTypeResponseObject> resp,
            VectorIndexSourceNode source)
        {
            if (resp.Data == null || resp.StatusCode != HttpStatusCode.OK)
            {
                // Remove the node from the list of checked nodes because an error occurred.
                this.checkedNodes.Remove(this.treeView1.Nodes[source.Index]);
                this.treeView1.Nodes[source.Index].Checked = false;
                this.treeView1.Nodes[source.Index].Text    =
                    this.treeView1.Nodes[source.Index].Text.Replace(
                        GbdxResources.SearchingText,
                        GbdxResources.Source_ErrorMessage);

                // Don't allow it to process further.
                return;
            }

            // do this for normal tree traversal with a source - geometry - type
            if (string.IsNullOrEmpty(this.query))
            {
                // Results were found so lets get rid of the searching text.
                this.treeView1.Nodes[source.Index].Text =
                    this.treeView1.Nodes[source.Index].Text.Replace(GbdxResources.SearchingText, string.Empty);
                foreach (var geoType in resp.Data.Data)
                {
                    var newItem = new VectorIndexGeometryNode
                    {
                        Source       = source.Source,
                        GeometryType = geoType,
                        Text         =
                            string.Format(
                                "{0} ({1})",
                                geoType.Name,
                                geoType.Count)
                    };

                    this.treeView1.Nodes[source.Index].Nodes.Add(newItem);
                }
            }
            else // do this when there is a query whose tree structure goes geoemtry - type
            {
                // Results were found so lets get rid of the searching text.
                this.treeView1.Nodes.Remove(source);
                this.treeView1.CheckBoxes = true;
                foreach (var geoType in resp.Data.Data)
                {
                    var newItem = new VectorIndexGeometryNode
                    {
                        Source       = null,
                        GeometryType = geoType,
                        Text         =
                            string.Format(
                                "{0} ({1})",
                                geoType.Name,
                                geoType.Count)
                    };
                    this.treeView1.Nodes.Add(newItem);
                }
            }

            this.treeView1.Sort();
        }
        private void ShapeAoi(IPolygon poly = null)
        {
            this.query = string.Empty;

            var geometries = new List<IGeometry>();
            if (poly == null)
            {
                geometries = Jarvis.GetSelectedGeometries(ArcMap.Document.FocusMap);
            }
            else
            {
                // project the geometry to WGS84 only projection compatible on the backend
                var projectedPoly = Jarvis.ProjectToWGS84(poly);
                geometries.Add(projectedPoly);
            }

            // check to see if features were selected
            if (geometries.Count == 0)
            {
                MessageBox.Show(GbdxResources.noFeaturesSelected);
                return;
            }

            this.Aoi = Jarvis.CreateGeometryCollectionGeoJson(geometries);

            this.treeView1.CheckBoxes = false;
            this.treeView1.Nodes.Clear();
            var searchingNode = new VectorIndexSourceNode { Text = GbdxResources.SearchingText };
            this.treeView1.Nodes.Add(searchingNode);

            this.currentApplicationState = this.applicationStateGenerator.Next();

            if (this.textBoxSearch.Text.Equals(GbdxResources.EnterSearchTerms)
                || this.textBoxSearch.Text == string.Empty)
            {
                this.query = string.Empty;
                this.GetSources(this.currentApplicationState);
            }
            else
            {
                this.query = this.textBoxSearch.Text;
                this.GetGeometries(searchingNode, this.currentApplicationState);
            }
        }
        private void ProcessGeometries(
            IRestResponse<SourceTypeResponseObject> resp,
            VectorIndexSourceNode source,
            int applicationState,
            int attempts)
        {
            Jarvis.Logger.Info(resp.ResponseUri.ToString());

            if ((resp.Data == null || resp.StatusCode != HttpStatusCode.OK) && attempts <= MaxAttempts)
            {
                this.GetGeometries(source, applicationState, attempts+1);
            }

            if (applicationState == this.currentApplicationState)
            {
                this.Invoke(new UpdateTreeGeometries(this.UpdateTreeviewWithGeometry), resp, source);
            }
        }
        private void GetGeometries(VectorIndexSourceNode source, int applicationState, int attempts = 0)
        {
            var client = new RestClient(GbdxHelper.GetEndpointBase(Settings.Default));
            var addressUrl = string.Empty;

            if (string.IsNullOrEmpty(this.query))
            {
                addressUrl = string.Format("/insight-vector/api/shape/{0}/geometries", source.Source.Name);
            }
            else
            {
                addressUrl = "/insight-vector/api/shape/query/geometries?q=" + this.query;
            }

            var request = new RestRequest(addressUrl, Method.POST);
            request.AddHeader("Authorization", "Bearer  " + this.token);
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", this.Aoi, ParameterType.RequestBody);
            attempts++;
            client.ExecuteAsync<SourceTypeResponseObject>(
                request,
                resp => this.ProcessGeometries(resp, source, applicationState, attempts));
        }
        private void UpdateTreeViewWithSources(IRestResponse<SourceTypeResponseObject> resp)
        {
            // An error occurred
            if (resp.Data == null || resp.Data.Data == null)
            {
                var newItem = new VectorIndexSourceNode { Text = GbdxResources.Source_ErrorMessage };
                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(newItem);
                this.treeView1.CheckBoxes = false;

                // A error occured so stop processing
                return;
            }

            // If no sources found.
            if (resp.Data.Data.Count == 0)
            {
                var newItem = new VectorIndexSourceNode { Text = GbdxResources.NoDataFound };
                this.treeView1.Nodes.Add(newItem);
                this.treeView1.CheckBoxes = false;

                // No sources were found so stop processing
                return;
            }

            // Before populating the tree lets remove the empty node i created and turn checkboxes back on
            this.treeView1.Nodes.Clear();
            this.treeView1.CheckBoxes = true;

            foreach (var item in resp.Data.Data)
            {
                var newItem = new VectorIndexSourceNode
                                  {
                                      Text = string.Format("{0} ({1})", item.Name, item.Count),
                                      Source = item
                                  };

                this.treeView1.Nodes.Add(newItem);
            }

            this.treeView1.Sort();
        }
        private void UpdateTreeviewWithGeometry(
            IRestResponse<SourceTypeResponseObject> resp,
            VectorIndexSourceNode source)
        {
            if (resp.Data == null || resp.StatusCode != HttpStatusCode.OK)
            {
                // Remove the node from the list of checked nodes because an error occurred.
                this.checkedNodes.Remove(this.treeView1.Nodes[source.Index]);
                this.treeView1.Nodes[source.Index].Checked = false;
                this.treeView1.Nodes[source.Index].Text =
                    this.treeView1.Nodes[source.Index].Text.Replace(
                        GbdxResources.SearchingText,
                        GbdxResources.Source_ErrorMessage);

                // Don't allow it to process further.
                return;
            }

            // do this for normal tree traversal with a source - geometry - type
            if (string.IsNullOrEmpty(this.query))
            {
                // Results were found so lets get rid of the searching text.
                this.treeView1.Nodes[source.Index].Text =
                    this.treeView1.Nodes[source.Index].Text.Replace(GbdxResources.SearchingText, string.Empty);
                foreach (var geoType in resp.Data.Data)
                {
                    var newItem = new VectorIndexGeometryNode
                                      {
                                          Source = source.Source,
                                          GeometryType = geoType,
                                          Text =
                                              string.Format(
                                                  "{0} ({1})",
                                                  geoType.Name,
                                                  geoType.Count)
                                      };

                    this.treeView1.Nodes[source.Index].Nodes.Add(newItem);
                }
            }
            else // do this when there is a query whose tree structure goes geoemtry - type
            {
                // Results were found so lets get rid of the searching text.
                this.treeView1.Nodes.Remove(source);
                this.treeView1.CheckBoxes = true;
                foreach (var geoType in resp.Data.Data)
                {
                    var newItem = new VectorIndexGeometryNode
                                      {
                                          Source = null,
                                          GeometryType = geoType,
                                          Text =
                                              string.Format(
                                                  "{0} ({1})",
                                                  geoType.Name,
                                                  geoType.Count)
                                      };
                    this.treeView1.Nodes.Add(newItem);
                }
            }

            this.treeView1.Sort();
        }