From cb960c41fc8752b0d4024340b34bde1114755f43 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Sat, 1 Nov 2025 17:14:43 +0100 Subject: [PATCH 1/5] Adds Obsidian Canvas family tree generator Introduces new functionality to create interactive family tree visualizations for Obsidian Adds: - CanvasGenerator class to generate JSON Canvas files - Interactive root person selection for canvas creation - Support for generational tree layout with nodes and edges - Handling of disconnected family trees Enhances family tree visualization by allowing users to generate an Obsidian-compatible canvas with person relationships --- src/canvas_generator.py | 389 ++++++++++++++++++++++++++++++++++++++++ src/individual.py | 67 ++++++- src/main.py | 85 ++++++--- src/person_selector.py | 106 +++++++++++ 4 files changed, 622 insertions(+), 25 deletions(-) create mode 100644 src/canvas_generator.py create mode 100644 src/person_selector.py diff --git a/src/canvas_generator.py b/src/canvas_generator.py new file mode 100644 index 0000000..8b75b3d --- /dev/null +++ b/src/canvas_generator.py @@ -0,0 +1,389 @@ +""" +Canvas generator for creating Obsidian Canvas family trees. + +This module generates JSON Canvas files that visualize family relationships +from GEDCOM data in a generational tree layout compatible with Obsidian. +""" + +import json +import logging +import os +import uuid +from typing import List, Dict, Tuple +from collections import defaultdict, deque +from individual import Individual + + +logger = logging.getLogger(__name__) + + +class CanvasGenerator: + """Generates Obsidian Canvas files for family tree visualization.""" + + # Layout constants + NODE_WIDTH = 250 + NODE_BASE_HEIGHT = 60 # Base height, will grow with content + HORIZONTAL_SPACING = 100 + VERTICAL_SPACING = 200 + IMAGE_HEIGHT = 150 # Height for nodes with images + TREE_SPACING = 400 # Space between disconnected trees + + def __init__(self, individuals: List[Individual], output_dir: str): + """ + Initialize the canvas generator. + + Args: + individuals: List of all individuals from GEDCOM + output_dir: Directory where canvas file will be saved + """ + self.individuals = individuals + self.output_dir = output_dir + + # Create lookup dictionary for fast access + self.individual_map: Dict[str, Individual] = { + ind.get_pointer(): ind for ind in individuals + } + + # Canvas data structures + self.nodes: List[Dict] = [] + self.edges: List[Dict] = [] + + logger.info(f"Initialized CanvasGenerator with {len(individuals)} individuals") + + def generate_canvas(self, root_person_id: str, canvas_filename: str = "Family Tree.canvas") -> str: + """ + Generate canvas file with family tree visualization. + + Args: + root_person_id: GEDCOM pointer of the root person + canvas_filename: Name of the output canvas file + + Returns: + Path to the generated canvas file + """ + logger.info(f"Generating canvas with root person: {root_person_id}") + + # Build family tree structure starting from root + tree_structure = self._build_tree_structure(root_person_id) + + # Calculate node positions using generational layout + positioned_nodes = self._calculate_positions(tree_structure) + + # Create canvas nodes and edges + self._create_canvas_elements(positioned_nodes, tree_structure) + + # Find and add disconnected family trees + self._add_disconnected_trees(tree_structure) + + # Write canvas file + canvas_path = os.path.join(self.output_dir, canvas_filename) + self._write_canvas_file(canvas_path) + + logger.info(f"Canvas generated with {len(self.nodes)} nodes and {len(self.edges)} edges") + return canvas_path + + def _build_tree_structure(self, root_id: str) -> Dict[str, Dict]: + """ + Build tree structure starting from root person using BFS. + + Returns a dict mapping person_id -> { + 'individual': Individual, + 'generation': int, + 'spouses': List[str], + 'children': List[str], + 'parents': List[str] + } + """ + structure = {} + visited = set() + queue = deque([(root_id, 0)]) # (person_id, generation) + + while queue: + person_id, generation = queue.popleft() + + if person_id in visited or person_id not in self.individual_map: + continue + + visited.add(person_id) + individual = self.individual_map[person_id] + + # Initialize structure for this person + structure[person_id] = { + 'individual': individual, + 'generation': generation, + 'spouses': [], + 'children': [], + 'parents': [] + } + + # Get families where this person is a child (to find parents) + families_as_child = individual.get_families_as_child() + for family in families_as_child: + # Add parents + if family.get('father'): + father_id = family['father'] + structure[person_id]['parents'].append(father_id) + if father_id not in visited: + queue.append((father_id, generation - 1)) + + if family.get('mother'): + mother_id = family['mother'] + structure[person_id]['parents'].append(mother_id) + if mother_id not in visited: + queue.append((mother_id, generation - 1)) + + # Get families where this person is a spouse (to find spouses and children) + families = individual.get_families() + for family in families: + # Add spouse + partner = family.get('partner') + if partner: + spouse_id = partner.get_pointer() + if spouse_id and spouse_id not in structure[person_id]['spouses']: + structure[person_id]['spouses'].append(spouse_id) + if spouse_id not in visited: + queue.append((spouse_id, generation)) + + # Add children + children = family.get('children', []) + for child in children: + child_id = child.get_pointer() + if child_id and child_id not in structure[person_id]['children']: + structure[person_id]['children'].append(child_id) + if child_id not in visited: + queue.append((child_id, generation + 1)) + + logger.info(f"Built tree structure with {len(structure)} people from root {root_id}") + return structure + + def _calculate_positions(self, tree_structure: Dict[str, Dict]) -> Dict[str, Tuple[int, int]]: + """ + Calculate x, y positions for each person using generational layout. + + Returns dict mapping person_id -> (x, y) + """ + # Group people by generation + generations: Dict[int, List[str]] = defaultdict(list) + for person_id, data in tree_structure.items(): + generations[data['generation']].append(person_id) + + positions = {} + min_gen = min(generations.keys()) if generations else 0 + max_gen = max(generations.keys()) if generations else 0 + + # Calculate positions generation by generation + for gen in range(min_gen, max_gen + 1): + people_in_gen = generations.get(gen, []) + + # Sort by last name for consistent positioning + people_in_gen.sort(key=lambda pid: self.individual_map[pid].get_names()[1] or "") + + # Calculate Y position (vertical, based on generation) + y_pos = gen * (self.NODE_BASE_HEIGHT + self.VERTICAL_SPACING) + + # Calculate X positions (horizontal, evenly spaced) + total_width = len(people_in_gen) * self.NODE_WIDTH + (len(people_in_gen) - 1) * self.HORIZONTAL_SPACING + start_x = -(total_width // 2) # Center the generation + + for idx, person_id in enumerate(people_in_gen): + x_pos = start_x + idx * (self.NODE_WIDTH + self.HORIZONTAL_SPACING) + positions[person_id] = (x_pos, y_pos) + + return positions + + def _create_canvas_elements(self, positions: Dict[str, Tuple[int, int]], tree_structure: Dict[str, Dict]): + """ + Create canvas nodes and edges from positioned tree structure. + """ + node_ids = {} # Map person_id -> canvas node_id + + # Create nodes + for person_id, (x, y) in positions.items(): + individual = tree_structure[person_id]['individual'] + node_id = self._create_node(individual, x, y) + node_ids[person_id] = node_id + + # Create edges + for person_id, data in tree_structure.items(): + from_node_id = node_ids.get(person_id) + if not from_node_id: + continue + + # Create parent-child edges + for child_id in data['children']: + to_node_id = node_ids.get(child_id) + if to_node_id: + self._create_edge(from_node_id, to_node_id, "Child", "bottom", "top") + + # Create spouse edges + for spouse_id in data['spouses']: + to_node_id = node_ids.get(spouse_id) + if to_node_id: + # Only create edge once (from lower ID to higher ID to avoid duplicates) + if person_id < spouse_id: + self._create_edge(from_node_id, to_node_id, "Spouse", "right", "left") + + def _add_disconnected_trees(self, main_tree: Dict[str, Dict]): + """ + Find individuals not in main tree and create separate tree groups. + """ + connected_ids = set(main_tree.keys()) + disconnected_ids = set(self.individual_map.keys()) - connected_ids + + if not disconnected_ids: + logger.info("No disconnected family trees found") + return + + logger.info(f"Found {len(disconnected_ids)} people in disconnected trees") + + # Find separate tree roots (people with no parents in the disconnected set) + tree_roots = [] + + for person_id in disconnected_ids: + individual = self.individual_map[person_id] + families_as_child = individual.get_families_as_child() + + has_parent_in_disconnected = False + for family in families_as_child: + father_id = family.get('father') + mother_id = family.get('mother') + if (father_id and father_id in disconnected_ids) or (mother_id and mother_id in disconnected_ids): + has_parent_in_disconnected = True + break + + if not has_parent_in_disconnected: + tree_roots.append(person_id) + + logger.info(f"Found {len(tree_roots)} disconnected tree roots") + + # Calculate offset for disconnected trees + # Place them to the right of the main tree + if self.nodes: + max_x = max(node['x'] + node['width'] for node in self.nodes) + offset_x = max_x + self.TREE_SPACING + else: + offset_x = 0 + + # Build and position each disconnected tree + for root_id in tree_roots: + if root_id not in disconnected_ids: + continue # Already processed + + # Build tree structure for this root + tree_structure = self._build_tree_structure(root_id) + + # Update disconnected_ids to track what we've processed + disconnected_ids -= set(tree_structure.keys()) + + # Calculate positions + positions = self._calculate_positions(tree_structure) + + # Offset positions + offset_positions = { + pid: (x + offset_x, y) for pid, (x, y) in positions.items() + } + + # Create nodes and edges + self._create_canvas_elements(offset_positions, tree_structure) + + # Update offset for next tree + if offset_positions: + max_x_in_tree = max(x + self.NODE_WIDTH for x, y in offset_positions.values()) + offset_x = max_x_in_tree + self.TREE_SPACING + + def _create_node(self, individual: Individual, x: int, y: int) -> str: + """ + Create a canvas node for an individual. + + Returns the node ID. + """ + node_id = str(uuid.uuid4().hex[:16]) + first_name, last_name = individual.get_names() + + # Get filename (same logic as in markdown generator) + birth_year = "" + events = individual.get_events() + for event in events: + if event["type"] == "BIRT" and event.get("date"): + import re + year_match = re.search(r'\b(\d{4})\b', event["date"]) + if year_match: + birth_year = year_match.group(1) + break + + # Build filename for WikiLink + filename_parts = [] + if last_name: + filename_parts.append(last_name) + if first_name: + filename_parts.append(first_name) + if birth_year: + filename_parts.append(birth_year) + filename = " ".join(filename_parts) + + # Get images + images = individual.get_images() + has_image = len(images) > 0 + + # Build node text content + text_parts = [] + + # Add image if available (use first image) + if has_image: + # images is a list of dicts with 'file', 'title', 'format' keys + image_file = images[0].get('file', '') + if image_file: + text_parts.append(f"![Image]({image_file})") + + # Add WikiLink to person's markdown file + text_parts.append(f"[[{filename}]]") + + node_text = "\n".join(text_parts) + + # Calculate height based on content + height = self.IMAGE_HEIGHT if has_image else self.NODE_BASE_HEIGHT + + node = { + "id": node_id, + "type": "text", + "text": node_text, + "x": x, + "y": y, + "width": self.NODE_WIDTH, + "height": height + } + + self.nodes.append(node) + return node_id + + def _create_edge(self, from_node: str, to_node: str, label: str, + from_side: str = "bottom", to_side: str = "top"): + """ + Create a canvas edge between two nodes. + """ + edge_id = str(uuid.uuid4().hex[:16]) + + edge = { + "id": edge_id, + "fromNode": from_node, + "fromSide": from_side, + "toNode": to_node, + "toSide": to_side, + "label": label + } + + self.edges.append(edge) + + def _write_canvas_file(self, canvas_path: str): + """ + Write canvas data to JSON file. + """ + canvas_data = { + "nodes": self.nodes, + "edges": self.edges + } + + with open(canvas_path, 'w', encoding='utf-8') as f: + json.dump(canvas_data, f, indent="\t") + + logger.info(f"Canvas file written to: {canvas_path}") diff --git a/src/individual.py b/src/individual.py index df4c23c..9ec429e 100644 --- a/src/individual.py +++ b/src/individual.py @@ -40,12 +40,21 @@ class Individual: def get_id(self) -> str: """ Provide the GEDCOM identifier for this individual without surrounding '@' characters. - + Returns: str: The GEDCOM identifier with all '@' characters removed. """ return self.element.get_pointer().replace('@', '') + def get_pointer(self) -> str: + """ + Get the full GEDCOM pointer/ID for this individual (with @ symbols). + + Returns: + str: The GEDCOM pointer (e.g., '@I123@') + """ + return self.element.get_pointer() + def get_names(self) -> Tuple[str, str]: """ Return the individual's first and last name with surrounding whitespace removed. @@ -225,6 +234,62 @@ class Individual: return families + def get_families_as_child(self) -> List[Dict]: + """ + Get all families where this person is a child (to find parents). + + Returns: + List of dictionaries with family information including: + - father: str (father's GEDCOM ID) or None + - mother: str (mother's GEDCOM ID) or None + """ + families = [] + + # Use the python-gedcom method to get parents + # Then find the family record that connects them + parent_elements = self.gedcom.get_parents(self.element) + + # Get all family records + for family in self.gedcom.get_root_child_elements(): + if family.get_tag() == gedcom.tags.GEDCOM_TAG_FAMILY: + # Check if this person is a child in this family + children = self.gedcom.get_family_members(family, gedcom.tags.GEDCOM_TAG_CHILD) + child_pointers = [c.get_pointer() for c in children] + + if self.element.get_pointer() in child_pointers: + # This person is a child in this family, get the parents + parents = self.gedcom.get_family_members(family, "PARENTS") + + father_id = None + mother_id = None + + for parent in parents: + # Determine gender to assign father/mother + # Check gender tag + gender = None + for child_elem in parent.get_child_elements(): + if child_elem.get_tag() == "SEX": + gender = child_elem.get_value() + break + + if gender == "M": + father_id = parent.get_pointer() + elif gender == "F": + mother_id = parent.get_pointer() + else: + # If no gender specified, assign to father if empty, else mother + if not father_id: + father_id = parent.get_pointer() + elif not mother_id: + mother_id = parent.get_pointer() + + families.append({ + "father": father_id, + "mother": mother_id, + }) + + return families + def get_events(self) -> List[Dict[str, str]]: """ Collects the individual's life events found on the GEDCOM element. diff --git a/src/main.py b/src/main.py index f2e10f5..74761c1 100644 --- a/src/main.py +++ b/src/main.py @@ -19,17 +19,21 @@ from gedcom_parser import GedcomParser from individual import Individual from markdown_generator import MarkdownGenerator from index_generator import IndexGenerator +from canvas_generator import CanvasGenerator +from person_selector import select_root_person def setup_logging(verbose: bool = False): """ Configure root logger formatting and level for the application. - + Sets the logging level to DEBUG when `verbose` is True, otherwise to INFO. - Also applies a consistent message format and timestamp date format used across the application. - + Also applies a consistent message format and timestamp date format used + across the application. + Parameters: - verbose (bool): When True, enable DEBUG-level logging; otherwise use INFO-level logging. + verbose (bool): When True, enable DEBUG-level logging; otherwise + use INFO-level logging. """ level = logging.DEBUG if verbose else logging.INFO logging.basicConfig( @@ -41,16 +45,18 @@ def setup_logging(verbose: bool = False): def extract_gedzip(zip_path: Path, temp_dir: Path) -> Tuple[Path, Optional[Path]]: """ - Extracts a ZIP/GEDZIP archive and locates the GEDCOM file and an optional media directory. - - Searches the extracted contents for the first `.ged` file and for image files (jpg, jpeg, png, gif, bmp) - to identify a media directory. - + Extracts a ZIP/GEDZIP archive and locates the GEDCOM file and an optional + media directory. + + Searches the extracted contents for the first `.ged` file and for image + files (jpg, jpeg, png, gif, bmp) to identify a media directory. + Returns: - A tuple `(gedcom_file_path, media_directory_path)` where `gedcom_file_path` is the path to the found - GEDCOM file and `media_directory_path` is the path to the directory containing media files or `None` - if no media files were found. - + A tuple `(gedcom_file_path, media_directory_path)` where + `gedcom_file_path` is the path to the found GEDCOM file and + `media_directory_path` is the path to the directory containing media + files or `None` if no media files were found. + Raises: ValueError: If no GEDCOM (`.ged`) file is found in the archive. """ @@ -96,17 +102,25 @@ def convert_gedcom_to_markdown( create_index: bool = True, media_dir: Optional[Path] = None, use_flat_structure: bool = False, + create_canvas: bool = False, ) -> int: """ Convert a GEDCOM file into Obsidian-compatible Markdown notes organized on disk. - + Parameters: gedcom_file (Path): Path to the input GEDCOM file. - output_dir (Path): Directory where generated markdown, media, and story files will be written. - create_index (bool): Whether to generate an index file linking the generated person notes. - media_dir (Optional[Path]): Optional source directory of media files to copy into the output media directory. - use_flat_structure (bool): If True, write all outputs directly into `output_dir`; if False, create subdirectories (`people/`, `media/`, `stories/`). - + output_dir (Path): Directory where generated markdown, media, and + story files will be written. + create_index (bool): Whether to generate an index file linking the + generated person notes. + media_dir (Optional[Path]): Optional source directory of media files + to copy into the output media directory. + use_flat_structure (bool): If True, write all outputs directly into + `output_dir`; if False, create subdirectories (`people/`, + `media/`, `stories/`). + create_canvas (bool): Whether to generate an Obsidian canvas file for + family tree visualization. + Returns: int: 0 on success, 1 on failure. """ @@ -147,6 +161,19 @@ def convert_gedcom_to_markdown( # Wrap individuals in our data model individuals = [Individual(elem, parser) for elem in individual_elements] + # Generate canvas if requested + if create_canvas: + logger.info("Canvas generation requested") + root_person_id = select_root_person(individuals) + + if root_person_id: + logger.info(f"Generating canvas with root person: {root_person_id}") + canvas_gen = CanvasGenerator(individuals, str(output_dir)) + canvas_path = canvas_gen.generate_canvas(root_person_id) + logger.info(f"Canvas created: {canvas_path}") + else: + logger.warning("No root person selected, skipping canvas generation") + # Generate markdown notes logger.info(f"Generating markdown notes in: {people_dir}") generator = MarkdownGenerator( @@ -206,10 +233,13 @@ def convert_gedcom_to_markdown( def main(): """ - Run the command-line interface to convert a GEDCOM or GEDZIP archive into Obsidian-compatible Markdown notes. - - Parses CLI arguments, prepares input and output paths (extracting archives to a temporary directory when needed), invokes the conversion process, and cleans up any temporary files. - + Run the command-line interface to convert a GEDCOM or GEDZIP archive + into Obsidian-compatible Markdown notes. + + Parses CLI arguments, prepares input and output paths (extracting + archives to a temporary directory when needed), invokes the conversion + process, and cleans up any temporary files. + Returns: int: Exit code where `0` indicates success and `1` indicates failure. """ @@ -246,6 +276,12 @@ def main(): "--no-index", action="store_true", help="Do not create an index file" ) + parser.add_argument( + "--canvas", + action="store_true", + help="Create an Obsidian canvas file for family tree visualization", + ) + parser.add_argument( "-v", "--verbose", action="store_true", help="Enable verbose logging" ) @@ -283,6 +319,7 @@ def main(): create_index=not args.no_index, media_dir=media_dir, use_flat_structure=args.flat, + create_canvas=args.canvas, ) return exit_code @@ -295,4 +332,4 @@ def main(): if __name__ == "__main__": - sys.exit(main()) \ No newline at end of file + sys.exit(main()) diff --git a/src/person_selector.py b/src/person_selector.py new file mode 100644 index 0000000..03a2064 --- /dev/null +++ b/src/person_selector.py @@ -0,0 +1,106 @@ +""" +Interactive person selector for choosing root person in family tree. + +This module provides functionality to display individuals from a GEDCOM file +in a multi-column format and allow the user to select one as the root person +for canvas generation. +""" + +import logging +from typing import List, Optional +from individual import Individual + + +logger = logging.getLogger(__name__) + + +def select_root_person(individuals: List[Individual]) -> Optional[str]: + """ + Display an interactive list of individuals and let user select root person. + + Args: + individuals: List of Individual objects from GEDCOM file + + Returns: + The GEDCOM pointer/ID of the selected individual, or None if cancelled + """ + if not individuals: + logger.error("No individuals provided for selection") + return None + + # Sort individuals by last name, then first name + sorted_individuals = sorted( + individuals, + key=lambda ind: ( + ind.get_names()[1] or "", # Last name + ind.get_names()[0] or "" # First name + ) + ) + + print("\n" + "="*80) + print("SELECT ROOT PERSON FOR FAMILY TREE CANVAS") + print("="*80) + print(f"\nTotal individuals: {len(sorted_individuals)}\n") + + # Display individuals in a numbered list with columns + # Format: Index | Name | Birth | Death + print(f"{'#':<5} {'Name':<40} {'Birth':<12} {'Death':<12}") + print("-" * 80) + + for idx, individual in enumerate(sorted_individuals, start=1): + first_name, last_name = individual.get_names() + full_name = f"{last_name or ''} {first_name or ''}".strip() + + # Get birth and death years + birth_year = "" + death_year = "" + events = individual.get_events() + + for event in events: + if event["type"] == "BIRT" and event.get("date"): + # Extract year from date string (format varies) + date_str = event["date"] + # Try to find a 4-digit year + import re + year_match = re.search(r'\b(\d{4})\b', date_str) + if year_match: + birth_year = year_match.group(1) + elif event["type"] == "DEAT" and event.get("date"): + date_str = event["date"] + import re + year_match = re.search(r'\b(\d{4})\b', date_str) + if year_match: + death_year = year_match.group(1) + + print(f"{idx:<5} {full_name:<40} {birth_year:<12} {death_year:<12}") + + print("-" * 80) + + # Prompt for selection + while True: + try: + selection = input("\nEnter number to select root person (or 'q' to quit): ").strip() + + if selection.lower() == 'q': + logger.info("User cancelled person selection") + return None + + index = int(selection) + if 1 <= index <= len(sorted_individuals): + selected = sorted_individuals[index - 1] + first_name, last_name = selected.get_names() + full_name = f"{last_name or ''} {first_name or ''}".strip() + + print(f"\nSelected: {full_name}") + logger.info(f"User selected root person: {full_name} ({selected.get_pointer()})") + + return selected.get_pointer() + else: + print(f"Please enter a number between 1 and {len(sorted_individuals)}") + + except ValueError: + print("Invalid input. Please enter a number or 'q' to quit.") + except KeyboardInterrupt: + print("\n\nSelection cancelled.") + logger.info("User cancelled person selection with Ctrl+C") + return None -- 2.36.6 From 635adb09ae7ea4bbde5bdaf1ca025bd2a252a3bc Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Sat, 1 Nov 2025 18:16:36 +0100 Subject: [PATCH 2/5] Updates .gitignore to track test output Adds test_output/ directory to ignored files list Ensures local test output remains untracked in version control --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index e85f847..4e50b8a 100644 --- a/.gitignore +++ b/.gitignore @@ -126,3 +126,5 @@ dmypy.json .DS_Store examples/ + +test_output/ -- 2.36.6 From 18fd58972f48ff6c4e1e1618e4cedb10bf009af5 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Sun, 2 Nov 2025 14:08:23 +0100 Subject: [PATCH 3/5] Enhance canvas generator with advanced tree layout Refactors canvas generation to support more complex family tree visualization Introduces sophisticated layout algorithms for: - Generational positioning of individuals - Handling spouse and children relationships - Vertical and horizontal spacing strategies - Support for disconnected family trees Improves visual representation of genealogical data with more nuanced node placement and connection logic --- .gitignore | 2 + src/canvas_generator.py | 835 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 805 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 4e50b8a..acddcc8 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,5 @@ dmypy.json examples/ test_output/ + +output/ diff --git a/src/canvas_generator.py b/src/canvas_generator.py index 8b75b3d..8444edb 100644 --- a/src/canvas_generator.py +++ b/src/canvas_generator.py @@ -10,7 +10,7 @@ import logging import os import uuid from typing import List, Dict, Tuple -from collections import defaultdict, deque +from collections import deque from individual import Individual @@ -22,10 +22,11 @@ class CanvasGenerator: # Layout constants NODE_WIDTH = 250 - NODE_BASE_HEIGHT = 60 # Base height, will grow with content - HORIZONTAL_SPACING = 100 - VERTICAL_SPACING = 200 - IMAGE_HEIGHT = 150 # Height for nodes with images + NODE_BASE_HEIGHT = 60 # Base height for nodes without images + GENERATION_SPACING = 680 # Horizontal spacing between generations (left-to-right) + SIBLING_SPACING = 305 # Vertical spacing between siblings when stacked + COUPLE_SPACING = 190 # Vertical spacing between spouses when stacked + IMAGE_HEIGHT = 350 # Height for nodes with images (increased to show image + name) TREE_SPACING = 400 # Space between disconnected trees def __init__(self, individuals: List[Individual], output_dir: str): @@ -62,9 +63,11 @@ class CanvasGenerator: Path to the generated canvas file """ logger.info(f"Generating canvas with root person: {root_person_id}") + logger.info(f"Total individuals in GEDCOM: {len(self.individuals)}") # Build family tree structure starting from root tree_structure = self._build_tree_structure(root_person_id) + logger.info(f"Main tree contains {len(tree_structure)} people connected to root") # Calculate node positions using generational layout positioned_nodes = self._calculate_positions(tree_structure) @@ -80,6 +83,7 @@ class CanvasGenerator: self._write_canvas_file(canvas_path) logger.info(f"Canvas generated with {len(self.nodes)} nodes and {len(self.edges)} edges") + logger.info(f"Total people represented: {len(self.nodes)}/{len(self.individuals)}") return canvas_path def _build_tree_structure(self, root_id: str) -> Dict[str, Dict]: @@ -158,39 +162,789 @@ class CanvasGenerator: def _calculate_positions(self, tree_structure: Dict[str, Dict]) -> Dict[str, Tuple[int, int]]: """ - Calculate x, y positions for each person using generational layout. + Calculate positions using left-to-right timeline layout. + + Layout strategy: + - X-axis: Generational flow (children left, parents right) + - Y-axis: Vertical stacking of siblings + - Start with root person at (0, 0) + - Place descendants to the LEFT + - Place ancestors to the RIGHT + - Stack siblings VERTICALLY with minimal spacing Returns dict mapping person_id -> (x, y) """ - # Group people by generation - generations: Dict[int, List[str]] = defaultdict(list) - for person_id, data in tree_structure.items(): - generations[data['generation']].append(person_id) - positions = {} - min_gen = min(generations.keys()) if generations else 0 - max_gen = max(generations.keys()) if generations else 0 - # Calculate positions generation by generation - for gen in range(min_gen, max_gen + 1): - people_in_gen = generations.get(gen, []) + if not tree_structure: + return positions - # Sort by last name for consistent positioning - people_in_gen.sort(key=lambda pid: self.individual_map[pid].get_names()[1] or "") + # Find root person (generation 0) + root_id = None + for person_id, data in tree_structure.items(): + if data['generation'] == 0: + root_id = person_id + break - # Calculate Y position (vertical, based on generation) - y_pos = gen * (self.NODE_BASE_HEIGHT + self.VERTICAL_SPACING) + if not root_id: + logger.error("No root person found at generation 0") + return positions - # Calculate X positions (horizontal, evenly spaced) - total_width = len(people_in_gen) * self.NODE_WIDTH + (len(people_in_gen) - 1) * self.HORIZONTAL_SPACING - start_x = -(total_width // 2) # Center the generation + logger.info(f"Root person: {root_id} at generation 0") - for idx, person_id in enumerate(people_in_gen): - x_pos = start_x + idx * (self.NODE_WIDTH + self.HORIZONTAL_SPACING) - positions[person_id] = (x_pos, y_pos) + # Start root person at origin + positions[root_id] = (0, 0) + processed = {root_id} + + # Determine direction based on gender + # Male: grow upward (negative y), Female: grow downward (positive y) + root_individual = tree_structure[root_id]['individual'] + root_gender = root_individual.get_gender() + root_direction = 'up' if root_gender == 'M' else 'down' + logger.info(f"Root person gender: {root_gender}, direction: {root_direction}") + + # Place spouse vertically adjacent + root_data = tree_structure[root_id] + spouses = root_data.get('spouses', []) + if spouses: + spouse_id = spouses[0] + if spouse_id in tree_structure: + spouse_y = self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[spouse_id] = (0, spouse_y) + processed.add(spouse_id) + + # Determine spouse direction based on gender + spouse_individual = tree_structure[spouse_id]['individual'] + spouse_gender = spouse_individual.get_gender() + spouse_direction = 'up' if spouse_gender == 'M' else 'down' + + # Position spouse's siblings and their families + self._position_spouse_siblings(spouse_id, 0, spouse_y, tree_structure, positions, processed, spouse_direction) + # Position spouse's ancestors (parents, grandparents, etc.) to the RIGHT + self._layout_ancestors_right(spouse_id, tree_structure, positions, processed, spouse_direction) + + # Layout descendants (children) to the LEFT + self._layout_descendants_left(root_id, tree_structure, positions, processed) + + # Layout ancestors (parents) to the RIGHT + self._layout_ancestors_right(root_id, tree_structure, positions, processed, root_direction) + + # Position any remaining unprocessed people + unprocessed = set(tree_structure.keys()) - processed + if unprocessed: + logger.info(f"Positioning {len(unprocessed)} remaining unconnected people") + # Place them far to the right as a separate tree + max_x = max(x for x, y in positions.values()) if positions else 0 + current_y = 0 + for person_id in unprocessed: + positions[person_id] = (max_x + self.TREE_SPACING, current_y) + current_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + processed.add(person_id) + + logger.info(f"Positioned {len(positions)} out of {len(tree_structure)} people") return positions + def _layout_descendants_left( + self, + root_id: str, + tree_structure: Dict[str, Dict], + positions: Dict[str, Tuple[int, int]], + processed: set + ): + """ + Layout descendants to the left of root person with vertical sibling stacking. + + Each child is positioned to the left of their parent. + Siblings are stacked vertically. + """ + if root_id not in tree_structure or root_id not in positions: + return + + root_data = tree_structure[root_id] + children = root_data.get('children', []) + + if not children: + return + + # Get root position + root_x, root_y = positions[root_id] + + # Check if root has spouse - need to center children between root and spouse + spouses = root_data.get('spouses', []) + if spouses and spouses[0] in positions: + spouse_x, spouse_y = positions[spouses[0]] + parent_center_y = (root_y + spouse_y) / 2 + else: + parent_center_y = root_y + + # Calculate total vertical height needed for all children + total_children_height = 0 + child_heights = {} + + for child_id in children: + if child_id not in tree_structure: + continue + # Each child needs space for themselves + their spouse + child_height = self._calculate_family_height(child_id, tree_structure, set()) + child_heights[child_id] = child_height + total_children_height += child_height + + # Add spacing between siblings + if len(children) > 1: + total_children_height += (len(children) - 1) * self.SIBLING_SPACING + + # Position children starting from top, centered on parent + child_x = root_x - self.GENERATION_SPACING + start_y = parent_center_y - (total_children_height / 2) + current_y = start_y + + for child_id in children: + if child_id in processed or child_id not in tree_structure: + continue + + positions[child_id] = (child_x, current_y) + processed.add(child_id) + + # Position child's spouse below them + child_data = tree_structure[child_id] + child_spouses = child_data.get('spouses', []) + if child_spouses: + spouse_id = child_spouses[0] + if spouse_id in tree_structure and spouse_id not in processed: + spouse_y = current_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[spouse_id] = (child_x, spouse_y) + processed.add(spouse_id) + + # Determine spouse direction based on gender + spouse_individual = tree_structure[spouse_id]['individual'] + spouse_gender = spouse_individual.get_gender() + spouse_direction = 'up' if spouse_gender == 'M' else 'down' + + # Position spouse's siblings and their families + self._position_spouse_siblings(spouse_id, child_x, spouse_y, tree_structure, positions, processed, spouse_direction) + # Position spouse's ancestors (parents, grandparents, etc.) + self._layout_ancestors_right(spouse_id, tree_structure, positions, processed, spouse_direction) + + # Recursively layout this child's descendants (further left) + self._layout_descendants_left(child_id, tree_structure, positions, processed) + + # Move down for next sibling + current_y += child_heights.get(child_id, self.IMAGE_HEIGHT) + self.SIBLING_SPACING + + def _layout_ancestors_right( + self, + root_id: str, + tree_structure: Dict[str, Dict], + positions: Dict[str, Tuple[int, int]], + processed: set, + direction: str = 'down' + ): + """ + Layout ancestors to the right of root person with vertical sibling stacking. + + Parents are positioned to the right. + Parent couples are stacked vertically. + + Args: + direction: 'up' to grow upward (negative y), 'down' to grow downward (positive y) + """ + if root_id not in tree_structure or root_id not in positions: + return + + # Track minimum available y-coordinate at each x-position to avoid overlaps + min_y_at_x = {} + + # Process only this person's parents (not spouse's) + # Spouse's parents will be processed in their own call with their own direction + person_id = root_id + if person_id not in positions: + return + + person_data = tree_structure[person_id] + person_name = person_data['individual'].get_names() + logger.info(f"Processing ancestors for {person_name}, direction={direction}") + parents = person_data.get('parents', []) + + if not parents: + return + + person_x, person_y = positions[person_id] + parent_x = person_x + self.GENERATION_SPACING + + # Position parent couple + if len(parents) == 2: + father_id, mother_id = parents[0], parents[1] + + # Check if we need to avoid overlap at this x-position + if parent_x in min_y_at_x: + # Position relative to existing people at this x-coordinate + parent_y = min_y_at_x[parent_x] + logger.info(f"Using min_y_at_x for x={parent_x}: y={parent_y}, direction={direction}") + else: + # Center parent couple on their child + parent_y = person_y + logger.info(f"Centering parents on child at y={person_y}, direction={direction}") + + if father_id in tree_structure and father_id not in processed: + positions[father_id] = (parent_x, parent_y) + processed.add(father_id) + + mother_y = parent_y # Default if mother doesn't exist + if mother_id in tree_structure and mother_id not in processed: + if direction == 'up': + mother_y = parent_y - self.IMAGE_HEIGHT - self.COUPLE_SPACING + else: + mother_y = parent_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[mother_id] = (parent_x, mother_y) + processed.add(mother_id) + + # Position siblings of both parents at same x-position, stacked vertically + if direction == 'up': + current_sibling_y = min(parent_y, mother_y) - self.SIBLING_SPACING - self.IMAGE_HEIGHT + else: + current_sibling_y = max(parent_y, mother_y) + self.IMAGE_HEIGHT + self.SIBLING_SPACING + + # Father's siblings + father_siblings = self._get_siblings(father_id, tree_structure) + for sibling_id in father_siblings: + if sibling_id in tree_structure and sibling_id not in processed: + positions[sibling_id] = (parent_x, current_sibling_y) + processed.add(sibling_id) + + # Position sibling's spouse + sibling_data = tree_structure[sibling_id] + sibling_spouses = sibling_data.get('spouses', []) + if sibling_spouses: + spouse_id = sibling_spouses[0] + if spouse_id in tree_structure and spouse_id not in processed: + if direction == 'up': + spouse_y = current_sibling_y - self.IMAGE_HEIGHT - self.COUPLE_SPACING + else: + spouse_y = current_sibling_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[spouse_id] = (parent_x, spouse_y) + processed.add(spouse_id) + # Position spouse's siblings and their families + self._position_spouse_siblings(spouse_id, parent_x, spouse_y, tree_structure, positions, processed, direction) + if direction == 'up': + current_sibling_y = min(current_sibling_y, spouse_y) - self.SIBLING_SPACING - self.IMAGE_HEIGHT + else: + current_sibling_y = max(current_sibling_y, spouse_y) + self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + if direction == 'up': + current_sibling_y -= self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + current_sibling_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + if direction == 'up': + current_sibling_y -= self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + current_sibling_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + + # Position sibling's children (cousins) to the left + self._layout_descendants_left(sibling_id, tree_structure, positions, processed) + + # Mother's siblings (who aren't already father's siblings) + mother_siblings = self._get_siblings(mother_id, tree_structure) + for sibling_id in mother_siblings: + if sibling_id not in father_siblings and sibling_id in tree_structure and sibling_id not in processed: + positions[sibling_id] = (parent_x, current_sibling_y) + processed.add(sibling_id) + + # Position sibling's spouse + sibling_data = tree_structure[sibling_id] + sibling_spouses = sibling_data.get('spouses', []) + if sibling_spouses: + spouse_id = sibling_spouses[0] + if spouse_id in tree_structure and spouse_id not in processed: + if direction == 'up': + spouse_y = current_sibling_y - self.IMAGE_HEIGHT - self.COUPLE_SPACING + else: + spouse_y = current_sibling_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[spouse_id] = (parent_x, spouse_y) + processed.add(spouse_id) + # Position spouse's siblings and their families + self._position_spouse_siblings(spouse_id, parent_x, spouse_y, tree_structure, positions, processed, direction) + if direction == 'up': + current_sibling_y = min(current_sibling_y, spouse_y) - self.SIBLING_SPACING - self.IMAGE_HEIGHT + else: + current_sibling_y = max(current_sibling_y, spouse_y) + self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + if direction == 'up': + current_sibling_y -= self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + current_sibling_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + if direction == 'up': + current_sibling_y -= self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + current_sibling_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + + # Position sibling's children (cousins) to the left + self._layout_descendants_left(sibling_id, tree_structure, positions, processed) + + # Update the minimum y for this x-position for next iteration + min_y_at_x[parent_x] = current_sibling_y + + # Recursively position their ancestors (further right) + # Process both parents' ancestors (maintain same direction) + if father_id in tree_structure: + self._layout_ancestors_right(father_id, tree_structure, positions, processed, direction) + if mother_id in tree_structure: + self._layout_ancestors_right(mother_id, tree_structure, positions, processed, direction) + + elif len(parents) == 1: + parent_id = parents[0] + if parent_id in tree_structure and parent_id not in processed: + positions[parent_id] = (parent_x, person_y) + processed.add(parent_id) + self._layout_ancestors_right(parent_id, tree_structure, positions, processed, direction) + + def _calculate_family_height( + self, + person_id: str, + tree_structure: Dict[str, Dict], + visited: set + ) -> int: + """ + Calculate total vertical height needed for a person and their spouse. + + Returns the height in pixels needed to display this person + spouse. + """ + if person_id in visited or person_id not in tree_structure: + return self.IMAGE_HEIGHT + + visited.add(person_id) + + data = tree_structure[person_id] + spouses = data.get('spouses', []) + + # Height for person + height = self.IMAGE_HEIGHT + + # Add spouse height if present + if spouses: + height += self.COUPLE_SPACING + self.IMAGE_HEIGHT + + return height + + def _get_siblings( + self, + person_id: str, + tree_structure: Dict[str, Dict] + ) -> list: + """ + Get all siblings of a person (people who share the same parents). + + Returns list of sibling IDs. + """ + if person_id not in tree_structure: + return [] + + person_data = tree_structure[person_id] + parents = person_data.get('parents', []) + + if not parents: + return [] + + # Find all children of the same parents + siblings = [] + + # Get children from parents + for parent_id in parents: + if parent_id in tree_structure: + parent_data = tree_structure[parent_id] + parent_children = parent_data.get('children', []) + for child_id in parent_children: + if child_id != person_id and child_id not in siblings: + siblings.append(child_id) + + return siblings + + def _position_spouse_siblings( + self, + spouse_id: str, + spouse_x: int, + spouse_y: int, + tree_structure: Dict[str, Dict], + positions: Dict[str, Tuple[int, int]], + processed: set, + direction: str = 'down' + ): + """ + Position the siblings of a spouse and their families. + + This ensures extended families (e.g., wife's siblings and their families) are included. + + Args: + direction: 'up' to grow upward (negative y), 'down' to grow downward (positive y) + """ + if spouse_id not in tree_structure: + return + + # Initialize current_y to position elements relative to the spouse + if direction == 'up': + current_y = spouse_y - self.IMAGE_HEIGHT - self.SIBLING_SPACING + else: + current_y = spouse_y + self.IMAGE_HEIGHT + self.SIBLING_SPACING + + # First, position the spouse's spouse's siblings if they exist + # (bidirectional spouse relationship) + spouse_data = tree_structure[spouse_id] + spouse_spouses = spouse_data.get('spouses', []) + if spouse_spouses: + for partner_id in spouse_spouses: + if partner_id in tree_structure and partner_id not in processed: + # Position the partner relative to the spouse + if direction == 'up': + partner_y = spouse_y - self.IMAGE_HEIGHT - self.COUPLE_SPACING + else: + partner_y = spouse_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[partner_id] = (spouse_x, partner_y) + processed.add(partner_id) + + if direction == 'up': + current_y = partner_y - self.SIBLING_SPACING - self.IMAGE_HEIGHT + else: + current_y = partner_y + self.IMAGE_HEIGHT + self.SIBLING_SPACING + + # Position the partner's siblings (spouse's in-laws) + partner_siblings = self._get_siblings(partner_id, tree_structure) + for sib_id in partner_siblings: + if sib_id in tree_structure and sib_id not in processed: + positions[sib_id] = (spouse_x, current_y) + processed.add(sib_id) + + # Position this in-law sibling's spouse + sib_data = tree_structure[sib_id] + sib_spouses = sib_data.get('spouses', []) + if sib_spouses and sib_spouses[0] in tree_structure and sib_spouses[0] not in processed: + if direction == 'up': + sib_spouse_y = current_y - self.IMAGE_HEIGHT - self.COUPLE_SPACING + else: + sib_spouse_y = current_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[sib_spouses[0]] = (spouse_x, sib_spouse_y) + processed.add(sib_spouses[0]) + + if direction == 'up': + current_y = sib_spouse_y - self.SIBLING_SPACING - self.IMAGE_HEIGHT + else: + current_y = sib_spouse_y + self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + if direction == 'up': + current_y -= self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + current_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + + # Position their children + self._layout_descendants_left(sib_id, tree_structure, positions, processed) + + # Get spouse's siblings + spouse_siblings = self._get_siblings(spouse_id, tree_structure) + + for sibling_id in spouse_siblings: + if sibling_id in tree_structure and sibling_id not in processed: + positions[sibling_id] = (spouse_x, current_y) + processed.add(sibling_id) + + # Position this sibling's spouse + sibling_data = tree_structure[sibling_id] + sibling_spouses = sibling_data.get('spouses', []) + if sibling_spouses: + sibling_spouse_id = sibling_spouses[0] + if sibling_spouse_id in tree_structure and sibling_spouse_id not in processed: + if direction == 'up': + sibling_spouse_y = current_y - self.IMAGE_HEIGHT - self.COUPLE_SPACING + else: + sibling_spouse_y = current_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + positions[sibling_spouse_id] = (spouse_x, sibling_spouse_y) + processed.add(sibling_spouse_id) + + if direction == 'up': + current_y = sibling_spouse_y - self.SIBLING_SPACING - self.IMAGE_HEIGHT + else: + current_y = sibling_spouse_y + self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + if direction == 'up': + current_y -= self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + current_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + if direction == 'up': + current_y -= self.IMAGE_HEIGHT + self.SIBLING_SPACING + else: + current_y += self.IMAGE_HEIGHT + self.SIBLING_SPACING + + # Position this sibling's children (to the left) + self._layout_descendants_left(sibling_id, tree_structure, positions, processed) + + def _calculate_subtree_widths( + self, + tree_structure: Dict[str, Dict], + person_id: str, + visited: set + ) -> Dict[str, int]: + """ + Calculate the width needed for each person's subtree (descendants). + + Width is measured in terms of minimum horizontal space needed. + """ + if person_id in visited or person_id not in tree_structure: + return {} + + visited.add(person_id) + widths = {} + + data = tree_structure[person_id] + children = data.get('children', []) + + if not children: + # Leaf node: width is just this person + spacing + widths[person_id] = self.NODE_WIDTH + self.HORIZONTAL_SPACING + else: + # Calculate width needed for all children + child_widths = 0 + for child_id in children: + if child_id in tree_structure: + child_subtree_widths = self._calculate_subtree_widths( + tree_structure, child_id, visited + ) + widths.update(child_subtree_widths) + child_widths += child_subtree_widths.get( + child_id, + self.NODE_WIDTH + self.HORIZONTAL_SPACING + ) + + # This person's width is the max of: + # 1. Their own width + spouse width + # 2. Total width of their children + spouses = data.get('spouses', []) + own_width = (len(spouses) + 1) * (self.NODE_WIDTH + self.HORIZONTAL_SPACING) + widths[person_id] = max(own_width, child_widths) + + return widths + + def _calculate_ancestor_widths( + self, + tree_structure: Dict[str, Dict], + person_id: str, + visited: set + ) -> Dict[str, int]: + """ + Calculate the width needed for ancestors (going up the tree). + + Similar to _calculate_subtree_widths but follows parent relationships. + """ + if person_id in visited or person_id not in tree_structure: + return {} + + visited.add(person_id) + widths = {} + + data = tree_structure[person_id] + parents = data.get('parents', []) + + if not parents: + # No parents: width is just this person + spacing + widths[person_id] = self.NODE_WIDTH + self.HORIZONTAL_SPACING + else: + # Calculate width needed for all parents + parent_widths = 0 + for parent_id in parents: + if parent_id in tree_structure: + parent_ancestor_widths = self._calculate_ancestor_widths( + tree_structure, parent_id, visited + ) + widths.update(parent_ancestor_widths) + parent_widths += parent_ancestor_widths.get( + parent_id, + self.NODE_WIDTH + self.HORIZONTAL_SPACING + ) + + # This person's width is the max of their own width and parents' width + widths[person_id] = max( + self.NODE_WIDTH + self.HORIZONTAL_SPACING, + parent_widths + ) + + return widths + + def _layout_ancestors( + self, + root_person_id: str, + tree_structure: Dict[str, Dict], + positions: Dict[str, Tuple[int, int]], + processed: set, + subtree_widths: Dict[str, int] + ): + """ + Layout ancestors above their children. + + Positions each parent couple directly above their child, creating + a compact tree structure rather than spreading all parents horizontally. + + Args: + root_person_id: ID of the root person (already positioned) + tree_structure: Full tree structure + positions: Dict to store calculated positions + processed: Set of already-processed person IDs + subtree_widths: Pre-calculated widths for all people + """ + # Process all currently positioned people and add their parents + # We need to iterate multiple times to handle all ancestor generations + max_iterations = 10 # Safety limit + + for iteration in range(max_iterations): + # Find all people who are positioned but whose parents aren't yet + people_to_process = [] + for person_id in list(positions.keys()): + if person_id not in tree_structure: + continue + + data = tree_structure[person_id] + parents = data.get('parents', []) + + # Check if this person has unpositioned parents + has_unpositioned_parents = False + for parent_id in parents: + if parent_id in tree_structure and parent_id not in processed: + has_unpositioned_parents = True + break + + if has_unpositioned_parents: + people_to_process.append(person_id) + + if not people_to_process: + break # No more parents to position + + # Position parents for each person + for person_id in people_to_process: + if person_id not in positions: + continue + + child_x, child_y = positions[person_id] + data = tree_structure[person_id] + parents = data.get('parents', []) + + if not parents: + continue + + # Calculate Y position for parents (one generation above) + parent_y = child_y - (self.VERTICAL_SPACING + self.IMAGE_HEIGHT) + + if len(parents) == 2: + # Both parents: center the couple above the child + father_id, mother_id = parents[0], parents[1] + + # Calculate couple width + couple_width = 2 * self.NODE_WIDTH + self.HORIZONTAL_SPACING + + # Center couple above child + couple_start_x = child_x - (couple_width - self.NODE_WIDTH) // 2 + + # Position father + if father_id in tree_structure and father_id not in processed: + positions[father_id] = (couple_start_x, parent_y) + processed.add(father_id) + + # Position mother next to father + mother_x = couple_start_x + self.NODE_WIDTH + self.HORIZONTAL_SPACING + if mother_id in tree_structure and mother_id not in processed: + positions[mother_id] = (mother_x, parent_y) + processed.add(mother_id) + + elif len(parents) == 1: + # Single parent: center above child + parent_id = parents[0] + if parent_id in tree_structure and parent_id not in processed: + positions[parent_id] = (child_x, parent_y) + processed.add(parent_id) + + def _layout_person_and_descendants( + self, + person_id: str, + tree_structure: Dict[str, Dict], + positions: Dict[str, Tuple[int, int]], + processed: set, + subtree_widths: Dict[str, int], + x_offset: int, + y_pos: int + ): + """ + Recursively layout a person, their spouse, and descendants. + + Args: + person_id: ID of person to layout + tree_structure: Full tree structure + positions: Dict to store calculated positions + processed: Set of already-processed person IDs + subtree_widths: Pre-calculated subtree widths + x_offset: Left edge x-coordinate for this person's subtree + y_pos: Y-coordinate for this person + """ + if person_id not in tree_structure: + return + + if person_id in processed: + logger.debug(f"Skipping already processed person: {person_id}") + return + + processed.add(person_id) + data = tree_structure[person_id] + logger.debug(f"Positioning {person_id} at y={y_pos}") + + # Get spouse(s) + spouses = data.get('spouses', []) + + # Calculate width for this person + spouses + num_people = len(spouses) + 1 + people_width = num_people * self.NODE_WIDTH + (num_people - 1) * self.HORIZONTAL_SPACING + + # Get subtree width + total_width = subtree_widths.get(person_id, people_width) + + # Center this person (and spouses) within their subtree width + people_start_x = x_offset + (total_width - people_width) // 2 + + # Position this person + positions[person_id] = (people_start_x, y_pos) + + # Position spouse(s) to the right + current_x = people_start_x + self.NODE_WIDTH + self.HORIZONTAL_SPACING + for spouse_id in spouses: + if spouse_id not in processed and spouse_id in tree_structure: + positions[spouse_id] = (current_x, y_pos) + processed.add(spouse_id) + current_x += self.NODE_WIDTH + self.HORIZONTAL_SPACING + + # Layout children below + children = data.get('children', []) + if children: + # Calculate y position for children + child_y = y_pos + self.VERTICAL_SPACING + self.IMAGE_HEIGHT + + # Position children left-to-right, centered under parents + child_x_offset = x_offset + + for child_id in children: + if child_id in tree_structure and child_id not in processed: + child_width = subtree_widths.get( + child_id, + self.NODE_WIDTH + self.HORIZONTAL_SPACING + ) + + self._layout_person_and_descendants( + child_id, + tree_structure, + positions, + processed, + subtree_widths, + child_x_offset, + child_y + ) + + child_x_offset += child_width + def _create_canvas_elements(self, positions: Dict[str, Tuple[int, int]], tree_structure: Dict[str, Dict]): """ Create canvas nodes and edges from positioned tree structure. @@ -213,7 +967,7 @@ class CanvasGenerator: for child_id in data['children']: to_node_id = node_ids.get(child_id) if to_node_id: - self._create_edge(from_node_id, to_node_id, "Child", "bottom", "top") + self._create_edge(from_node_id, to_node_id, "Child", "left", "right") # Create spouse edges for spouse_id in data['spouses']: @@ -221,7 +975,7 @@ class CanvasGenerator: if to_node_id: # Only create edge once (from lower ID to higher ID to avoid duplicates) if person_id < spouse_id: - self._create_edge(from_node_id, to_node_id, "Spouse", "right", "left") + self._create_edge(from_node_id, to_node_id, "Spouse", "bottom", "top", bidirectional=True) def _add_disconnected_trees(self, main_tree: Dict[str, Dict]): """ @@ -271,6 +1025,7 @@ class CanvasGenerator: # Build tree structure for this root tree_structure = self._build_tree_structure(root_id) + logger.info(f" Disconnected tree from {root_id}: {len(tree_structure)} people") # Update disconnected_ids to track what we've processed disconnected_ids -= set(tree_structure.keys()) @@ -356,10 +1111,19 @@ class CanvasGenerator: self.nodes.append(node) return node_id - def _create_edge(self, from_node: str, to_node: str, label: str, - from_side: str = "bottom", to_side: str = "top"): + def _create_edge(self, from_node: str, to_node: str, label: str = "", + from_side: str = "bottom", to_side: str = "top", + bidirectional: bool = False): """ Create a canvas edge between two nodes. + + Args: + from_node: Source node ID + to_node: Target node ID + label: Edge label (optional) + from_side: Which side of source node to connect from + to_side: Which side of target node to connect to + bidirectional: If True, adds arrow on fromEnd to make it bidirectional """ edge_id = str(uuid.uuid4().hex[:16]) @@ -368,10 +1132,17 @@ class CanvasGenerator: "fromNode": from_node, "fromSide": from_side, "toNode": to_node, - "toSide": to_side, - "label": label + "toSide": to_side } + # Add label only if provided + if label: + edge["label"] = label + + # Add bidirectional arrow if requested + if bidirectional: + edge["fromEnd"] = "arrow" + self.edges.append(edge) def _write_canvas_file(self, canvas_path: str): -- 2.36.6 From 5a73b3ceeb03eb15ee3708dbb393002a4e591887 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Sun, 2 Nov 2025 14:22:13 +0100 Subject: [PATCH 4/5] Improves ancestor layout with overlap prevention Enhances family tree positioning algorithm to prevent vertical overlaps by: - Introducing shared tracking of minimum y-coordinates for each x-position - Supporting separate tracking for upward and downward growth directions - Adding more detailed logging for positioning decisions - Preventing ancestor and sibling positions from overlapping Resolves potential visual conflicts in complex multi-generational family tree layouts --- src/canvas_generator.py | 92 ++++++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 14 deletions(-) diff --git a/src/canvas_generator.py b/src/canvas_generator.py index 8444edb..81ef3fb 100644 --- a/src/canvas_generator.py +++ b/src/canvas_generator.py @@ -196,6 +196,10 @@ class CanvasGenerator: positions[root_id] = (0, 0) processed = {root_id} + # Shared dict to track next available y position at each x coordinate + # This prevents overlaps when multiple family branches use the same x + shared_min_y_at_x = {} + # Determine direction based on gender # Male: grow upward (negative y), Female: grow downward (positive y) root_individual = tree_structure[root_id]['individual'] @@ -221,13 +225,13 @@ class CanvasGenerator: # Position spouse's siblings and their families self._position_spouse_siblings(spouse_id, 0, spouse_y, tree_structure, positions, processed, spouse_direction) # Position spouse's ancestors (parents, grandparents, etc.) to the RIGHT - self._layout_ancestors_right(spouse_id, tree_structure, positions, processed, spouse_direction) + self._layout_ancestors_right(spouse_id, tree_structure, positions, processed, spouse_direction, shared_min_y_at_x) # Layout descendants (children) to the LEFT self._layout_descendants_left(root_id, tree_structure, positions, processed) # Layout ancestors (parents) to the RIGHT - self._layout_ancestors_right(root_id, tree_structure, positions, processed, root_direction) + self._layout_ancestors_right(root_id, tree_structure, positions, processed, root_direction, shared_min_y_at_x) # Position any remaining unprocessed people unprocessed = set(tree_structure.keys()) - processed @@ -324,6 +328,8 @@ class CanvasGenerator: # Position spouse's siblings and their families self._position_spouse_siblings(spouse_id, child_x, spouse_y, tree_structure, positions, processed, spouse_direction) # Position spouse's ancestors (parents, grandparents, etc.) + # Note: we don't pass shared_min_y_at_x here because this is from _layout_descendants_left + # which doesn't have access to it. This could cause overlaps for deep trees. self._layout_ancestors_right(spouse_id, tree_structure, positions, processed, spouse_direction) # Recursively layout this child's descendants (further left) @@ -338,7 +344,8 @@ class CanvasGenerator: tree_structure: Dict[str, Dict], positions: Dict[str, Tuple[int, int]], processed: set, - direction: str = 'down' + direction: str = 'down', + min_y_at_x: Dict[int, float] = None ): """ Layout ancestors to the right of root person with vertical sibling stacking. @@ -348,12 +355,14 @@ class CanvasGenerator: Args: direction: 'up' to grow upward (negative y), 'down' to grow downward (positive y) + min_y_at_x: Shared dict tracking next available y at each x position """ if root_id not in tree_structure or root_id not in positions: return - # Track minimum available y-coordinate at each x-position to avoid overlaps - min_y_at_x = {} + # Initialize if not provided + if min_y_at_x is None: + min_y_at_x = {} # Process only this person's parents (not spouse's) # Spouse's parents will be processed in their own call with their own direction @@ -377,10 +386,12 @@ class CanvasGenerator: father_id, mother_id = parents[0], parents[1] # Check if we need to avoid overlap at this x-position - if parent_x in min_y_at_x: - # Position relative to existing people at this x-coordinate - parent_y = min_y_at_x[parent_x] - logger.info(f"Using min_y_at_x for x={parent_x}: y={parent_y}, direction={direction}") + # Use (x, direction) as key to track upward and downward growth separately + key = (parent_x, direction) + if key in min_y_at_x: + # Position relative to existing people at this x-coordinate going this direction + parent_y = min_y_at_x[key] + logger.info(f"Using min_y_at_x for x={parent_x}, dir={direction}: y={parent_y}") else: # Center parent couple on their child parent_y = person_y @@ -389,6 +400,8 @@ class CanvasGenerator: if father_id in tree_structure and father_id not in processed: positions[father_id] = (parent_x, parent_y) processed.add(father_id) + father_name = tree_structure[father_id]['individual'].get_names() + logger.info(f"Positioned father {father_name} at ({parent_x}, {parent_y})") mother_y = parent_y # Default if mother doesn't exist if mother_id in tree_structure and mother_id not in processed: @@ -396,9 +409,32 @@ class CanvasGenerator: mother_y = parent_y - self.IMAGE_HEIGHT - self.COUPLE_SPACING else: mother_y = parent_y + self.IMAGE_HEIGHT + self.COUPLE_SPACING + + # Check if we need to avoid overlap at this position + key = (parent_x, direction) + if key in min_y_at_x: + if direction == 'up': + # For upward growth, ensure mother is above the min_y + if mother_y > min_y_at_x[key]: + mother_y = min_y_at_x[key] - self.IMAGE_HEIGHT - self.COUPLE_SPACING + logger.info(f"Adjusted mother position to avoid overlap: y={mother_y}") + else: + # For downward growth, ensure mother is below the min_y + if mother_y < min_y_at_x[key]: + mother_y = min_y_at_x[key] + self.IMAGE_HEIGHT + self.COUPLE_SPACING + logger.info(f"Adjusted mother position to avoid overlap: y={mother_y}") + positions[mother_id] = (parent_x, mother_y) processed.add(mother_id) + # Update min_y_at_x to include the mother's position + if direction == 'up': + min_y_at_x[key] = mother_y - self.SIBLING_SPACING - self.IMAGE_HEIGHT + logger.info(f"Updated min_y_at_x[{key}] = {min_y_at_x[key]} after positioning mother") + else: + min_y_at_x[key] = mother_y + self.IMAGE_HEIGHT + self.SIBLING_SPACING + logger.info(f"Updated min_y_at_x[{key}] = {min_y_at_x[key]} after positioning mother") + # Position siblings of both parents at same x-position, stacked vertically if direction == 'up': current_sibling_y = min(parent_y, mother_y) - self.SIBLING_SPACING - self.IMAGE_HEIGHT @@ -411,6 +447,8 @@ class CanvasGenerator: if sibling_id in tree_structure and sibling_id not in processed: positions[sibling_id] = (parent_x, current_sibling_y) processed.add(sibling_id) + sibling_name = tree_structure[sibling_id]['individual'].get_names() + logger.info(f"Positioned father sibling {sibling_name} at ({parent_x}, {current_sibling_y})") # Position sibling's spouse sibling_data = tree_structure[sibling_id] @@ -484,21 +522,47 @@ class CanvasGenerator: self._layout_descendants_left(sibling_id, tree_structure, positions, processed) # Update the minimum y for this x-position for next iteration - min_y_at_x[parent_x] = current_sibling_y + key = (parent_x, direction) + min_y_at_x[key] = current_sibling_y # Recursively position their ancestors (further right) # Process both parents' ancestors (maintain same direction) if father_id in tree_structure: - self._layout_ancestors_right(father_id, tree_structure, positions, processed, direction) + self._layout_ancestors_right(father_id, tree_structure, positions, processed, direction, min_y_at_x) if mother_id in tree_structure: - self._layout_ancestors_right(mother_id, tree_structure, positions, processed, direction) + self._layout_ancestors_right(mother_id, tree_structure, positions, processed, direction, min_y_at_x) elif len(parents) == 1: parent_id = parents[0] if parent_id in tree_structure and parent_id not in processed: - positions[parent_id] = (parent_x, person_y) + parent_name = tree_structure[parent_id]['individual'].get_names() + + # Check if we need to avoid overlap at this position + key = (parent_x, direction) + parent_y = person_y + if key in min_y_at_x: + if direction == 'up': + # For upward growth, ensure parent is above the min_y + if parent_y > min_y_at_x[key]: + parent_y = min_y_at_x[key] - self.IMAGE_HEIGHT - self.SIBLING_SPACING + logger.info(f"Adjusted single parent position to avoid overlap: y={parent_y}") + else: + # For downward growth, ensure parent is below the min_y + if parent_y < min_y_at_x[key]: + parent_y = min_y_at_x[key] + self.IMAGE_HEIGHT + self.SIBLING_SPACING + logger.info(f"Adjusted single parent position to avoid overlap: y={parent_y}") + + logger.info(f"Single parent case: positioning {parent_name} at ({parent_x}, {parent_y})") + positions[parent_id] = (parent_x, parent_y) processed.add(parent_id) - self._layout_ancestors_right(parent_id, tree_structure, positions, processed, direction) + + # Update min_y_at_x to include this parent's position + if direction == 'up': + min_y_at_x[key] = parent_y - self.IMAGE_HEIGHT - self.SIBLING_SPACING + else: + min_y_at_x[key] = parent_y + self.IMAGE_HEIGHT + self.SIBLING_SPACING + + self._layout_ancestors_right(parent_id, tree_structure, positions, processed, direction, min_y_at_x) def _calculate_family_height( self, -- 2.36.6 From e0ea4a3cc4ae68d355cda3f4c06f371062084860 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Sun, 2 Nov 2025 14:29:22 +0100 Subject: [PATCH 5/5] Adds Obsidian canvas generation with root person selection Enhances GEDCOM to Markdown conversion by introducing canvas generation feature Implements flexible root person selection for canvas via: - Command-line argument (`--root`) - Numeric index selection - Direct GEDCOM ID input - Interactive fallback selection Improves logging configuration to provide more granular control over output verbosity --- README.md | 17 ++++++++++++++ src/main.py | 45 ++++++++++++++++++++++++++++--------- src/person_selector.py | 51 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 75a9513..5344364 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,8 @@ python src/main.py -i family.zip -o output --flat - `-o`, `--output DIR`: Output directory for generated notes (required) - `--flat`: Use flat structure (all files in output root). Default creates subdirectories - `--no-index`: Skip creating the index file +- `--canvas`: Create an Obsidian canvas file for family tree visualization +- `--root ID`: Root person for canvas. Can be a selection number (e.g., `85`) or GEDCOM ID (e.g., `@I253884714@` or `I253884714`). If not provided with `--canvas`, will prompt interactively - `--verbose` or `-v`: Enable detailed logging ### Examples @@ -99,6 +101,21 @@ python src/main.py -i examples/family.zip -o examples/output --verbose python src/main.py -i examples/family.ged -o examples/output --flat --verbose ``` +**Generate family tree canvas with specific root person (by selection number):** +```bash +python src/main.py -i family.ged -o output --canvas --root 85 +``` + +**Generate canvas with specific root person (by GEDCOM ID):** +```bash +python src/main.py -i family.ged -o output --canvas --root @I253884714@ +``` + +**Generate canvas with interactive root person selection:** +```bash +python src/main.py -i family.ged -o output --canvas +``` + **Using long form arguments:** ```bash python src/main.py --input examples/family.zip --output examples/output diff --git a/src/main.py b/src/main.py index 74761c1..ee4a5a7 100644 --- a/src/main.py +++ b/src/main.py @@ -27,20 +27,32 @@ def setup_logging(verbose: bool = False): """ Configure root logger formatting and level for the application. - Sets the logging level to DEBUG when `verbose` is True, otherwise to INFO. + Sets the logging level to DEBUG when `verbose` is True, otherwise to INFO + for the main module only and WARNING for other modules. Also applies a consistent message format and timestamp date format used across the application. Parameters: - verbose (bool): When True, enable DEBUG-level logging; otherwise - use INFO-level logging. + verbose (bool): When True, enable DEBUG-level logging for all modules; + otherwise use INFO-level logging for main module only. """ - level = logging.DEBUG if verbose else logging.INFO - logging.basicConfig( - level=level, - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - ) + if verbose: + # Show all logs from all modules + level = logging.DEBUG + logging.basicConfig( + level=level, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + ) + else: + # Only show INFO from main module, WARNING+ from others + logging.basicConfig( + level=logging.WARNING, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + ) + # Enable INFO logging for main module only + logging.getLogger(__name__).setLevel(logging.INFO) def extract_gedzip(zip_path: Path, temp_dir: Path) -> Tuple[Path, Optional[Path]]: @@ -103,6 +115,7 @@ def convert_gedcom_to_markdown( media_dir: Optional[Path] = None, use_flat_structure: bool = False, create_canvas: bool = False, + root_id: Optional[str] = None, ) -> int: """ Convert a GEDCOM file into Obsidian-compatible Markdown notes organized on disk. @@ -120,6 +133,10 @@ def convert_gedcom_to_markdown( `media/`, `stories/`). create_canvas (bool): Whether to generate an Obsidian canvas file for family tree visualization. + root_id (Optional[str]): Root person identifier for canvas generation. + Can be a selection number (e.g., '85') or GEDCOM ID (e.g., + '@I253884714@' or 'I253884714'). If not provided and create_canvas + is True, will prompt interactively. Returns: int: 0 on success, 1 on failure. @@ -164,7 +181,7 @@ def convert_gedcom_to_markdown( # Generate canvas if requested if create_canvas: logger.info("Canvas generation requested") - root_person_id = select_root_person(individuals) + root_person_id = select_root_person(individuals, root_id) if root_person_id: logger.info(f"Generating canvas with root person: {root_person_id}") @@ -282,6 +299,13 @@ def main(): help="Create an Obsidian canvas file for family tree visualization", ) + parser.add_argument( + "--root", + type=str, + metavar="ID", + help="Root person for canvas. Can be a selection number (e.g., 85) or GEDCOM ID (e.g., @I253884714@). If not provided, will prompt interactively.", + ) + parser.add_argument( "-v", "--verbose", action="store_true", help="Enable verbose logging" ) @@ -320,6 +344,7 @@ def main(): media_dir=media_dir, use_flat_structure=args.flat, create_canvas=args.canvas, + root_id=args.root, ) return exit_code diff --git a/src/person_selector.py b/src/person_selector.py index 03a2064..a82d862 100644 --- a/src/person_selector.py +++ b/src/person_selector.py @@ -14,12 +14,16 @@ from individual import Individual logger = logging.getLogger(__name__) -def select_root_person(individuals: List[Individual]) -> Optional[str]: +def select_root_person(individuals: List[Individual], root_id: Optional[str] = None) -> Optional[str]: """ Display an interactive list of individuals and let user select root person. Args: individuals: List of Individual objects from GEDCOM file + root_id: Optional identifier for root person. Can be either: + - A numeric index from the selection list (e.g., '85') + - A GEDCOM ID (e.g., '@I253884714@' or 'I253884714') + If provided, validates and returns it. If not provided, prompts interactively. Returns: The GEDCOM pointer/ID of the selected individual, or None if cancelled @@ -28,6 +32,51 @@ def select_root_person(individuals: List[Individual]) -> Optional[str]: logger.error("No individuals provided for selection") return None + # If root_id is provided, validate it and return it + if root_id: + # Check if it's a numeric index (e.g., "85") + if root_id.isdigit(): + index = int(root_id) + # Sort individuals the same way as interactive selection + sorted_individuals = sorted( + individuals, + key=lambda ind: ( + ind.get_names()[1] or "", # Last name + ind.get_names()[0] or "" # First name + ) + ) + + if 1 <= index <= len(sorted_individuals): + selected = sorted_individuals[index - 1] + first_name, last_name = selected.get_names() + full_name = f"{last_name or ''} {first_name or ''}".strip() + logger.info(f"Using root person by index #{index}: {full_name} ({selected.get_pointer()})") + print(f"\nUsing root person: {full_name} ({selected.get_pointer()})") + return selected.get_pointer() + else: + logger.error(f"Root person index '{root_id}' out of range (1-{len(sorted_individuals)})") + print(f"\nError: Root person index '{root_id}' out of range (1-{len(sorted_individuals)})") + print("Falling back to interactive selection...\n") + # Fall through to interactive selection + else: + # Treat as GEDCOM ID - normalize by adding @ symbols if not present + normalized_id = root_id if root_id.startswith('@') else f'@{root_id}@' + + # Find the individual with this ID + for individual in individuals: + if individual.get_pointer() == normalized_id: + first_name, last_name = individual.get_names() + full_name = f"{last_name or ''} {first_name or ''}".strip() + logger.info(f"Using provided root person: {full_name} ({normalized_id})") + print(f"\nUsing root person: {full_name} ({normalized_id})") + return normalized_id + + # ID not found + logger.error(f"Root person ID '{root_id}' not found in GEDCOM file") + print(f"\nError: Root person ID '{root_id}' not found in GEDCOM file") + print("Falling back to interactive selection...\n") + # Fall through to interactive selection + # Sort individuals by last name, then first name sorted_individuals = sorted( individuals, -- 2.36.6