Ejemplo n.º 1
0
        public override void Subscribe(Mode serviceMode)
        {
            base.Subscribe(serviceMode);
            Reset();

            PoIs.StartBatch();

            //var isLoaded = LoadOrCreateDataServiceDescription(saveDsd);
            IsLoading = true;
            try
            {
                var layer = api.layers.GetLayer(this.csLayer.Id);
                if (layer.TypeUrl.Length > 0) layer.TypeUrl = layer.TypeUrl.TrimStart('/');
                this.IsTimelineEnabled = true;



                if (!string.IsNullOrEmpty(layer.TypeUrl))
                {
                    if (layer.TypeUrl.StartsWith("api/resources/") || layer.TypeUrl.StartsWith("data/resourceTypes/"))
                    {
                        var r = layer.TypeUrl.Replace("api/resources/", "");
                        r = r.Replace("data/resourceTypes/", "");
                        r = r.Replace(".json", "");

                        var template = this.dsb.Templates.FirstOrDefault(t => t.Name == r);
                        if (template == null)
                        {
                            resource = api.resources.GetResource(r);
//                            var ps = new PoiService();
//                            ps.Name = r;
//                            ps.IsTemplate = true;
//                            ps.InitPoiService();
                            //dsb.Templates.Add(ps);
                            foreach (var ft in resource.FeatureTypes)
                            {
                                var st = ft.Value.Style;


                                var p = new global::DataServer.PoI
                                {
                                    ContentId = ft.Key,
                                    PoiId = ft.Key,
                                    Service = this,
                                    Id = Guid.NewGuid(),
                                    Style = new PoIStyle
                                    {
                                        DrawingMode = DrawingModes.Point,
                                        Icon = AppState.Config.Get("csWebApiServer", "http://localhost:3002") + "/" + ft.Value.Style.IconUri,
                                        //Picture = new BitmapImage(new Uri("http://localhost:3002/" + ft.Value.Style.IconUri))m
                                        CallOutFillColor = Colors.LightBlue,
                                        CallOutForeground = Colors.Black,
                                        CallOutTimeOut = 10,
                                        CallOutOrientation = CallOutOrientation.RightSideMenu,
                                        FillOpacity= ft.Value.Style.FillOpacity,
                                        StrokeColor= (Color)ColorConverter.ConvertFromString(ft.Value.Style.StrokeColor),
                                        StrokeOpacity = ft.Value.Style.Opacity,
                                    },
                                    MetaInfo = new List<MetaInfo>()
                                };

                                if (ft.Value.Style.FillColor != null)
                                {
                                    p.Style.FillColor =
                                        (Color) ColorConverter.ConvertFromString(ft.Value.Style.FillColor);
                                }
                                if (ft.Value.Style.StrokeColor != null)
                                {
                                    p.Style.StrokeColor = (Color)ColorConverter.ConvertFromString(ft.Value.Style.StrokeColor);
                                }
                                if (!string.IsNullOrWhiteSpace(p.Style.Icon))
                                {
                                    p.Style.Picture = new BitmapImage(p.Style.IconUri);
                                }
                                if (!string.IsNullOrEmpty(st.NameLabel)) p.Style.NameLabel = st.NameLabel;

                                if (ft.Value.PropertyTypeKeys != null) {
                                    var properties = ft.Value.PropertyTypeKeys.Split(';');
                                    if (properties.Length > 0)
                                    {
                                        foreach (var prop in properties)
                                        {
                                            if (resource.PropertyTypeData.ContainsKey(prop))
                                            {
                                                var rp = resource.PropertyTypeData[prop];
                                                var mi = new MetaInfo()
                                                {
                                                    Label = rp.Label,
                                                    Description = rp.Description,
                                                    VisibleInCallOut = true,
                                                    IsSearchable = true,
                                                    Section = rp.Section,
                                                    IsEditable = rp.CanEdit ?? true,
                                                    Title = rp.Title
                                                };
                                                switch (rp.Type.ToLower())
                                                {
                                                    case "string":
                                                        mi.Type = MetaTypes.text;
                                                        break;
                                                    case "number":
                                                        mi.Type = MetaTypes.number;
                                                        break;
                                                    case "date":
                                                        mi.Type = MetaTypes.datetime;
                                                        break;
                                                }
                                                p.MetaInfo.Add(mi);
                                            }

                                        }
                                    }
                                }

                                if (PoITypes.All(pt => pt.ContentId != p.ContentId))
                                    this.PoITypes.Add(p);
                                //ps.PoITypes.Add(p);

                            }

                        }


                    }
                    if (layer.TypeUrl.StartsWith(api.client.BasePath))
                    {
                        //api.resources.GetResource()    
                    }

                }
                this.PoIs.CollectionChanged += (es, tp) =>
                {

                    if (tp.NewItems != null && IsInitialized)
                    {
                        foreach (global::DataServer.PoI p in tp.NewItems)
                        {
                            var newp = (!availablePoIs.Contains(p.Id.ToString()));
                            if (newp)
                            {
                                if (IsInitialized)
                                {
                                    var f = GetFeatureFromPoi(p);
                                    WebApiService.SyncPoi(p, layer, f, api);
                                    api.features.AddFeature(layer.Id, f);
                                }
                                availablePoIs.Add(p.Id.ToString());
                            }


                        }
                        // foreach (va)
                    }
                };
                sub = api.GetLayerSubscription(layer.Id);
                sub.LayerCallback += (e, s) =>
                {
                    Execute.OnUIThread(()=> {
                        switch (s.action)
                        {
                            case LayerUpdateAction.deleteFeature:

                                var dp =
                                    PoIs.FirstOrDefault(
                                        k => k.Data.ContainsKey("cs") && ((Feature)k.Data["cs"]).Id == s.featureId);
                                if (dp != null)
                                {
                                    RemovePoi(dp);
                                }
                                break;
                            case LayerUpdateAction.updateFeature:
                                var f = GetFeature((JObject)s.item);
                                // find feature
                                var p = PoIs.FirstOrDefault(k => k.Data.ContainsKey("cs") && ((Feature)k.Data["cs"]).Id == f.Id);
                                if (p != null)
                                {
                                    // update poi
                                    UpdateFeature(f, (global::DataServer.PoI)p, layer);
                                    //TriggerContentChanged(p);
                                }
                                else
                                {
                                    // add poi  
                                    var g = Guid.NewGuid();
                                    availablePoIs.Add(g.ToString());
                                    var np = AddFeature(f, g, layer);

                                }
                                break;
                        }
                    });
                };
                foreach (var f in layer.Features)
                {
                    var p = AddFeature(f, Guid.NewGuid(), layer);
                    availablePoIs.Add(p.Id.ToString());
                }


                IsLoading = false;

                ContentLoaded = true;

                Execute.OnUIThread(() => Layer.IsLoading = false);
                PoIs.FinishBatch();
            }
            catch (Exception e)
            {
                AppState.TriggerNotification(e.Message);
            }


        }
