Skip to content

Commit

Permalink
Minor improvements for location records
Browse files Browse the repository at this point in the history
  • Loading branch information
Serg-Norseman committed Mar 14, 2024
1 parent e2394c8 commit 34514f7
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 32 deletions.
58 changes: 47 additions & 11 deletions projects/GKCore/GDModel/GDMLocationRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Collections.Generic;
using GDModel.Providers.GEDCOM;
using GKCore.Calendar;
using GKCore.Options;
using GKCore.Types;

namespace GDModel
Expand Down Expand Up @@ -99,6 +100,35 @@ public override void Assign(GDMTag source)
AssignList(otherLoc.fTopLevels, TopLevels);
}

/// <summary>
/// The MoveTo() merges records and their references, but does not change the text in the target.
/// </summary>
/// <param name="targetRecord"></param>
public override void MoveTo(GDMRecord targetRecord)
{
GDMLocationRecord targetLoc = (targetRecord as GDMLocationRecord);
if (targetLoc == null)
throw new ArgumentException(@"Argument is null or wrong type", "targetRecord");

base.MoveTo(targetRecord);

if (targetLoc.Map.IsEmpty() && !fMap.IsEmpty()) {
targetLoc.Map.Assign(fMap);
}

while (fNames.Count > 0) {
GDMLocationName obj = fNames.Extract(0);
targetLoc.Names.Add(obj);
}
SortNames();

while (fTopLevels.Count > 0) {
GDMLocationLink obj = fTopLevels.Extract(0);
targetLoc.TopLevels.Add(obj);
}
SortTopLevels();
}

