# Project Specification: Territory Address Combiner This document outlines the plan for developing a script to assign Territory IDs to addresses based on their geographic coordinates. ## 1. Project Setup & File I/O [x] 1.1. Read the `TerritoryExport.csv` file. [x] 1.2. Parse the CSV data into a list of objects, where each object represents a territory and contains its ID and boundary points. [x] 1.3. Read the `Addresses.csv` file. [x] 1.4. Parse the CSV data into a list of objects, where each object represents an address and its properties, including latitude and longitude. [x] ## 2. Data Processing and Structuring [x] 2.1. For each territory, parse the `Boundary` string into a numerical list of coordinate pairs. Each pair will represent a vertex of the polygon. [x] 2.2. For each address, ensure its `Latitude` and `Longitude` are stored as numerical data types. [x] ## 3. Core Logic: Point-in-Polygon (PIP) Implementation [x] 3.1. Create a function that implements the Ray Casting algorithm to determine if a point is inside a polygon. [x] 3.2. This function will accept two arguments: the coordinates of the address (the point) and the list of vertices for a territory boundary (the polygon). [x] 3.3. The function will return `true` if the point is inside the polygon and `false` otherwise. [x] ## 4. Territory Assignment [x] 4.1. Iterate through each address in the parsed list from `Addresses.csv`. [x] 4.2. For each address, iterate through each territory from the parsed list from `TerritoryExport.csv`. [x] 4.3. Use the PIP function (from step 3) to check if the address's coordinate is inside the current territory's boundary. [x] 4.4. If the PIP function returns `true`: 4.4.1. Assign the territory's `TerritoryID` to the `TerritoryID` field of the address object. [x] 4.4.2. Break the inner loop (territory iteration) and proceed to the next address. [x] ## 5. Output Generation [x] 5.1. Create a new file named `Addresses_Updated.csv`. [x] 5.2. Write the header row from the original `Addresses.csv` to the new file. [x] 5.3. Iterate through the updated list of address objects. [x] 5.4. For each address object, write a new row to `Addresses_Updated.csv` with all the original data plus the newly assigned `TerritoryID`. [x] ## 6. Finalization [x] 6.1. Close any open file streams. [x] 6.2. Report successful completion to the user. [x]