Ejemplo n.º 2
0
        private global::DataServer.PoI AddFeature(Feature f, Guid id, Layer layer)
        {
            var p = new global::DataServer.PoI { Service = this, Id = id, PoiTypeId = f.Properties.ContainsKey("featureTypeId") ?(string)f.Properties["featureTypeId"]:null };
            UpdateFeature(f, p, layer);


            PoIs.Add(p);
            p.Deleted += (o, s) => { if (IsInitialized) api.features.DeleteFeature(f.Id, layer.Id); };

            var posChanged = Observable.FromEventPattern<PositionEventArgs>(ev => p.PositionChanged += ev,
                ev => p.PositionChanged -= ev);
            posChanged.Throttle(TimeSpan.FromSeconds(1)).Subscribe(k =>
            {
                if (f.Geometry.Coordinates is JArray)
                {
                    var coords = f.Geometry.Coordinates as JArray;
                    if (coords != null &&
                        ((double) coords[0] != p.Position.Longitude || (double) coords[1] != p.Position.Latitude))
                    {
                        ((JArray) f.Geometry.Coordinates)[0] = p.Position.Longitude;
                        ((JArray) f.Geometry.Coordinates)[1] = p.Position.Latitude;
                        //var c = ((JArray) f.Geometry.Coordinates).Select(x => (double) x).ToList();
                        //c[0] = p.Position.Longitude;
                        //c[1] = p.Position.Latitude;
                        //f.Geometry.Coordinates = c;
                        var t = api.features.UpdateFeatureAsync(f, layer.Id, f.Id);
                    }
                }
            });
            return p;
        }
Ejemplo n.º 3
0
        protected override Exception ProcessFile()
        {
           try
           {
               var json = System.IO.File.ReadAllText(File);
               int count = 1;
               JObject areas = JObject.Parse(json);
               try
               {
                   foreach (var p in areas["featureTypes"]) {
                       var property = p as JProperty;
                       if (property == null) continue;
                       var poiType = new global::DataServer.PoI {Service = this}; // EV Set the service in order to be able to set and save the style.
                       poiType.TypeFromGeoJson(property);
                       PoITypes.Add(poiType);
                   }
               }
               catch (Exception e)  // no type def?
               {
                   int i = 0;
               } 

               foreach (var f in areas["features"].Values<JObject>())
               {
                   //var lockId = f["properties"]["lock"].Value<string>();
                   //var pos = f["properties"]["pos"].Value<string>();
                   //var angle = f["properties"]["angle"].Value<double>();
                   var p = new global::DataServer.PoI { Service = this, Id = Guid.NewGuid(), PoiTypeId = "Default" };
                   p.FromGeoJson(f, false); // Into this object!
//
//                   foreach (var pr in f["properties"])
//                   {
//                       if (pr is JProperty)
//                       {
//                           var prp = (JProperty)pr;
//                           if (prp.Name == "Id")
//                           {
//                               p.Id = Guid.Parse(prp.Value.ToString());
//                           }
//                           else if (prp.Name == "FeatureTypeId")
//                           {
//                               p.PoiTypeId = prp.Value.ToString();
//                           }
//                           else
//                           {
//                               p.Labels[prp.Name] = prp.Value.ToString().RestoreInvalidCharacters();                               
//                           }
//                       }
//                   }
//
//                   try
//                   {
//                       WellKnownTextIO wkt = new WellKnownTextIO();
//                       string geometry = f["geometry"].ToString(); // TODO Probably this is very inefficiently coded.
//                       wkt.FromGeoJson(geometry, false);  // Into this WKT.
//                       p.PoiTypeId = "WKT";
//                       p.WktText = wkt.WktText;
//                   }
//                   catch (Exception e)
//                   {
//                       p.PoiTypeId = "Point";
//
//                       // TODO The code below perfectly illustrates what is wrong with the shape/position related attributes of the BaseContent class.
//                       p.Points.Add(new Point(0, 0));
//                       p.Geometry = new Geometries.Point(0, 0, 0); // { X = 0, Y = 0, Z= 0 };
//                       p.Position = new Position(0, 0);
//                   }

                   PoIs.Add(p);
                   count += 1;
               }

               IsSubscribed = true;
               return null; // Nothing went wrong.
           }
            catch (Exception e)
            {
                return e;
            }

           
        }