public override void Open(IGeoModel model) { Debug.Assert(model != null); builder = model.CreateGridBuilder(); Debug.Assert(builder != null); using (ILineReader lineReader = new SkippingLineReader(Location.LocalPath)) { ReadHeader(lineReader); BuildGridParameters(); switch (maxFieldCode) { case 3: // XYZ points not supported // TODO: Signal not supported break; case 5: ReadFiveColumnGrid(lineReader); break; case 7: ReadFiveColumnGrid(lineReader); // Ignore the ID and name columns break; default: // Unknown // TODO: Inform user. break; } } builder.Build(); }
static int Main(string[] args) { Uri uri; try { uri = new Uri(args[0]); } catch (UriFormatException) { string absPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + args[0]; try { uri = new Uri(absPath); } catch (UriFormatException) { return(1); } } IGeoModel model = Factory <IGeoModel> .Instance.Create("Test"); LoaderController.Instance.Open(uri, model); Console.ReadKey(); return(0); }
public override void Open(IGeoModel model) { if (entityType == null) { // TODO: Translate exceptions into OpenExceptions entityType = DetermineType(); if (entityType == null) { StringBuilder message = new StringBuilder(); throw new OpenException("Unable to determine CPS3 entity type"); } } if (Factory <ICps3EntityLoader> .Instance.ContainsKey(entityType)) { ICps3EntityLoader entityLoader = Factory <ICps3EntityLoader> .Instance.Create(entityType); entityLoader.Location = Location; entityLoader.Open(model); } else { StringBuilder message = new StringBuilder(); message.AppendFormat("No loader for CPS3 entity {0}", entityType); throw new OpenException(message.ToString()); } }
public override void Open(IGeoModel model) { Debug.Assert(model != null); builder = model.CreateGridBuilder(); Debug.Assert(builder != null); using (FileStream stream = new FileStream(Location.LocalPath, FileMode.Open, FileAccess.Read)) using (BinaryReader reader = EndianBinaryReader.CreateForBigEndianData(stream)) { // We keep track of our own length rather than testing for EOF which is // inefficient with FileStream long length = stream.Length; if (length < 4) { throw new OpenException("EarthVision Binary File too short"); } long position = 0; bool foundMagic = ReadMagic(reader); if (!foundMagic) { StringBuilder message = new StringBuilder(); message.AppendFormat("Incorrect EarthVision Binary Grid header"); throw new OpenException(message.ToString()); } position += 4; while (position < length) { ReadRecord(reader, ref position); } } builder.Build(); }
public override void Open(IGeoModel model) { Debug.Assert(model != null); builder = model.CreateGridBuilder(); ReadHeader(); ReadBody(); builder.Build(); }
public void Open(Uri uri, IGeoModel model, string key) { bool finished = true; do { ILoader loader = loaderFactory.Create(key); finished = OpenWithFailover(loader, model, finished, false); }while (!finished); }
private static bool OpenWithFailover(ILoader loader, IGeoModel model, bool finished, bool showTryOthers) { // TODO: Maybe we can use the finished parameter to determine whether // we need to present the 'Try Others' option. Maybe the finished // arg should be renamed to hideTryOthers... bool tryAgain = false; do { try { loader.Open(model); finished = true; break; } catch (OpenException openException) { // TODO: Notify the user of the open failure // "Failed to open URI because ... Console.WriteLine(openException.Message); // Do you want to Try again? Abort? Try other loaders? List <Pair <object, string> > choices = new List <Pair <object, string> >(3); choices.Add(new Pair <object, string>(OpenChoice.TryAgain, "Try Again")); choices.Add(new Pair <object, string>(OpenChoice.TryOthers, "Try Other Formats")); choices.Add(new Pair <object, string>(OpenChoice.Abort, "Cancel")); IChoiceList choiceList = Factory <IChoiceList> .Instance.CreateDefault(choices); OpenChoice decision = (OpenChoice)choiceList.Show(); switch (decision) { case OpenChoice.TryAgain: tryAgain = true; finished = false; break; case OpenChoice.TryOthers: tryAgain = false; finished = false; break; case OpenChoice.Abort: tryAgain = false; finished = true; break; } } }while (tryAgain); return(finished); }
public void Open(IGeoModel model) { Debug.Assert(model != null); using (SkippingLineReader lineReader = new SkippingLineReader(uri.LocalPath, commentBlanksPattern)) { int numSurfaces = ReadHeader(lineReader); // Read the body string line; while ((line = lineReader.ReadLine()) != null) { // Split the fields in the line string name = line.Substring(0, 16).Trim(); string shotPoint = line.Substring(16, 10).Trim(); double x = Double.Parse(line.Substring(26, 10).Trim()); double y = Double.Parse(line.Substring(36, 10).Trim()); // Retrieve the next n values, where n is the number of horizons string[] zs = line.Substring(46).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (zs.Length < numSurfaces) { StringBuilder message = new StringBuilder(); message.AppendFormat("Too few z values at line {0}", lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), uri); } double[] z = new double[numSurfaces]; for (int i = 0; i < numSurfaces; ++i) { // Charisma Z values may have non-numeric characters // within the field, such as 1234.45F so we use a regex // to extract the substring the matches a float Match match = Patterns.CFloat.Match(zs[i]); if (match.Success) { z[i] = Double.Parse(match.Value); } else { StringBuilder message = new StringBuilder(); message.AppendFormat("Bad value '{0}' at line {1}", zs[i], lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), uri); } } // TODO: Call the builder here } } }
public override void Open(IGeoModel model) { // Read all of the Z values into an redimensionable array. Regex delimiterPattern = new Regex(@"[\s,;]+"); using (SkippingLineReader lineReader = new SkippingLineReader(Location.LocalPath, commentBlanksPattern)) { try { string line; // TODO: This redimensionable array needs to be a new type // containing a 1-D array, but which we can view as // a 2-D array with different sizes. For now, we use // a List List <double> redimensionableArray = new List <double>(); while ((line = lineReader.ReadLine()) != null) { string[] fields = delimiterPattern.Split(line); foreach (string field in fields) { double value = Double.Parse(field); redimensionableArray.Add(value); } } } catch (FormatException formatException) { StringBuilder message = new StringBuilder(); message.AppendFormat("Invalid field value at line {0}", lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), Location, formatException); } } // TODO: Write AsciiScanlineZ heuristics // Try to guess the null value using statistics and by testing // commonly used values. Null value could be the most common. // Factorize the length of the z array into a set of factors // Try each pair of common factors to determine which produces // the smoothest grid: Test each node against those to its north // and east - sum the differences. The grid with the smallest // total difference is the smoothest. // TODO: Write AsciiScanlineZ loader }
public void Open(IGeoModel model) { Debug.Assert(uri != null); Debug.Assert(model != null); builder = model.CreateGridBuilder(); int postHeaderLine = ParseHeader(); // Read the body of the grid, skipping comment and blank lines using (SkippingLineReader lineReader = new SkippingLineReader(uri.LocalPath, Cps3.commentBlanksPattern)) { // Skip forward over the header while (lineReader.PhysicalLineNumber < postHeaderLine) { lineReader.ReadLineNoSkip(); } // Parse the body of the grid string line; int counter = 0; while ((line = lineReader.ReadLine()) != null) { string[] fields = Regex.Split(line, @"\s+"); foreach (string field in fields) { double z; if (double.TryParse(field, out z)) { int i = counter % parameters.INum.Value; int j = counter / parameters.INum.Value; if (z != parameters.ZNull) { builder[i, j] = z; } ++counter; } else { StringBuilder message = new StringBuilder(); message.AppendFormat("Bad grid data '{0}' at line {1}", field, lineReader.PhysicalLineNumber); throw new OpenException(message.ToString()); } } } } }
public override void Open(IGeoModel model) { Debug.Assert(model != null); builder = model.CreateGridBuilder(); using (LineReader lineReader = new LineReader(Location.LocalPath)) { try { ReadHeader(lineReader); ReadRowMajorGrid(lineReader); } catch (EndOfStreamException endOfStreamException) { throw new OpenException("Unexpected end of file", Location, endOfStreamException); } } builder.Build(); }
public void Read(ILineReader lineReader, IGeoModel model) { // Pre-condition: The next line to be read will be the first line // of the header builder = model.CreateGridBuilder(); try { ReadHeader(lineReader); parameters.VerifyAndCompleteParameters(); parameters.Build(builder); ReadBody(lineReader); } catch (FormatException formatException) { StringBuilder message = new StringBuilder(); message.AppendFormat("Unable to parse ZMap file at line {0} because {1}", lineReader.PhysicalLineNumber, formatException.Message); throw new OpenException(message.ToString()); } builder.Build(); }
public override void Open(IGeoModel model) { using (SkippingLineReader lineReader = new SkippingLineReader(Location.LocalPath, commentHistoryBlanksPattern)) { string type = DetermineType(lineReader); if (!typeLoaders.ContainsKey(type)) { StringBuilder message = new StringBuilder(); message.AppendFormat("Unrecognized ZMap object type code '{0}' at line {1}", type, lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), Location); } string key = typeLoaders[type]; if (!Factory <IZMapReader> .Instance.ContainsKey(key)) { StringBuilder message = new StringBuilder(); message.AppendFormat("ZMap object type code '{0}' recognized at line {1}, but no loader has been implemented.", type, lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), Location); } lineReader.UnreadLine(); IZMapReader zMapReader = Factory <IZMapReader> .Instance.Create(key); zMapReader.Read(lineReader, model); } }
public override void Open(IGeoModel model) { Debug.Assert(model != null); builder = model.CreateGridBuilder(); DetermineType(); switch (subType) { case SubType.SurferAscii: OpenSurferAscii(); break; case SubType.Surfer6Binary: OpenSurfer6Binary(); break; case SubType.Surfer7Binary: OpenSurfer7Binary(); break; default: throw new OpenException("Not recognised as a Golden Software Surfer Binary grid file", Location); } builder.Build(); }
public override void Open(IGeoModel model) { }
public void Open(Uri uri, IGeoModel model) { string localPath = uri.LocalPath; string uriBasename = Path.GetFileName(localPath); // TODO: Some basic checks such as existence of the file // should be done here. List <object> rankedKeys = new List <object>(); foreach (object key in loaderFactory.Keys) { ILoader loader = loaderFactory.Create(key); loader.Location = uri; bool matched = false; foreach (Regex pattern in loader.UriPatterns) { if (pattern.IsMatch(uriBasename)) { matched = true; break; } } if (matched) { rankedKeys.Insert(0, key); } else { rankedKeys.Add(key); } } bool finished = false; List <object> maybeKeys = new List <object>(); foreach (string key in rankedKeys) { ILoader loader = loaderFactory.Create(key); loader.Location = uri; // TODO: Wrap this is a try..catch and deal with failures such // as file not found. Recognition recognition = loader.Recognize(); if (recognition == Recognition.Yes) { finished = OpenWithFailover(loader, model, finished, true); if (finished) { break; } maybeKeys.Add(key); } else if (recognition == Recognition.Maybe) { maybeKeys.Add(key); } } if (!finished) { if (maybeKeys.Count == 1) { ILoader loader = loaderFactory.Create(maybeKeys[0]); loader.Location = uri; finished = OpenWithFailover(loader, model, finished, false); } else { do { // Present the user with a choice List <Pair <object, string> > choices = new List <Pair <object, string> >(); foreach (object key in maybeKeys) { ILoader loader = loaderFactory.Create(key); loader.Location = uri; choices.Add(new Pair <object, string>(key, loader.Description)); } IChoiceList choiceList = Factory <IChoiceList> .Instance.CreateDefault(choices); object chosenKey = choiceList.Show(); if (chosenKey != null) { ILoader loader = loaderFactory.Create(chosenKey); loader.Location = uri; finished = OpenWithFailover(loader, model, finished, true); } else { finished = true; } }while (!finished); } } }
public abstract void Open(IGeoModel model);