/// <summary> /// Creates map object from <see cref="CartoProject"/> /// </summary> /// <param name="project"></param> /// <param name="definitions"></param> /// <param name="env"></param> /// <param name="cartoTranslator"></param> /// <returns></returns> private static Map CreateMap(CartoProject project, List <CartoDefinition> definitions, Env env, ICartoTranslator cartoTranslator) { Map map = new Map(project.Name); map.Size = new SizeF(700, 500); map.MinimumScale = ConvertUtility.ToScaleDenominator(project.MinZoom); map.MaximumScale = ConvertUtility.ToScaleDenominator(project.MaxZoom); // if (project.Bounds != null) // map.WGS84Bounds = project.Bounds[0] + "," + project.Bounds[1] + "," + project.Bounds[2] + "," + project.Bounds[3]; map.CRS = cartoTranslator.ToCoordinateSystem(string.IsNullOrEmpty(project.Srs) ? project.SrsName : project.Srs, !string.IsNullOrEmpty(project.SrsName)); SetMapProperties(map, GetProperties(definitions, env, "Map"), cartoTranslator); SetFontSets(map, definitions, env, cartoTranslator); if (project.Center != null) { if (map.CoordinateSystem == null) { CoordinateSystemFactory csFactory = new CoordinateSystemFactory(); map.CoordinateSystem = (CoordinateSystem)csFactory.CreateSphericalMercatorCoordinateSystem(); } double cx = Convert.ToDouble(project.Center[0]); double cy = Convert.ToDouble(project.Center[1]); double cz = Convert.ToDouble(project.Center[2]); double scale = MapSurfer.Utilities.MapUtility.GetTileMapResolution(cz); GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation trans = CoordinateTransformationFactory.CreateCoordinateTransformation(GeographicCoordinateSystem.WGS84, map.CoordinateSystem); trans.MathTransform.Transform(ref cx, ref cy, ref cz); map.SetCenterAndZoom(cx, cy, scale); } return(map); }
public new Map Load(string fileName, IProgressIndicator progress, object userInfo) { // LogFactory.WriteLogEntry(Logger.Default, string.Format("Loading CartoCSS project from '{0}' ...", fileName), LogEntryType.Information); CartoProject cartoProject = CartoProject.FromFile(File.ReadAllText(fileName), Path.GetExtension(fileName)); return(CartoProcessor.GetMap(cartoProject, Path.GetDirectoryName(fileName), progress)); }
protected override Map OnLoad(Stream input, IProgressIndicator progress, object userInfo) { object[] objArray = (object[])userInfo; string fileName = (string)objArray[0]; // LogFactory.WriteLogEntry(Logger.Default, string.Format("Loading CartoCSS project from '{0}' ...", fileName), LogEntryType.Information); using (StreamReader sr = new StreamReader(input)) { CartoProject cartoProject = CartoProject.FromFile(sr.ReadToEnd(), Path.GetExtension(fileName)); return(CartoProcessor.GetMap(cartoProject, Path.GetDirectoryName(fileName), progress)); } }
public static CartoProject FromFile(string fileContent, string fileExt) { CartoProject cartoProject = null; switch (fileExt.ToLower()) { case ".mml": cartoProject = JsonConvert.DeserializeObject <CartoProject>(fileContent); if (cartoProject.Interactivity != null) { try { Dictionary <string, object> dict = JsonConvert.DeserializeObject <Dictionary <string, object> >(cartoProject.Interactivity.ToString()); cartoProject.Interactivity = dict; } catch { } try { bool enabled = JsonConvert.DeserializeObject <bool>(cartoProject.Interactivity.ToString()); cartoProject.Interactivity = enabled; } catch { } } break; case ".yaml": case ".yml": using (StringReader input = new StringReader(fileContent)) { Deserializer deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention(), ignoreUnmatched: true); var parser = new MergingParser(new YamlDotNet.Core.Parser(input)); cartoProject = deserializer.Deserialize <CartoProject>(new EventReader(parser)); } break; default: throw new Exception("Unknown extension of the CartoCSS project."); } cartoProject.PrepeareFormat(true); return(cartoProject); }
public static Map ReadFromFile(string fileContent, string fileName) { string path = Path.GetDirectoryName(fileName); CartoProject cartoProject = null; switch (Path.GetExtension(fileName).ToLower()) { case ".mml": cartoProject = JsonConvert.DeserializeObject <CartoProject>(fileContent); if (cartoProject.Interactivity != null) { try { Dictionary <string, object> dict = JsonConvert.DeserializeObject <Dictionary <string, object> >(cartoProject.Interactivity.ToString()); cartoProject.Interactivity = dict; } catch { } try { bool enabled = JsonConvert.DeserializeObject <bool>(cartoProject.Interactivity.ToString()); cartoProject.Interactivity = enabled; } catch { } } break; case ".yaml": using (StringReader input = new StringReader(fileContent)) { Deserializer deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention(), ignoreUnmatched: true); var parser = new MergingParser(new YamlDotNet.Core.Parser(input)); cartoProject = deserializer.Deserialize <CartoProject>(new EventReader(parser)); } break; default: throw new Exception("Unknown extension of the CartoCSS project."); } Map map = null; if (cartoProject.Stylesheet != null && cartoProject.Stylesheet.Length > 0 && cartoProject.Layers.Length > 0) { ICartoTranslator cartoTranslator = CartoGeneratorConverterFactory.CreateTranslator(cartoProject.Generator); CartoParser parser = new CartoParser(); parser.NodeProvider = new CartoNodeProvider(); Env env = new Env(); List <Ruleset> ruleSets = new List <Ruleset>(); List <CartoDefinition> definitions = new List <CartoDefinition>(); foreach (string styleName in cartoProject.Stylesheet) { string styleFileName = Path.Combine(path, styleName); try { Ruleset ruleSet = parser.Parse(File.ReadAllText(styleFileName), styleFileName, env); ruleSets.Add(ruleSet); // Get an array of Ruleset objects, flattened // and sorted according to specificitySort var defs = new List <CartoDefinition>(); defs = ruleSet.Flatten(defs, null, env); defs.Sort(new SpecificitySorter()); definitions.AddRange(defs); env.Frames.Push(ruleSet); } catch (Exception ex) { Exception ex2 = new IOException(string.Format("An error occured during parsing of the style '{0}'.", styleFileName) + ex.Message); LogFactory.WriteLogEntry(Logger.Default, ex2); throw ex2; } } string interactivityLayer = null; if (cartoProject.GetInteractivity() != null && cartoProject.GetInteractivity().ContainsKey("layer")) { interactivityLayer = cartoProject.GetInteractivity()["layer"].ToString(); } map = CreateMap(cartoProject, definitions, env, cartoTranslator); foreach (CartoLayer cartoLayer in cartoProject.Layers) { CartoDatasource datasource = cartoLayer.Datasource; StyledLayer styledLayer = CreateStyledLayer(cartoLayer, map, cartoTranslator); try { string[] classes = (cartoLayer.Class.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); Dictionary <string, bool> classIndex = new Dictionary <string, bool>(classes.Length); for (int i = 0; i < classes.Length; i++) { classIndex[classes[i]] = true; } var matching = definitions.FindAll(delegate(CartoDefinition def) { return(def.AppliesTo(cartoLayer.Name, classIndex)); }); if (matching.Count > 0) { List <CartoStyle> rules = InheritDefinitions(matching, env); if (rules.Count > 0) { SortStyles(rules, env); for (int k = 0; k < rules.Count; k++) { CartoStyle cartoStyle = rules[k]; cartoStyle.Fold(env); string styleName = cartoLayer.Name + (cartoStyle.Attachment != "__default__" ? "-" + cartoStyle.Attachment : ""); FeatureTypeStyle style = CreateStyle(styleName, cartoStyle, env, cartoTranslator); if (style.Rules.Count > 0) { styledLayer.Styles.Add(style); } } cartoTranslator.ProcessStyles(styledLayer.Styles); } if (!string.IsNullOrEmpty(interactivityLayer) && interactivityLayer.Equals(styledLayer.Name)) { styledLayer.Enabled = false; } map.AddLayer(styledLayer); } } catch (Exception ex) { Exception ex2 = new IOException(string.Format("Unable to create data source provider with type '{0}' for the layer '{1}'.", datasource.Type, cartoLayer.Name) + ex.Message); LogFactory.WriteLogEntry(Logger.Default, ex2); } } } return(map); }