public override void Clear()
{
base.Clear();
Expand Down Expand Up @@ -133,16 +163,20 @@ public override void ReplaceXRefs(GDMXRefReplacer map)

public GDMList<GDMLocationName> GetFullNames(GDMTree tree)
{
var result = new GDMList<GDMLocationName>();
GDMList<GDMLocationName> result;

if (fTopLevels.Count > 0) {
var buffer = new List<GDMLocationName>();
if (fTopLevels.Count == 0) {
result = fNames;
} else {
result = new GDMList<GDMLocationName>();

var buffer = new List<GDMLocationName>();
for (int j = 0; j < fTopLevels.Count; j++) {
var topLevel = fTopLevels[j];
var topLoc = tree.GetPtrValue<GDMLocationRecord>(topLevel);
if (topLoc == null) continue;

bool wasJoin = false;
var topNames = topLoc.GetFullNames(tree);
for (int i = 0; i < topNames.Count; i++) {
var topName = topNames[i];
Expand All @@ -153,10 +187,19 @@ public GDMList<GDMLocationName> GetFullNames(GDMTree tree)
newLocName.StringValue = topName.StringValue;
newLocName.Date.ParseString(interDate.StringValue);
buffer.Add(newLocName);
wasJoin = true;
}
}

if (!wasJoin && topNames.Count > 0) {
var newLocName = new GDMLocationName();
newLocName.StringValue = topNames[topNames.Count - 1].StringValue;
newLocName.Date.ParseString(topLevel.Date.StringValue);
buffer.Add(newLocName);
}
}

bool reverseOrder = GlobalOptions.Instance.ReversePlaceEntitiesOrder;
for (int j = 0; j < buffer.Count; j++) {
var topLocName = buffer[j];
var topName = topLocName.StringValue;
Expand All @@ -167,7 +210,7 @@ public GDMList<GDMLocationName> GetFullNames(GDMTree tree)

var interDate = GDMCustomDate.GetIntersection(topDate, locName.Date.Value);
if (!interDate.IsEmpty()) {
string newName = locName.StringValue + ", " + topName;
string newName = (!reverseOrder) ? topName + ", " + locName.StringValue : locName.StringValue + ", " + topName;

var newLocName = new GDMLocationName();
newLocName.StringValue = newName;
Expand All @@ -176,13 +219,6 @@ public GDMList<GDMLocationName> GetFullNames(GDMTree tree)
}
}
}
} else {
for (int i = 0; i < fNames.Count; i++) {
var locName = fNames[i];
if (locName.Date.IsEmpty()) continue;

result.Add(locName);
}
}

return result;
Expand Down
43 changes: 32 additions & 11 deletions projects/GKCore/GDModel/GDMTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,17 +697,38 @@ public bool DeleteLocationRecord(GDMLocationRecord locRec)

int num = fRecords.Count;
for (int i = 0; i < num; i++) {
var evsRec = fRecords[i] as GDMRecordWithEvents;
if (evsRec == null || !evsRec.HasEvents) continue;

for (int j = evsRec.Events.Count - 1; j >= 0; j--) {
var evt = evsRec.Events[j];
if (!evt.HasPlace) continue;

GDMPointer evLocation = evt.Place.Location;
if (evLocation.XRef == locRec.XRef) {
evLocation.XRef = string.Empty;
}
var rec = fRecords[i];

switch (rec.RecordType) {
case GDMRecordType.rtIndividual:
case GDMRecordType.rtFamily: {
var evsRec = rec as GDMRecordWithEvents;
if (evsRec == null || !evsRec.HasEvents) continue;

for (int j = evsRec.Events.Count - 1; j >= 0; j--) {
var evt = evsRec.Events[j];
if (!evt.HasPlace) continue;

GDMPointer evLocation = evt.Place.Location;
if (evLocation.XRef == locRec.XRef) {
evLocation.XRef = string.Empty;
}
}
}
break;

case GDMRecordType.rtLocation: {
var lRec = rec as GDMLocationRecord;
if (lRec == null) continue;

for (int j = lRec.TopLevels.Count - 1; j >= 0; j--) {
var topLev = lRec.TopLevels[j];
if (topLev.XRef == locRec.XRef) {
lRec.TopLevels.DeleteAt(j);
}
}
}
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions projects/GKCore/GKCore/Charts/TreeChartModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public TreeChartModel()
fPreparedFamilies = new HashSet<string>();
fPreparedIndividuals = new HashSet<string>();
fScale = 1.0f;
fPicScale = 1.0f;

fBranchDistance = DEF_BRANCH_DISTANCE;
fLevelDistance = DEF_LEVEL_DISTANCE;
Expand Down
26 changes: 24 additions & 2 deletions projects/GKCore/GKCore/GKUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3025,11 +3025,33 @@ public static void ShowLocationInfo(IBaseContext baseContext, GDMLocationRecord
summary.Add("");
}

GDMTree tree = baseContext.Tree;

if (locRec.TopLevels.Count > 0) {
summary.Add("");
summary.Add(LangMan.LS(LSID.TopLevelLinks) + ":");

for (int i = 0; i < locRec.TopLevels.Count; i++) {
var topLev = locRec.TopLevels[i];
var topLoc = tree.GetPtrValue<GDMLocationRecord>(topLev);

string st = HyperLink(topLev.XRef, topLoc.GetNameByDate(topLev.Date.Value));
if (!string.IsNullOrEmpty(st)) {
summary.Add(" " + st);
}

st = topLev.Date.GetDisplayStringExt(glob.DefDateFormat, glob.ShowDatesSign, glob.ShowDatesCalendar);
if (!string.IsNullOrEmpty(st)) {
summary.Add(" " + st);
}

summary.Add("");
}
}

summary.Add(LangMan.LS(LSID.Latitude) + ": " + locRec.Map.Lati);
summary.Add(LangMan.LS(LSID.Longitude) + ": " + locRec.Map.Long);

GDMTree tree = baseContext.Tree;

var fullNames = locRec.GetFullNames(tree);
if (fullNames.Count > 0) {
//linkList.Sort();
Expand Down
5 changes: 5 additions & 0 deletions projects/GKTests/GDModel/GDMLocationRecordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Linq;
using GDModel.Providers.GEDCOM;
using GKCore;
using GKCore.Options;
using GKCore.Types;
using GKTests;
using NUnit.Framework;
Expand Down Expand Up @@ -190,6 +191,8 @@ public void Test_NamesByDate()
[Test]
public void Test_Hierarchy()
{
GlobalOptions.Instance.ReversePlaceEntitiesOrder = true;

var tree = new GDMTree();

// J2G Rus transfer: 26 JAN 1918 (Julian), 1 FEB -> 14 FEB 1918
Expand Down Expand Up @@ -381,6 +384,8 @@ public void Test_Hierarchy()

//var gedcomProvider = new GEDCOMProvider(tree);
//gedcomProvider.SaveToStreamExt(new FileStream("d:\\Russia.ged", FileMode.CreateNew), GEDCOMCharacterSet.csUTF8);

GlobalOptions.Instance.ReversePlaceEntitiesOrder = false;
}

[Test]
Expand Down
54 changes: 46 additions & 8 deletions samples/RussiaLocations.ged
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
0 HEAD
1 SOUR GEDKeeper
2 VERS 45
2 VERS 46
1 DEST GEDKeeper
1 CHAR UTF-8
1 GEDC
2 VERS 5.5.1
2 FORM LINEAGE-LINKED
1 FILE RussiaLocations.ged
2 _UID 1FED9A011B5AFA4AAED1C2C28CB076BFD434
2 _REV 24
1 DATE 06 FEB 2024
2 TIME 20:58:12.9
2 _REV 30
1 DATE 14 MAR 2024
2 TIME 14:29:17.1
0 @L1@ _LOC
1 _UID 259212A7C781634B851B7F4C19FE40CFF7BD
1 CHAN
Expand Down Expand Up @@ -264,9 +264,18 @@
0 @L16@ _LOC
1 _UID DF70190CB963D04D8174DE1DCB424307F4B4
1 CHAN
2 DATE 06 FEB 2024
3 TIME 00:18:37.1
2 DATE 14 MAR 2024
3 TIME 14:28:46.4
1 NAME Архангелогородская губерния
2 DATE FROM 18 DEC 1708 TO 24 JAN 1780
1 NAME Архангельская область
2 DATE FROM 25 JAN 1780 TO FEB 1784
1 NAME Архангельское наместничество
2 DATE FROM 26 MAR 1784 TO 11 DEC 1796
1 NAME Архангельская губерния
2 DATE FROM 12 DEC 1796 TO 14 JAN 1929
1 _LOC @L1@
2 DATE FROM 1708
0 @L18@ _LOC
1 _UID E86CA4E9BA504B409993827382B325D0C14A
1 CHAN
Expand Down Expand Up @@ -333,9 +342,18 @@
0 @L27@ _LOC
1 _UID 48C2974CB8E17E488A4573677F35AD398FD1
1 CHAN
2 DATE 06 FEB 2024
3 TIME 00:21:07.2
2 DATE 14 MAR 2024
3 TIME 14:28:18
1 NAME Вологодская провинция
2 DATE FROM 1719 TO 24 JAN 1780
1 NAME Вологодское наместничество
2 DATE FROM 25 JAN 1780 TO 11 DEC 1796
1 NAME Вологодская губерния
2 DATE FROM 12 DEC 1796 TO 14 JAN 1929
1 _LOC @L16@
2 DATE FROM 1719 TO 1775
1 _LOC @L1@
2 DATE FROM 1776
0 @L28@ _LOC
1 _UID 74E55FEBED50C64ABE3840D14D25BFD5FDB1
1 CHAN
Expand Down Expand Up @@ -783,4 +801,24 @@
2 DATE FROM 1775
1 _LOC @L1@
2 DATE FROM 05 SEP 1809 TO 1918
0 @L94@ _LOC
1 _UID 53F45B9DC0CA5C44A527B04DA686F7ED42F8
1 CHAN
2 DATE 14 MAR 2024
3 TIME 13:55:50.6
1 NAME Тотемский уезд
2 DATE FROM 1708 TO 1928
1 NAME Тотемский район
2 DATE FROM 1929
1 _LOC @L16@
2 DATE FROM 1708 TO 1718
1 _LOC @L27@
2 DATE FROM 1719 TO SEP 1929
0 @L95@ _LOC
1 _UID FA8FC00526A4304D8CF40904B10D6CE7332B
1 CHAN
2 DATE 14 MAR 2024
3 TIME 13:53:38.5
1 NAME Северный край
2 DATE FROM 01 OCT 1929 TO 05 DEC 1936
0 TRLR

0 comments on commit 34514f7

Please sign in to comment.