public MIFProvider(string mifpath) { try { m_miffile = MIFParser.LoadFile(mifpath); } catch (Exception ex) { throw new Exception("Couldn't read MIF file " + mifpath + "\nError: " + ex.Message); } }
/// <summary> /// This will parse the given mif and its counterpart /// </summary> /// <param name="path"></param> /// <returns></returns> public static MapInfoLayer LoadFile(string path) { MapInfoLayer layer = new MapInfoLayer(Path.GetFileNameWithoutExtension(path)); //read MID string midfilepath = Path.GetDirectoryName(path) + "\\" + Path.GetFileNameWithoutExtension(path) + ".mid"; if (!File.Exists(midfilepath)) { throw new Exception("Kunne ikke finde MID-fil: \"" + midfilepath + "\""); } StreamReader sr = new StreamReader(midfilepath, Encoding.Default); string[] midlines = sr.ReadToEnd().Trim().Split('\n'); sr.Close(); layer.Objects = new MapInfoObject[midlines.Length]; for (int i = 0; i < midlines.Length; i++) { layer.Objects[i] = new MapInfoObject(layer); layer.Objects[i].LoadFromMIDLine(midlines[i]); } //read MIF if (!File.Exists(path)) { throw new Exception("Kunne ikke finde MIF-fil"); } sr = new StreamReader(path, Encoding.Default); string[] miflines = sr.ReadToEnd().Trim().Split('\n'); sr.Close(); //coordsys int l = 0; while (!miflines[l].StartsWith("CoordSys")) { l++; } layer.CoordinateSystem = miflines[l]; //Index if (miflines.Length >= 4 && miflines[3].StartsWith("Index ")) { layer.Index = miflines[3].Substring(6).Trim(); } //columns l = 0; while (!miflines[l].StartsWith("Columns")) { l++; } layer.ColumnNames = new string[int.Parse(miflines[l].Substring(8))]; layer.ColumnTypes = new string[int.Parse(miflines[l].Substring(8))]; l++; int c = 0; string[] e; while (!miflines[l].StartsWith("Data")) { e = miflines[l].Trim().Split(' '); layer.ColumnNames[c] = e[0]; layer.ColumnTypes[c] = e[1]; if (layer.ColumnTypes[c].StartsWith("Decimal(")) { layer.ColumnTypes[c] += " " + e[2]; } c++; l++; } l++; //objects int curobj = 0; while (l < miflines.Length) { //find object start while (l < miflines.Length && (miflines[l] == "" || miflines[l][0] == '\r' || miflines[l][0] == ' ')) { l++; } if (l == miflines.Length) { break; } if (l < miflines.Length && miflines[l].StartsWith("Region ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Region; layer.Objects[curobj].SubObjects = new double[int.Parse(miflines[l++].Substring(7))][]; for (int re = 0; re < layer.Objects[curobj].SubObjects.Length; re++) { layer.Objects[curobj].SubObjects[re] = new double[int.Parse(miflines[l++].Trim()) * 2]; for (int i = 0; i < layer.Objects[curobj].SubObjects[re].Length; i += 2) { double[] coord = ParseCoordinatePair(miflines[l++]); layer.Objects[curobj].SubObjects[re][i] = coord[0]; layer.Objects[curobj].SubObjects[re][i + 1] = coord[1]; } } } else if (l < miflines.Length && miflines[l].StartsWith("Point ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Point; layer.Objects[curobj].SubObjects = new double[1][]; layer.Objects[curobj].SubObjects[0] = ParseCoordinatePair(miflines[l++].Substring(6)); } else if (l < miflines.Length && miflines[l].StartsWith("Pline Multiple")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Line; layer.Objects[curobj].SubObjects = new double[int.Parse(miflines[l++].Substring(15))][]; for (int i = 0; i < layer.Objects[curobj].SubObjects.Length; i++) { layer.Objects[curobj].SubObjects[i] = new double[int.Parse(miflines[l++].Trim()) * 2]; for (int j = 0; j < layer.Objects[curobj].SubObjects[i].Length; j += 2) { double[] coord = ParseCoordinatePair(miflines[l++]); layer.Objects[curobj].SubObjects[i][j] = coord[0]; layer.Objects[curobj].SubObjects[i][j + 1] = coord[1]; } } } else if (l < miflines.Length && miflines[l].StartsWith("Pline ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Line; layer.Objects[curobj].SubObjects = new double[1][]; layer.Objects[curobj].SubObjects[0] = new double[int.Parse(miflines[l++].Substring(6)) * 2]; for (int i = 0; i < layer.Objects[curobj].SubObjects[0].Length; i += 2) { double[] coord = ParseCoordinatePair(miflines[l++]); layer.Objects[curobj].SubObjects[0][i] = coord[0]; layer.Objects[curobj].SubObjects[0][i + 1] = coord[1]; } } else if (l < miflines.Length && miflines[l].StartsWith("Text")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Text; string text = miflines[++l]; //what to do with the text? Do we wanna keep it? Nah, text sucks l++; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Trim().Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[0], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture) }; l++; //this be the font line } else if (l < miflines.Length && miflines[l].StartsWith("Line ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Line; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Ellipse ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Ellipse; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Roundrect ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Roundrect; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[5], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Arc ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Arc; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[5], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[6], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Rect ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Region; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); double[] tmppoints = new double[8]; tmppoints[0] = double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture); tmppoints[1] = double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture); tmppoints[4] = double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture); tmppoints[5] = double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture); tmppoints[2] = tmppoints[4]; tmppoints[3] = tmppoints[1]; tmppoints[6] = tmppoints[0]; tmppoints[7] = tmppoints[5]; layer.Objects[curobj].SubObjects[0] = tmppoints; } else if (l < miflines.Length && miflines[l].StartsWith("none")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Unknown; l++; } else { throw new Exception("MIF-objekt-type (" + miflines[l] + ") er endnu ikke understøttet"); } //parse style if (l < miflines.Length && miflines[l].StartsWith(" Pen ")) { layer.Objects[curobj].BorderColor = Color.FromArgb(255, Color.FromArgb(ParseParenteseBlock(miflines[l].Substring(8))[2])); l++; } if (l < miflines.Length && miflines[l].StartsWith(" Brush ")) { layer.Objects[curobj].FillColor = Color.FromArgb(255, Color.FromArgb(ParseParenteseBlock(miflines[l].Substring(10))[2])); l++; } curobj++; } return(layer); }
/// <summary> /// Will save the given layer to the given path /// </summary> /// <param name="layer"></param> /// <param name="path"></param> public static void SaveFile(MapInfoLayer layer, string path) { //create MID string midpath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path) + ".mid"); System.IO.StreamWriter sw = new System.IO.StreamWriter(midpath, false, System.Text.Encoding.Default); foreach (MapInfoObject obj in layer) { string line = ""; for (int i = 0; i < layer.ColumnNames.Length; i++) { //write if (layer.ColumnTypes[i].StartsWith("Char(")) { line += "\"" + obj.Data[i] + "\","; } else { line += obj.Data[i] + ","; } } line = line.Substring(0, line.Length - 1); sw.WriteLine(line); } sw.Close(); sw.Dispose(); //create MIF string mifpath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path) + ".mif"); try { sw = new System.IO.StreamWriter(mifpath, false, System.Text.Encoding.Default); } catch (Exception ex) { throw new Exception("Kunne ikke få adgang til filen \"" + mifpath + "\"\nError: " + ex.Message); } try { sw.WriteLine("Version 600"); sw.WriteLine("Charset \"WindowsLatin1\""); sw.WriteLine("Delimiter \", \""); if (!String.IsNullOrEmpty(layer.Index)) { sw.WriteLine("Index " + layer.Index); } sw.WriteLine(layer.CoordinateSystem); sw.WriteLine("Columns " + (layer.ColumnNames.Length).ToString()); for (int i = 0; i < layer.ColumnNames.Length; i++) { sw.WriteLine(" " + layer.ColumnNames[i] + " " + layer.ColumnTypes[i]); } sw.WriteLine("Data"); sw.WriteLine(""); } catch (Exception ex) { throw new Exception("Kunne ikke skrive header til filen \"" + mifpath + "\"\nError: " + ex.Message); } try { foreach (MapInfo.MapInfoObject obj in layer) { switch (obj.ObjectType) { case MapInfoObjectTypes.Line: if (obj.SubObjects.Length != 1) { throw new Exception("Linie-samlinger er ikke understøttet"); } sw.WriteLine("Pline " + (obj.SubObjects[0].Length / 2).ToString()); for (int j = 0; j < obj.SubObjects[0].Length; j += 2) { sw.WriteLine(obj.SubObjects[0][j].ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + obj.SubObjects[0][j + 1].ToString(System.Globalization.CultureInfo.InvariantCulture)); } sw.WriteLine(" Pen (1,2," + ((obj.BorderColor.R * 65536) + (obj.BorderColor.G * 256) + obj.BorderColor.B) + ") "); break; case MapInfoObjectTypes.Point: if (obj.SubObjects.Length != 1) { throw new Exception("Punkt-samlinger er ikke understøttet"); } sw.WriteLine("Point " + obj.SubObjects[0][0].ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + obj.SubObjects[0][1].ToString(System.Globalization.CultureInfo.InvariantCulture)); sw.WriteLine(" Symbol (35," + ((obj.BorderColor.R * 65536) + (obj.BorderColor.G * 256) + obj.BorderColor.B) + ",12) "); break; case MapInfoObjectTypes.Region: sw.WriteLine("Region " + obj.SubObjects.Length); foreach (double[] objcoord in obj.SubObjects) { if (objcoord.Length % 2 != 0) { throw new Exception("Antallet af koordinater er ikke deleligt med 2 (" + objcoord.Length + ")"); } sw.WriteLine(" " + (objcoord.Length / 2).ToString()); for (int j = 0; j < objcoord.Length; j += 2) { sw.WriteLine(objcoord[j].ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + objcoord[j + 1].ToString(System.Globalization.CultureInfo.InvariantCulture)); } } sw.WriteLine(" Pen (1,2," + ((obj.BorderColor.R * 65536) + (obj.BorderColor.G * 256) + obj.BorderColor.B) + ") "); if (!obj.FillColor.IsEmpty) { sw.WriteLine(" Brush (1," + ((obj.FillColor.R * 65536) + (obj.FillColor.G * 256) + obj.FillColor.B) + ",0)"); } else { sw.WriteLine(" Brush (1,0,0)"); } break; default: sw.WriteLine("None"); break; } } } catch (Exception ex) { throw new Exception("Kunne ikke skrive objekter til filen \"" + mifpath + "\"\nError: " + ex.Message); } finally { if (sw != null) { sw.Close(); sw.Dispose(); sw = null; } } }
public MapInfoObject(MapInfoLayer layer) { ParentLayer = layer; }
/// <summary> /// This will parse the given mif and its counterpart /// </summary> /// <param name="path"></param> /// <returns></returns> public static MapInfoLayer LoadFile(string path) { MapInfoLayer layer = new MapInfoLayer(Path.GetFileNameWithoutExtension(path)); //read MID string midfilepath = Path.GetDirectoryName(path) + "\\" + Path.GetFileNameWithoutExtension(path) + ".mid"; if (!File.Exists(midfilepath)) throw new Exception("Kunne ikke finde MID-fil: \"" + midfilepath + "\""); StreamReader sr = new StreamReader(midfilepath, Encoding.Default); string[] midlines = sr.ReadToEnd().Trim().Split('\n'); sr.Close(); layer.Objects = new MapInfoObject[midlines.Length]; for (int i = 0; i < midlines.Length; i++) { layer.Objects[i] = new MapInfoObject(layer); layer.Objects[i].LoadFromMIDLine(midlines[i]); } //read MIF if (!File.Exists(path)) throw new Exception("Kunne ikke finde MIF-fil"); sr = new StreamReader(path, Encoding.Default); string[] miflines = sr.ReadToEnd().Trim().Split('\n'); sr.Close(); //coordsys int l = 0; while (!miflines[l].StartsWith("CoordSys")) l++; layer.CoordinateSystem = miflines[l]; //Index if (miflines.Length >= 4 && miflines[3].StartsWith("Index ")) layer.Index = miflines[3].Substring(6).Trim(); //columns l = 0; while (!miflines[l].StartsWith("Columns")) l++; layer.ColumnNames = new string[int.Parse(miflines[l].Substring(8))]; layer.ColumnTypes = new string[int.Parse(miflines[l].Substring(8))]; l++; int c = 0; string[] e; while (!miflines[l].StartsWith("Data")) { e = miflines[l].Trim().Split(' '); layer.ColumnNames[c] = e[0]; layer.ColumnTypes[c] = e[1]; if (layer.ColumnTypes[c].StartsWith("Decimal(")) layer.ColumnTypes[c] += " " + e[2]; c++; l++; } l++; //objects int curobj = 0; while (l < miflines.Length) { //find object start while (l < miflines.Length && (miflines[l] == "" || miflines[l][0] == '\r' || miflines[l][0] == ' ')) l++; if (l == miflines.Length) break; if (l < miflines.Length && miflines[l].StartsWith("Region ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Region; layer.Objects[curobj].SubObjects = new double[int.Parse(miflines[l++].Substring(7))][]; for (int re = 0; re < layer.Objects[curobj].SubObjects.Length; re++) { layer.Objects[curobj].SubObjects[re] = new double[int.Parse(miflines[l++].Trim()) * 2]; for (int i = 0; i < layer.Objects[curobj].SubObjects[re].Length; i += 2) { double[] coord = ParseCoordinatePair(miflines[l++]); layer.Objects[curobj].SubObjects[re][i] = coord[0]; layer.Objects[curobj].SubObjects[re][i + 1] = coord[1]; } } } else if (l < miflines.Length && miflines[l].StartsWith("Point ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Point; layer.Objects[curobj].SubObjects = new double[1][]; layer.Objects[curobj].SubObjects[0] = ParseCoordinatePair(miflines[l++].Substring(6)); } else if (l < miflines.Length && miflines[l].StartsWith("Pline Multiple")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Line; layer.Objects[curobj].SubObjects = new double[int.Parse(miflines[l++].Substring(15))][]; for (int i = 0; i < layer.Objects[curobj].SubObjects.Length; i++ ) { layer.Objects[curobj].SubObjects[i] = new double[int.Parse(miflines[l++].Trim()) * 2]; for (int j = 0; j < layer.Objects[curobj].SubObjects[i].Length; j += 2) { double[] coord = ParseCoordinatePair(miflines[l++]); layer.Objects[curobj].SubObjects[i][j] = coord[0]; layer.Objects[curobj].SubObjects[i][j + 1] = coord[1]; } } } else if (l < miflines.Length && miflines[l].StartsWith("Pline ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Line; layer.Objects[curobj].SubObjects = new double[1][]; layer.Objects[curobj].SubObjects[0] = new double[int.Parse(miflines[l++].Substring(6)) * 2]; for (int i = 0; i < layer.Objects[curobj].SubObjects[0].Length; i += 2) { double[] coord = ParseCoordinatePair(miflines[l++]); layer.Objects[curobj].SubObjects[0][i] = coord[0]; layer.Objects[curobj].SubObjects[0][i + 1] = coord[1]; } } else if (l < miflines.Length && miflines[l].StartsWith("Text")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Text; string text = miflines[++l]; //what to do with the text? Do we wanna keep it? Nah, text sucks l++; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Trim().Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[0], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture) }; l++; //this be the font line } else if (l < miflines.Length && miflines[l].StartsWith("Line ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Line; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Ellipse ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Ellipse; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Roundrect ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Roundrect; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[5], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Arc ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Arc; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); layer.Objects[curobj].SubObjects[0] = new double[] { double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[5], System.Globalization.CultureInfo.InvariantCulture), double.Parse(tmp[6], System.Globalization.CultureInfo.InvariantCulture) }; } else if (l < miflines.Length && miflines[l].StartsWith("Rect ")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Region; layer.Objects[curobj].SubObjects = new double[1][]; string[] tmp = miflines[l++].Split(' '); double[] tmppoints = new double[8]; tmppoints[0] = double.Parse(tmp[1], System.Globalization.CultureInfo.InvariantCulture); tmppoints[1] = double.Parse(tmp[2], System.Globalization.CultureInfo.InvariantCulture); tmppoints[4] = double.Parse(tmp[3], System.Globalization.CultureInfo.InvariantCulture); tmppoints[5] = double.Parse(tmp[4], System.Globalization.CultureInfo.InvariantCulture); tmppoints[2] = tmppoints[4]; tmppoints[3] = tmppoints[1]; tmppoints[6] = tmppoints[0]; tmppoints[7] = tmppoints[5]; layer.Objects[curobj].SubObjects[0] = tmppoints; } else if (l < miflines.Length && miflines[l].StartsWith("none")) { layer.Objects[curobj].ObjectType = MapInfoObjectTypes.Unknown; l++; } else throw new Exception("MIF-objekt-type (" + miflines[l] + ") er endnu ikke understøttet"); //parse style if (l < miflines.Length && miflines[l].StartsWith(" Pen ")) { layer.Objects[curobj].BorderColor = Color.FromArgb(255, Color.FromArgb(ParseParenteseBlock(miflines[l].Substring(8))[2])); l++; } if (l < miflines.Length && miflines[l].StartsWith(" Brush ")) { layer.Objects[curobj].FillColor = Color.FromArgb(255, Color.FromArgb(ParseParenteseBlock(miflines[l].Substring(10))[2])); l++; } curobj++; } return layer; }
/// <summary> /// Will save the given layer to the given path /// </summary> /// <param name="layer"></param> /// <param name="path"></param> public static void SaveFile(MapInfoLayer layer, string path) { //create MID string midpath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path) + ".mid"); System.IO.StreamWriter sw = new System.IO.StreamWriter(midpath, false, System.Text.Encoding.Default); foreach (MapInfoObject obj in layer) { string line = ""; for (int i = 0; i < layer.ColumnNames.Length; i++) { //write if (layer.ColumnTypes[i].StartsWith("Char(")) line += "\"" + obj.Data[i] + "\","; else line += obj.Data[i] + ","; } line = line.Substring(0, line.Length - 1); sw.WriteLine(line); } sw.Close(); sw.Dispose(); //create MIF string mifpath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path) + ".mif"); try { sw = new System.IO.StreamWriter(mifpath, false, System.Text.Encoding.Default); } catch (Exception ex) { throw new Exception("Kunne ikke få adgang til filen \"" + mifpath + "\"\nError: " + ex.Message); } try { sw.WriteLine("Version 600"); sw.WriteLine("Charset \"WindowsLatin1\""); sw.WriteLine("Delimiter \", \""); if(!String.IsNullOrEmpty(layer.Index))sw.WriteLine("Index " + layer.Index); sw.WriteLine(layer.CoordinateSystem); sw.WriteLine("Columns " + (layer.ColumnNames.Length).ToString()); for (int i = 0; i < layer.ColumnNames.Length; i++) { sw.WriteLine(" " + layer.ColumnNames[i] + " " + layer.ColumnTypes[i]); } sw.WriteLine("Data"); sw.WriteLine(""); } catch (Exception ex) { throw new Exception("Kunne ikke skrive header til filen \"" + mifpath + "\"\nError: " + ex.Message); } try { foreach (MapInfo.MapInfoObject obj in layer) { switch (obj.ObjectType) { case MapInfoObjectTypes.Line: if (obj.SubObjects.Length != 1) throw new Exception("Linie-samlinger er ikke understøttet"); sw.WriteLine("Pline " + (obj.SubObjects[0].Length / 2).ToString()); for (int j = 0; j < obj.SubObjects[0].Length; j += 2) sw.WriteLine(obj.SubObjects[0][j].ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + obj.SubObjects[0][j + 1].ToString(System.Globalization.CultureInfo.InvariantCulture)); sw.WriteLine(" Pen (1,2," + ((obj.BorderColor.R * 65536) + (obj.BorderColor.G * 256) + obj.BorderColor.B) + ") "); break; case MapInfoObjectTypes.Point: if (obj.SubObjects.Length != 1) throw new Exception("Punkt-samlinger er ikke understøttet"); sw.WriteLine("Point " + obj.SubObjects[0][0].ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + obj.SubObjects[0][1].ToString(System.Globalization.CultureInfo.InvariantCulture)); sw.WriteLine(" Symbol (35," + ((obj.BorderColor.R * 65536) + (obj.BorderColor.G * 256) + obj.BorderColor.B) + ",12) "); break; case MapInfoObjectTypes.Region: sw.WriteLine("Region " + obj.SubObjects.Length); foreach (double[] objcoord in obj.SubObjects) { if (objcoord.Length % 2 != 0) throw new Exception("Antallet af koordinater er ikke deleligt med 2 (" + objcoord.Length + ")"); sw.WriteLine(" " + (objcoord.Length / 2).ToString()); for (int j = 0; j < objcoord.Length; j += 2) sw.WriteLine(objcoord[j].ToString(System.Globalization.CultureInfo.InvariantCulture) + " " + objcoord[j + 1].ToString(System.Globalization.CultureInfo.InvariantCulture)); } sw.WriteLine(" Pen (1,2," + ((obj.BorderColor.R * 65536) + (obj.BorderColor.G * 256) + obj.BorderColor.B) + ") "); if (!obj.FillColor.IsEmpty) sw.WriteLine(" Brush (1," + ((obj.FillColor.R * 65536) + (obj.FillColor.G * 256) + obj.FillColor.B) + ",0)"); else sw.WriteLine(" Brush (1,0,0)"); break; default: sw.WriteLine("None"); break; } } } catch (Exception ex) { throw new Exception("Kunne ikke skrive objekter til filen \"" + mifpath + "\"\nError: " + ex.Message); } finally { if (sw != null) { sw.Close(); sw.Dispose(); sw = null; } } }