示例#1
0
        public override GOFeature ParseFeatureData(VectorTileFeature feature, GOLayer layer)
        {
            IDictionary properties = feature.GetProperties();
            GOFeature   goFeature;

            if (layer.layerType == GOLayer.GOLayerType.Roads)
            {
                goFeature = new GORoadFeature();
            }
            else
            {
                goFeature = new GOFeature();
            }

            goFeature.kind = GOEnumUtils.MapboxToKind(layer.name);

            goFeature.y = layer.defaultLayerY();
            if (properties.Contains("_symbol"))
            {
                goFeature.y = Convert.ToInt64(properties ["_symbol"]) / 10.0f * Global.tilesizeRank;
            }

            goFeature.height = layer.defaultRendering.polygonHeight;

            return(goFeature);
        }
 public GOFeatureMeshBuilder(GOFeature f)
 {
     feature = f;
     if (feature.goFeatureType == WaveMap.GOFeature.GOFeatureType.Polygon || feature.goFeatureType == WaveMap.GOFeature.GOFeatureType.MultiPolygon)
     {
         center = feature.convertedGeometry.Aggregate((acc, cur) => acc + cur) / feature.convertedGeometry.Count;
     }
 }
        public override GOFeature ParseFeatureData(VectorTileFeature feature, GOLayer layer)
        {
            IDictionary properties = feature.GetProperties();
            GOFeature   goFeature;

            if (layer.layerType == GOLayer.GOLayerType.Roads)
            {
                goFeature = new GORoadFeature();
                ((GORoadFeature)goFeature).isBridge = properties.Contains("brunnel") && (string)properties ["brunnel"] == "bridge";
                ((GORoadFeature)goFeature).isTunnel = properties.Contains("brunnel") && (string)properties ["brunnel"] == "tunnel";
                ((GORoadFeature)goFeature).isLink   = properties.Contains("brunnel") && (string)properties ["brunnel"] == "link";
            }
            else
            {
                goFeature = new GOFeature();
            }

            goFeature.kind = GOEnumUtils.MapboxToKind((string)properties["class"]);

            goFeature.y = goFeature.index / 1000 + layer.defaultLayerY();

            if (goFeature.kind == GOFeatureKind.lake)             //terrible fix for vector maps without a sort value.
            {
                goFeature.y = layer.defaultLayerY(GOLayer.GOLayerType.Landuse);
            }

            goFeature.height = layer.defaultRendering.polygonHeight;

            if (layer.useRealHeight && properties.Contains("render_height"))
            {
                double h = Convert.ToDouble(properties["render_height"]);
                goFeature.height = (float)h;
            }

            if (layer.useRealHeight && properties.Contains("render_min_height"))
            {
                double hm = Convert.ToDouble(properties["render_min_height"]);
                goFeature.y = (float)hm;
                if (goFeature.height >= hm)
                {
                    goFeature.y      = (float)hm;
                    goFeature.height = (float)goFeature.height - (float)hm;
                }
            }

            return(goFeature);
        }
        public GOFeature ParseFeatureData(IDictionary properties, GOLayer layer)
        {
            GOFeature goFeature;

            if (layer.layerType == GOLayer.GOLayerType.Roads)
            {
                goFeature = new GORoadFeature();

                goFeature.kind = GOEnumUtils.MapzenToKind((string)properties ["kind"]);
                if (properties.Contains("kind_detail"))                   //Mapzen
                {
                    goFeature.detail = (string)properties ["kind_detail"];
                }

                ((GORoadFeature)goFeature).isBridge = properties.Contains("is_bridge") && properties ["is_bridge"].ToString() == "True";
                ((GORoadFeature)goFeature).isTunnel = properties.Contains("is_tunnel") && properties ["is_tunnel"].ToString() == "True";
                ((GORoadFeature)goFeature).isLink   = properties.Contains("is_link") && properties ["is_link"].ToString() == "True";
            }
            else
            {
                goFeature      = new GOFeature();
                goFeature.kind = GOEnumUtils.MapzenToKind((string)properties ["kind"]);
                if (properties.Contains("kind_detail"))                   //Mapzen
                {
                    goFeature.kind = GOEnumUtils.MapzenToKind((string)properties ["kind_detail"]);
                }
            }

            goFeature.name = (string)properties ["name"];

            Int64 sort = 0;

            if (properties.Contains("sort_rank"))
            {
                sort = (Int64)properties ["sort_rank"];
            }
            else if (properties.Contains("sort_key"))
            {
                sort = (Int64)properties ["sort_key"];
            }
            goFeature.y    = sort / 1000.0f;
            goFeature.sort = sort;

            goFeature.height = layer.defaultRendering.polygonHeight;

            if (layer.useRealHeight && properties.Contains("height"))
            {
                double h = Convert.ToDouble(properties["height"]);
                goFeature.height = (float)h;
            }

            if (layer.useRealHeight && properties.Contains("min_height"))
            {
                double hm = Convert.ToDouble(properties["min_height"]);
                goFeature.y = (float)hm;
                if (goFeature.height >= hm)
                {
                    goFeature.y      = (float)hm;
                    goFeature.height = (float)goFeature.height - (float)hm;
                }
            }


            return(goFeature);
        }
        public override IEnumerator BuildTile(IDictionary mapData, GOLayer layer, bool delayedLoad)
        {
            GameObject parent = new GameObject();

            parent.name             = layer.name;
            parent.transform.parent = this.transform;
            parent.SetActive(!layer.startInactive);

            if (mapData == null)
            {
                Debug.LogWarning("Map Data is null!");
                                #if !UNITY_WEBPLAYER
                FileHandler.Remove(gameObject.name);
                                #endif
                yield break;
            }

            IList features = (IList)mapData ["features"];

            if (features == null)
            {
                yield break;
            }

            IList stack = new List <GOFeature> ();

            foreach (IDictionary geo in features)
            {
                IDictionary geometry   = (IDictionary)geo ["geometry"];
                IDictionary properties = (IDictionary)geo ["properties"];
                string      type       = (string)geometry ["type"];

                string kind = (string)properties ["kind"];
                if (properties.Contains("kind_detail"))
                {
                    kind = (string)properties ["kind_detail"];
                }

                var id = properties ["id"];
                if (idCheck(id, layer) == false && layer.layerType == GOLayer.GOLayerType.Buildings)
                {
                    continue;
                }

                if (layer.useOnly.Length > 0 && !layer.useOnly.Contains(GOEnumUtils.MapzenToKind(kind)))
                {
                    continue;
                }
                if (layer.avoid.Length > 0 && layer.avoid.Contains(GOEnumUtils.MapzenToKind(kind)))
                {
                    continue;
                }

                if (type == "MultiLineString" || (type == "Polygon" && !layer.isPolygon))
                {
                    IList lines = new List <object>();
                    lines = (IList)geometry ["coordinates"];
                    foreach (IList coordinates in lines)
                    {
                        GOFeature gf = ParseFeatureData(properties, layer);
                        gf.geometry   = coordinates;
                        gf.type       = type;
                        gf.layer      = layer;
                        gf.parent     = parent;
                        gf.properties = properties;
                        gf.ConvertGeometries();
                        gf.ConvertAttributes();
                        gf.index         = (Int64)features.IndexOf(geo);
                        gf.goFeatureType = GOFeature.GOFeatureType.MultiLine;
                        stack.Add(gf);
                    }
                }

                else if (type == "LineString")
                {
                    IList     coordinates = (IList)geometry ["coordinates"];
                    GOFeature gf          = ParseFeatureData(properties, layer);
                    gf.geometry   = coordinates;
                    gf.type       = type;
                    gf.layer      = layer;
                    gf.parent     = parent;
                    gf.properties = properties;
                    gf.ConvertGeometries();
                    gf.ConvertAttributes();
                    gf.index         = (Int64)features.IndexOf(geo);
                    gf.goFeatureType = GOFeature.GOFeatureType.Line;
                    stack.Add(gf);
                }

                else if (type == "Polygon")
                {
                    List <object> shapes = new List <object>();
                    shapes = (List <object>)geometry["coordinates"];

                    IList         subject = null;
                    List <object> clips   = null;
                    if (shapes.Count == 1)
                    {
                        subject = (List <object>)shapes[0];
                    }
                    else if (shapes.Count > 1)
                    {
                        subject = (List <object>)shapes[0];
                        clips   = shapes.GetRange(1, shapes.Count - 1);
                    }
                    else
                    {
                        continue;
                    }

                    GOFeature gf = ParseFeatureData(properties, layer);
                    gf.geometry   = subject;
                    gf.clips      = clips;
                    gf.type       = type;
                    gf.layer      = layer;
                    gf.parent     = parent;
                    gf.properties = properties;
                    gf.ConvertGeometries();
                    gf.ConvertAttributes();
                    gf.index         = (Int64)features.IndexOf(geo);
                    gf.goFeatureType = GOFeature.GOFeatureType.Polygon;
                    stack.Add(gf);
                }

                if (type == "MultiPolygon")
                {
                    GameObject multi = new GameObject("MultiPolygon");
                    multi.transform.parent = parent.transform;

                    IList shapes = new List <object>();
                    shapes = (IList)geometry["coordinates"];

                    foreach (List <object> polygon in shapes)
                    {
                        IList         subject = null;
                        List <object> clips   = null;
                        if (polygon.Count > 0)
                        {
                            subject = (List <object>)polygon[0];
                        }
                        else if (polygon.Count > 1)
                        {
                            clips = polygon.GetRange(1, polygon.Count - 1);
                        }
                        else
                        {
                            continue;
                        }

                        GOFeature gf = ParseFeatureData(properties, layer);
                        gf.geometry   = subject;
                        gf.clips      = clips;
                        gf.type       = type;
                        gf.layer      = layer;
                        gf.parent     = parent;
                        gf.properties = properties;
                        gf.ConvertGeometries();
                        gf.ConvertAttributes();
                        gf.index         = (Int64)features.IndexOf(geo);
                        gf.goFeatureType = GOFeature.GOFeatureType.MultiPolygon;

                        stack.Add(gf);
                    }
                }
            }

            if (layer.layerType == GOLayer.GOLayerType.Roads)
            {
                stack = GORoadFeature.MergeRoads(stack);
            }

            int n = 25;
            for (int i = 0; i < stack.Count; i += n)
            {
                for (int k = 0; k < n; k++)
                {
                    if (i + k >= stack.Count)
                    {
                        yield return(null);

                        break;
                    }

                    GOFeature   r       = (GOFeature)stack [i + k];
                    IEnumerator routine = r.BuildFeature(this, delayedLoad);
                    if (routine != null)
                    {
                        if (Application.isPlaying)
                        {
                            StartCoroutine(routine);
                        }
                        else
                        {
                            GORoutine.start(routine, this);
                        }
                    }
                }
                yield return(null);
            }

            yield return(null);
        }
        public GameObject BuildPolygon(string name, GOLayer layer, float height)
        {
            //GameObject polygon = GameObject.Instantiate((GameObject)Resources.Load("Prefabs/CityBuildings/" + "Building_CornerHouse_A"));
            GameObject polygon = new GameObject();

            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = feature.convertedGeometry;
            if (feature.clips != null)
            {
                foreach (IList clipVerts in feature.clips)
                {
                    poly.holes.Add(GOFeature.CoordsToVerts(clipVerts));
                }
            }

            MeshFilter filter = polygon.AddComponent <MeshFilter>();

            polygon.AddComponent(typeof(MeshRenderer));

            try {
                mesh = Poly2Mesh.CreateMesh(poly);
            } catch {
            }
            if (height > 1)
            {
                if (mesh)
                {
                    Vector2[] uv2d = new Vector2[mesh.vertices.Length];
                    for (int i = 0; i < uv2d.Length; i++)
                    {
                        uv2d[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
                    }
                    var meshlemp = Mesh.Instantiate(mesh);
                    meshlemp.uv = uv2d;
                    mesh2D      = Mesh.Instantiate(meshlemp);

                    mesh.uv = uv2d;
                    if (height > 0)
                    {
                        mesh = SimpleExtruder.Extrude(mesh, polygon, height);
                        Vector2[] uvs3D = new Vector2[mesh.vertices.Length];
                        for (int i = 0; i < uvs3D.Length - 1; i++)
                        {
                            uvs3D[i] = new Vector2(Vector2.Distance(new Vector2(mesh.vertices[i + 1].x, mesh.vertices[i + 1].z), new Vector2(mesh.vertices[i].x, mesh.vertices[i].z)), mesh.vertices[i].y);
                        }
                        // uvs2[uvs2.Length - 1] = new Vector2(Mathf.Sqrt((float)(Math.Pow(mesh.vertices[0].x - mesh.vertices[uvs2.Length - 1].x, 2) + Math.Pow(mesh.vertices[0].z - mesh.vertices[uvs2.Length - 1].z, 2))), mesh.vertices[uvs2.Length - 1].y);
                        uvs3D[uvs3D.Length - 1] = new Vector2(10, mesh.vertices[uvs3D.Length - 1].y);
                        mesh.uv = uvs3D;
                    }
                }
            }
            else
            {
                if (mesh)
                {
                    Vector2[] uvs = new Vector2[mesh.vertices.Length];
                    for (int i = 0; i < uvs.Length; i++)
                    {
                        uvs[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
                    }
                    mesh.uv = uvs;
                    if (height > 0)
                    {
                        mesh = SimpleExtruder.Extrude(mesh, polygon, height);
                    }
                }
            }
            filter.sharedMesh = mesh;

            if (layer.useColliders)
            {
                polygon.AddComponent <MeshCollider>().sharedMesh = mesh;
            }
            return(polygon);
        }
        public GameObject BuildPolygon(string name, Vector3 pos)
        {
            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = feature.convertedGeometry;
            if (feature.clips != null)
            {
                foreach (IList clipVerts in feature.clips)
                {
                    poly.holes.Add(GOFeature.CoordsToVerts(clipVerts));
                }
            }
            try
            {
                mesh = Poly2Mesh.CreateMesh(poly);
            }
            catch
            {
            }
            float   d     = 0f;
            Vector3 f_Pos = pos;

            for (int i = 0; i < mesh.vertices.Length; i++)
            {
                var l = Vector2.Distance(pos, mesh.vertices[i]);
                if (d < l)
                {
                    d     = l;
                    f_Pos = mesh.vertices[i];
                }
            }
            float ea_y = GetSC(f_Pos, pos);
            var   sc   = d / 1.414f;

            if (sc > 2f)
            {
                sc = sc / 4;
                GameObject polygon = new GameObject("polygon");
                polygon.transform.localPosition = pos;
                //for (int i = 0;i<2;i++)
                // {
                //    for (int j = 0;j<2;j++)
                //   {
                GameObject item = GameObject.Instantiate((GameObject)Resources.Load("Prefabs/CityBuildings/" + "B_10"));
                item.transform.localPosition    = pos;     //new Vector3((pos.x-sc/2)+sc*i,0, (pos.z - sc / 2) + sc * j) ;
                item.transform.localEulerAngles = new Vector3(0, ea_y, 0);
                item.transform.localScale       = Vector3.one * (sc * Global.tilesizeRank);
                item.transform.SetParent(polygon.transform);
                //  }
                //   }
                polygon.transform.Rotate(0, ea_y, 0);
                return(polygon);
            }
            else
            {
                if (sc < 0.8f)
                {
                    sc = 1f;
                }
                GameObject polygon = GameObject.Instantiate((GameObject)Resources.Load("Prefabs/CityBuildings/" + "B_" + UnityEngine.Random.Range(3, 10)));
                polygon.transform.localPosition    = pos;
                polygon.transform.localEulerAngles = new Vector3(0, ea_y, 0);
                polygon.transform.localScale       = Vector3.one * (sc * Global.tilesizeRank);
                return(polygon);
            }
        }
 public static bool IsGeoPolygonClockwise(IList coords)
 {
     return(IsClockwise(GOFeature.CoordsToVerts(coords)));
 }
示例#9
0
        public IEnumerator BuildLayer(VectorTileLayer layerData, GOLayer layer, bool delayedLoad)
        {
            GameObject parent = null;

            if (transform.FindChild(layer.name) == null)
            {
                parent                  = new GameObject();
                parent.name             = layer.name;
                parent.transform.parent = this.transform;
                parent.SetActive(!layer.startInactive);
            }
            else
            {
                parent = transform.FindChild(layer.name).gameObject;
            }


            if (layerData.FeatureCount() == 0)
            {
                yield break;
            }


            List <GOFeature> stack = new List <GOFeature> ();

            for (int i = 0; i < layerData.FeatureCount(); i++)
            {
                //get the feature
                VectorTileFeature feature   = layerData.GetFeature(i);
                GOFeature         goFeature = ParseFeatureData(feature, layer);

                if (layer.useOnly.Length > 0 && !layer.useOnly.Contains(goFeature.kind))
                {
                    continue;
                }
                if (layer.avoid.Length > 0 && layer.avoid.Contains(goFeature.kind))
                {
                    continue;
                }

                if (layer.layerType == GOLayer.GOLayerType.Roads)
                {
                    GORoadFeature grf = (GORoadFeature)goFeature;
                    if ((grf.isBridge && !layer.useBridges) || (grf.isTunnel && !layer.useTunnels) || (grf.isLink && !layer.useBridges))
                    {
                        continue;
                    }
                }

                //multipart
                List <List <LatLng> > geomWgs84 = feature.GeometryAsWgs84((ulong)map.zoomLevel, (ulong)tileCoordinates.x, (ulong)tileCoordinates.y, 0);

                string type = feature.GetFeatureType(geomWgs84);
                if (type == null || feature.GeometryType == GeomType.POINT)
                {
                    continue;
                }
                if (feature.GeometryType == GeomType.POLYGON && layer.layerType == GOLayer.GOLayerType.Roads)
                {
                    continue;
                }

                if (type == "MultiLineString" || (type == "Polygon" && !layer.isPolygon))
                {
                    foreach (IList geometry in geomWgs84)
                    {
                        GOFeature gf = ParseFeatureData(feature, layer);
                        gf.geometry   = geometry;
                        gf.type       = type;
                        gf.layer      = layer;
                        gf.parent     = parent;
                        gf.properties = feature.GetProperties();
                        gf.ConvertGeometries();
                        gf.ConvertAttributes();
                        gf.index         = (Int64)i;
                        gf.goFeatureType = GOFeature.GOFeatureType.MultiLine;
                        stack.Add(gf);
                    }
                }
                else if (type == "LineString")
                {
                    GOFeature gf = ParseFeatureData(feature, layer);
                    gf.geometry   = geomWgs84 [0];
                    gf.type       = type;
                    gf.layer      = layer;
                    gf.parent     = parent;
                    gf.properties = feature.GetProperties();
                    gf.ConvertGeometries();
                    gf.ConvertAttributes();
                    gf.index         = (Int64)i;
                    gf.goFeatureType = GOFeature.GOFeatureType.Line;
                    if (geomWgs84.Count == 0)
                    {
                        continue;
                    }

                    stack.Add(gf);
                }

                else if (type == "Polygon")
                {
                    GOFeature gf = ParseFeatureData(feature, layer);
                    gf.geometry   = geomWgs84 [0];
                    gf.type       = type;
                    gf.layer      = layer;
                    gf.parent     = parent;
                    gf.properties = feature.GetProperties();
                    gf.ConvertGeometries();
                    gf.ConvertAttributes();
                    gf.index         = (Int64)i;
                    gf.goFeatureType = GOFeature.GOFeatureType.Polygon;

                    stack.Add(gf);
                }

                else if (type == "MultiPolygon")
                {
                    GameObject multi = new GameObject("MultiPolygon");
                    multi.transform.parent = parent.transform;

                    IList subject = null;
                    IList clips   = new List <List <LatLng> >();

                    for (int j = 0; j < geomWgs84.Count; j++)                     //Clip ascending

                    {
                        IList p = geomWgs84 [j];
                        if (GOFeature.IsGeoPolygonClockwise(p))
                        {
                            subject = p;
                        }
                        else
                        {
                            //Add clip
                            clips.Add(p);
                        }
                        //Last one
                        if (j == geomWgs84.Count - 1 || (j < geomWgs84.Count - 1 && GOFeature.IsGeoPolygonClockwise(geomWgs84 [j + 1]) && subject != null))
                        {
                            GOFeature gf = ParseFeatureData(feature, layer);
                            gf.geometry   = subject;
                            gf.clips      = clips;
                            gf.type       = type;
                            gf.layer      = layer;
                            gf.parent     = parent;
                            gf.properties = feature.GetProperties();
                            gf.ConvertGeometries();
                            gf.ConvertAttributes();
                            gf.index         = (Int64)i;
                            gf.goFeatureType = GOFeature.GOFeatureType.MultiPolygon;

                            stack.Add(gf);

                            subject = null;
                            clips   = new List <List <LatLng> >();
                        }
                    }
                }
            }

            IList iStack = (IList)stack;

            if (layer.layerType == GOLayer.GOLayerType.Roads)
            {
                iStack = GORoadFeature.MergeRoads(iStack);
            }

            int n = 25;

            for (int i = 0; i < iStack.Count; i += n)
            {
                for (int k = 0; k < n; k++)
                {
                    if (i + k >= iStack.Count)
                    {
                        yield return(null);

                        break;
                    }

                    GOFeature   r       = (GOFeature)iStack [i + k];
                    IEnumerator routine = r.BuildFeature(this, delayedLoad);
                    if (routine != null)
                    {
                        if (Application.isPlaying)
                        {
                            StartCoroutine(routine);
                        }
                        else
                        {
                            GORoutine.start(routine, this);
                        }
                    }
                }
                yield return(new WaitForSeconds(0.5f));
            }

            Resources.UnloadUnusedAssets();

            yield return(null);
        }