PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/4.0/Source/FamilyShowLib/PlacesExport.cs

#
C# | 587 lines | 436 code | 129 blank | 22 comment | 110 complexity | cd7e3a8a19a92fff573ec6725c6a1ef4 MD5 | raw file
  1. /*
  2. * Exports time encoded place information to a kml file.
  3. *
  4. * Three sections are exported: Events and People
  5. * 1. Events are births, marriages and deaths and the year that the event occurs is included.
  6. * 2. People are exported with a timespan from date of birth to date of death.
  7. * 3. All places with no time information.
  8. *
  9. * The format is based on the open kml standard for map information.
  10. * The recommended software for reading the file is Google Earth as
  11. * this program will search for coordinate information of places
  12. * specified in the file. Other similar services such as Bing maps
  13. * require coordinates to be specifed in the file. Google Earth can also
  14. * read the time information and allows the user to specify a time period to
  15. * display e.g. 1900-1950.
  16. *
  17. * Places export supports restrictions and quick filter for living people.
  18. */
  19. //burials
  20. //cremations
  21. using System.IO;
  22. namespace Microsoft.FamilyShowLib
  23. {
  24. public class PlacesExport
  25. {
  26. #region export methods
  27. public string[] ExportPlaces(PeopleCollection peopleCollection, string fileName, bool hideliving, bool times, bool lifespans, bool places,bool burials, bool deaths, bool cremations, bool births, bool marriages)
  28. {
  29. string PlacesFileName = Path.GetFileNameWithoutExtension(fileName);
  30. TextWriter tw = new StreamWriter(fileName);
  31. #region styles
  32. // Write text necessary for kml file.
  33. // Uses Google Earths's male and female icons.
  34. tw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  35. "<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n" +
  36. "<Document>\n" +
  37. "<name>" + PlacesFileName + "</name>\n" +
  38. "<Style id=\"sn_man2\">\n" +
  39. "<IconStyle>\n" +
  40. "<scale>0.9</scale>\n" +
  41. "<Icon>\n" +
  42. "<href>http://maps.google.com/mapfiles/kml/shapes/man.png</href>\n" +
  43. "</Icon>\n" +
  44. "</IconStyle>\n" +
  45. "<LabelStyle>\n" +
  46. "<scale>0.9</scale>\n" +
  47. "</LabelStyle>\n" +
  48. "</Style>\n" +
  49. "<Style id=\"sh_man0\">\n" +
  50. "<IconStyle>\n" +
  51. " <scale>0.9</scale>\n" +
  52. "<Icon>\n" +
  53. " <href>http://maps.google.com/mapfiles/kml/shapes/man.png</href>\n" +
  54. "</Icon>\n" +
  55. "</IconStyle>\n" +
  56. "<LabelStyle>\n" +
  57. " <scale>0.9</scale>\n" +
  58. "</LabelStyle>\n" +
  59. "</Style>\n" +
  60. "<StyleMap id=\"msn_man\">\n" +
  61. "<Pair>\n" +
  62. " <key>normal</key>\n" +
  63. " <styleUrl>#sn_man2</styleUrl>\n" +
  64. "</Pair>\n" +
  65. "<Pair>\n" +
  66. " <key>highlight</key>\n" +
  67. " <styleUrl>#sh_man0</styleUrl>\n" +
  68. "</Pair>\n" +
  69. "</StyleMap>\n" +
  70. "<Style id=\"sn_woman1\">\n" +
  71. " <IconStyle>\n" +
  72. " <scale>0.9</scale>\n" +
  73. " <Icon>\n" +
  74. " <href>http://maps.google.com/mapfiles/kml/shapes/woman.png</href>\n" +
  75. " </Icon>\n" +
  76. " </IconStyle>\n" +
  77. " <LabelStyle>\n" +
  78. " <scale>0.9</scale>\n" +
  79. " </LabelStyle>\n" +
  80. "</Style>\n" +
  81. "<Style id=\"sh_woman0\">\n" +
  82. " <IconStyle>\n" +
  83. " <scale>0.9</scale>\n" +
  84. " <Icon>\n" +
  85. " <href>http://maps.google.com/mapfiles/kml/shapes/woman.png</href>\n" +
  86. " </Icon>\n" +
  87. " </IconStyle>\n" +
  88. " <LabelStyle>\n" +
  89. " <scale>0.9</scale>\n" +
  90. " </LabelStyle>\n" +
  91. "</Style>\n" +
  92. "<StyleMap id=\"msn_woman\">\n" +
  93. "<Pair>\n" +
  94. " <key>normal</key>\n" +
  95. " <styleUrl>#sn_woman1</styleUrl>\n" +
  96. "</Pair>\n" +
  97. "<Pair>\n" +
  98. " <key>highlight</key>\n" +
  99. " <styleUrl>#sh_woman0</styleUrl>\n" +
  100. "</Pair>\n" +
  101. "</StyleMap>");
  102. #endregion
  103. int i = 0; // Counter for all the places exported.
  104. if (places)
  105. {
  106. #region places with no time information
  107. tw.WriteLine("<Folder>\n" +
  108. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.People + "</name>\n" +
  109. "<open>0</open>\n" +
  110. "<Folder>\n" +
  111. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Births + "</name>\n" +
  112. "<open>0</open>");
  113. if (births)
  114. {
  115. foreach (Person p in peopleCollection)
  116. {
  117. if (!(hideliving && p.IsLiving))
  118. {
  119. if (!string.IsNullOrEmpty(p.BirthPlace) && p.Restriction != Restriction.Private)
  120. {
  121. string name = string.Empty;
  122. tw.WriteLine("<Placemark>\n" +
  123. " <name>" + p.FullName + "</name>\n" +
  124. " <address>" + p.BirthPlace + "</address>\n" +
  125. " <description>" + p.BirthPlace + "</description>");
  126. if (p.Gender == Gender.Male)
  127. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  128. else
  129. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  130. i++;
  131. }
  132. }
  133. }
  134. tw.WriteLine("</Folder>");
  135. }
  136. if (deaths)
  137. {
  138. tw.WriteLine("<Folder>\n<name>" + Microsoft.FamilyShowLib.Properties.Resources.Deaths + "</name>\n" +
  139. "<open>0</open>");
  140. foreach (Person p in peopleCollection)
  141. {
  142. if (!(hideliving && p.IsLiving))
  143. {
  144. if (!string.IsNullOrEmpty(p.DeathPlace) && p.Restriction != Restriction.Private)
  145. {
  146. tw.WriteLine("<Placemark>\n" +
  147. "<name>" + p.FullName + "</name>\n" +
  148. "<address>" + p.DeathPlace + "</address>\n" +
  149. "<description>" + p.DeathPlace + "</description>");
  150. if (p.Gender == Gender.Male)
  151. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  152. else
  153. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  154. i++;
  155. }
  156. }
  157. }
  158. tw.WriteLine("</Folder>");
  159. }
  160. if (burials)
  161. {
  162. tw.WriteLine("<Folder>\n" +
  163. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Burials + "</name>\n" +
  164. "<open>0</open>");
  165. foreach (Person p in peopleCollection)
  166. {
  167. if (!(hideliving && p.IsLiving))
  168. {
  169. if (!string.IsNullOrEmpty(p.BurialPlace) && p.Restriction != Restriction.Private)
  170. {
  171. tw.WriteLine("<Placemark>\n" +
  172. "<name>" + p.FullName + "</name>\n" +
  173. "<address>" + p.BurialPlace + "</address>\n" +
  174. "<description>" + p.BurialPlace + "</description>");
  175. if (p.Gender == Gender.Male)
  176. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  177. else
  178. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  179. i++;
  180. }
  181. }
  182. }
  183. tw.WriteLine("</Folder>");
  184. }
  185. if (cremations)
  186. {
  187. tw.WriteLine("<Folder>\n" +
  188. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Cremations + "</name>\n" +
  189. "<open>0</open>");
  190. foreach (Person p in peopleCollection)
  191. {
  192. if (!(hideliving && p.IsLiving))
  193. {
  194. if (!string.IsNullOrEmpty(p.CremationPlace) && p.Restriction != Restriction.Private)
  195. {
  196. tw.WriteLine("<Placemark>\n" +
  197. "<name>" + p.FullName + "</name>\n" +
  198. "<address>" + p.CremationPlace + "</address>\n" +
  199. "<description>" + p.CremationPlace + "</description>");
  200. if (p.Gender == Gender.Male)
  201. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  202. else
  203. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  204. i++;
  205. }
  206. }
  207. }
  208. tw.WriteLine("</Folder>");
  209. }
  210. if (marriages)
  211. {
  212. tw.WriteLine("<Folder>\n" +
  213. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Marriages + "</name>\n" +
  214. "<open>0</open>");
  215. foreach (Person p in peopleCollection)
  216. {
  217. if (!(hideliving && p.IsLiving))
  218. {
  219. if (p.Restriction != Restriction.Private)
  220. {
  221. foreach (Relationship rel in p.Relationships)
  222. {
  223. if (rel.RelationshipType == RelationshipType.Spouse)
  224. {
  225. SpouseRelationship spouseRel = ((SpouseRelationship)rel);
  226. if (!string.IsNullOrEmpty(spouseRel.MarriagePlace))
  227. {
  228. tw.WriteLine("<Placemark>\n" +
  229. "<name>" + p.FullName + "</name>\n" +
  230. "<address>" + spouseRel.MarriagePlace + "</address>\n" +
  231. "<description>" + spouseRel.MarriagePlace + "</description>");
  232. if (p.Gender == Gender.Male)
  233. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  234. else
  235. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  236. i++;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. tw.WriteLine("</Folder>");
  244. }
  245. #endregion
  246. }
  247. else if(times)
  248. {
  249. #region place with time information
  250. tw.WriteLine("<Folder>\n" +
  251. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Events + "</name>\n" +
  252. "<open>0</open>");
  253. if (births)
  254. {
  255. tw.WriteLine("<Folder>\n" +
  256. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Births + "</name>\n" +
  257. "<open>0</open>");
  258. foreach (Person p in peopleCollection)
  259. {
  260. if (!(hideliving && p.IsLiving))
  261. {
  262. if (!string.IsNullOrEmpty(p.BirthPlace) && p.Restriction != Restriction.Private)
  263. {
  264. tw.WriteLine("<Placemark>\n" +
  265. " <name>" + p.FullName + "</name>\n" +
  266. " <address>" + p.BirthPlace + "</address>\n" +
  267. " <description>" + p.BirthPlace + "</description>\n" +
  268. " <TimeStamp>\n<when>" + p.YearOfBirth + "</when>\n</TimeStamp>");
  269. if (p.Gender == Gender.Male)
  270. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  271. else
  272. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  273. i++;
  274. }
  275. }
  276. }
  277. tw.WriteLine("</Folder>");
  278. }
  279. if (deaths)
  280. {
  281. tw.WriteLine("<Folder>\n" +
  282. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Deaths + "</name>\n" +
  283. "<open>0</open>");
  284. foreach (Person p in peopleCollection)
  285. {
  286. if (!(hideliving && p.IsLiving))
  287. {
  288. if (!string.IsNullOrEmpty(p.DeathPlace) && p.Restriction != Restriction.Private)
  289. {
  290. tw.WriteLine("<Placemark>\n" +
  291. "<name>" + p.FullName + "</name>\n" +
  292. "<address>" + p.DeathPlace + "</address>\n" +
  293. "<description>" + p.DeathPlace + "</description>\n" +
  294. "<TimeStamp>\n<when>" + p.YearOfDeath + "</when>\n</TimeStamp>");
  295. if (p.Gender == Gender.Male)
  296. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  297. else
  298. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  299. i++;
  300. }
  301. }
  302. }
  303. tw.WriteLine("</Folder>");
  304. }
  305. if (deaths)
  306. {
  307. tw.WriteLine("<Folder>\n" +
  308. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Burials + "</name>\n" +
  309. "<open>0</open>");
  310. foreach (Person p in peopleCollection)
  311. {
  312. if (!(hideliving && p.IsLiving))
  313. {
  314. string year = string.Empty;
  315. if (p.BurialDate != null)
  316. year = p.BurialDate.Value.Year.ToString();
  317. if (!string.IsNullOrEmpty(p.BurialPlace) && p.Restriction != Restriction.Private && !string.IsNullOrEmpty(year))
  318. {
  319. tw.WriteLine("<Placemark>\n" +
  320. "<name>" + p.FullName + "</name>\n" +
  321. "<address>" + p.BurialPlace + "</address>\n" +
  322. "<description>" + p.BurialPlace + "</description>\n" +
  323. "<TimeStamp>\n<when>" + year + "</when>\n</TimeStamp>");
  324. if (p.Gender == Gender.Male)
  325. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  326. else
  327. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  328. i++;
  329. }
  330. }
  331. }
  332. tw.WriteLine("</Folder>");
  333. }
  334. if (cremations)
  335. {
  336. tw.WriteLine("<Folder>\n" +
  337. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Cremations + "</name>\n" +
  338. "<open>0</open>");
  339. foreach (Person p in peopleCollection)
  340. {
  341. if (!(hideliving && p.IsLiving))
  342. {
  343. string year = string.Empty;
  344. if(p.CremationDate!=null)
  345. year = p.CremationDate.Value.Year.ToString();
  346. if (!string.IsNullOrEmpty(p.CremationPlace) && p.Restriction != Restriction.Private && !string.IsNullOrEmpty(year))
  347. {
  348. tw.WriteLine("<Placemark>\n" +
  349. "<name>" + p.FullName + "</name>\n" +
  350. "<address>" + p.CremationPlace + "</address>\n" +
  351. "<description>" + p.CremationPlace + "</description>\n" +
  352. "<TimeStamp>\n<when>" + year + "</when>\n</TimeStamp>");
  353. if (p.Gender == Gender.Male)
  354. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  355. else
  356. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  357. i++;
  358. }
  359. }
  360. }
  361. tw.WriteLine("</Folder>");
  362. }
  363. if (marriages)
  364. {
  365. tw.WriteLine("<Folder>\n" +
  366. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.Marriages + "</name>\n" +
  367. "<open>0</open>");
  368. foreach (Person p in peopleCollection)
  369. {
  370. if (!(hideliving && p.IsLiving))
  371. {
  372. if (p.Restriction != Restriction.Private)
  373. {
  374. foreach (Relationship rel in p.Relationships)
  375. {
  376. if (rel.RelationshipType == RelationshipType.Spouse)
  377. {
  378. SpouseRelationship spouseRel = ((SpouseRelationship)rel);
  379. if (!string.IsNullOrEmpty(spouseRel.MarriagePlace))
  380. {
  381. string date = string.Empty;
  382. if (spouseRel.MarriageDate != null)
  383. date = spouseRel.MarriageDate.Value.Year.ToString();
  384. tw.WriteLine("<Placemark>\n" +
  385. "<name>" + p.FullName + "</name>\n" +
  386. "<address>" + spouseRel.MarriagePlace + "</address>\n" +
  387. "<description>" + spouseRel.MarriagePlace + "</description>\n" +
  388. "<TimeStamp>\n<when>" + date + "</when>\n</TimeStamp>");
  389. if (p.Gender == Gender.Male)
  390. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  391. else
  392. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  393. i++;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. }
  400. tw.WriteLine("</Folder>");
  401. }
  402. #endregion
  403. }
  404. else if (lifespans)
  405. {
  406. #region lifespans
  407. tw.WriteLine("<Folder>\n" +
  408. "<name>" + Microsoft.FamilyShowLib.Properties.Resources.People + "</name>\n" +
  409. "<open>0</open>");
  410. foreach (Person p in peopleCollection)
  411. {
  412. if (!(hideliving && p.IsLiving))
  413. {
  414. if (p.Restriction != Restriction.Private)
  415. {
  416. string place = string.Empty;
  417. if (!string.IsNullOrEmpty(p.BirthPlace) && string.IsNullOrEmpty(place))
  418. place = p.BirthPlace;
  419. if (!string.IsNullOrEmpty(p.DeathPlace) && string.IsNullOrEmpty(place))
  420. place = p.DeathPlace;
  421. tw.WriteLine("<Placemark>\n" +
  422. "<name>" + p.FullName + "</name>\n" +
  423. "<address>" + place + "</address>\n" +
  424. "<description>" + place + "</description>\n" +
  425. "<TimeSpan>");
  426. if (!string.IsNullOrEmpty(p.YearOfBirth))
  427. tw.WriteLine("<begin>" + p.YearOfBirth + "</begin>");
  428. if (!string.IsNullOrEmpty(p.YearOfBirth))
  429. tw.WriteLine("<end>" + p.YearOfDeath + "</end>");
  430. tw.WriteLine("</TimeSpan>");
  431. if (p.Gender == Gender.Male)
  432. tw.WriteLine("<styleUrl>#msn_man</styleUrl>\n</Placemark>");
  433. else
  434. tw.WriteLine("<styleUrl>#msn_woman</styleUrl>\n</Placemark>");
  435. i++;
  436. }
  437. }
  438. }
  439. #endregion
  440. }
  441. tw.WriteLine("</Folder>\n" +
  442. "</Document>\n" +
  443. "</kml>");
  444. tw.Close();
  445. string[] summary = new string[2];
  446. summary[0] = i.ToString() + " " + Microsoft.FamilyShowLib.Properties.Resources.PlacesExported;
  447. summary[1] = fileName.ToString();
  448. if (i == 0)
  449. {
  450. File.Delete(fileName);
  451. summary[0] = Microsoft.FamilyShowLib.Properties.Resources.NoPlaces;
  452. summary[1] = "No file";
  453. }
  454. return summary;
  455. }
  456. #endregion
  457. }
  458. }