# HG changeset patch # User Andrew # Date 1661635301 14400 # Sat Aug 27 17:21:41 2022 -0400 # Node ID adc768d8e44398ca4090ffe0c665d9f4a3c4721b # Parent e282f74cff644cca2e40b3d3a400bc7adfca0741 Generate the 1910 XML file. This involved a bit of re-arranging so we try to create population maps up front, and then create the XML cities just prior to exporting to XML. It does mean there's some re-processing of the "source" EE1 1900 XML file to re-import things, and those cities don't get the nice progression that newer ones do. This also highlights our lack of interpolation in intermediary years. Still, it is a step forward. diff --git a/src/main/java/DBObjects/CityDB.java b/src/main/java/DBObjects/CityDB.java --- a/src/main/java/DBObjects/CityDB.java +++ b/src/main/java/DBObjects/CityDB.java @@ -69,6 +69,8 @@ System.out.println(name + " has " + percentFormat.format(pop * 100) + "% of the people in " + region.getName() + " in " + year); } + //TODO: Interpolation, exterpolation + //E.g. England has 1891, 1911, it will use 1891 for a long time... public Integer getBaseUrbanPopulation(int year) { if (populationByYear.containsKey(year)) { return populationByYear.get(year); diff --git a/src/main/java/com/ajtjp/gearCity/DBReader.java b/src/main/java/com/ajtjp/gearCity/DBReader.java --- a/src/main/java/com/ajtjp/gearCity/DBReader.java +++ b/src/main/java/com/ajtjp/gearCity/DBReader.java @@ -121,7 +121,7 @@ List cities = new ArrayList<>(); try { PreparedStatement stmt = conn.prepareStatement( - "SELECT NAME, Region, Country, Longitude, Latitude, XML_ID, Picture from CityInfo" + "SELECT NAME, Region, Country, Longitude, Latitude, XML_ID, Picture from CityInfo Where Disabled is NULL" ); ResultSet rs = stmt.executeQuery(); while (rs.next()) { diff --git a/src/main/java/com/ajtjp/gearCity/Main.java b/src/main/java/com/ajtjp/gearCity/Main.java --- a/src/main/java/com/ajtjp/gearCity/Main.java +++ b/src/main/java/com/ajtjp/gearCity/Main.java @@ -42,9 +42,15 @@ * @author Andrew */ public class Main { + + //This will eventually go obsolete once we load everything from our DB + //Prior to that, it might decrease when we load EE1 cities from the DB + static int nextXMLID = 125; + public static void main(String[] args) throws Exception { // testXMLExport(); + Set citiesAlreadyExisting = new HashSet<>(); CreateCityDB cityDB = null; @@ -85,6 +91,67 @@ AddRegionalCities(region, citiesAlreadyExisting, cityDB); } + updateExistingXMLCities(citiesAlreadyExisting, cityDB); + + int[] years = { 1900, 1910 }; + for (int year : years) { + //Re-import so we don't add cities to the XML more than once + cityDB = ImportCitiesFromXMLFile(cityDB, citiesAlreadyExisting); + updateExistingXMLCities(citiesAlreadyExisting, cityDB); + +cityDBLoop: for (CityDB city : cities) { + //See if it's already in the XML, if so skip + for (City xml : cityDB.getCityList()) { + if (xml.getNAME().getName().equals(city.getName())) { + continue cityDBLoop; + } + } + System.out.println("Creating " + city + " in year " + year); + System.out.println(" The population is " + city.getAdjustedRegionalBasedPopulation(year)); + City xmlCity = new City(city, year, year + 10); + cityDB.getCityList().add(xmlCity); + } + + //Export back to file + Document document= BeanToXMLUtil.convertBeanToXML(cityDB); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(document); + + ExportToCityFile("D:\\Gear City Mods\\Europe Expanded II\\Maps\\Europe Expanded II\\scripts\\City" + year + ".xml", transformer, source); + ExportToCityFile("D:\\Gear City Mods\\Europe Expanded II\\Maps\\Europe Expanded II\\scripts\\City2020.xml", transformer, source); + } + + //Copy things over + Process proc = Runtime.getRuntime().exec("D:\\Gear City Mods\\Europe Expanded II\\desktopCopy.bat", null, new File("D:/Gear City Mods/Europe Expanded II/")); + int result = proc.waitFor(); + InputStream es = proc.getErrorStream(); + System.out.println("Result of copy process = " + result); + if (result != 0) { + System.out.println("Errors encountered while copying:"); + int count = 0; + for (;;) { + count = es.available(); + if (count == 0) { + break; + } + byte[] b = new byte[count]; + es.read(b); + String string = new String(b); + System.out.println(string); + } + } + } + + /** + * Updates: + * - Picture + * - Region + * N.B. Long-term I would like to just load everything from our DB always. + * @param citiesAlreadyExisting + * @param cityDB + */ + static void updateExistingXMLCities(Set citiesAlreadyExisting, CreateCityDB cityDB) { //Update existing cities for (String cityName : citiesAlreadyExisting) { if (CityDB.cities.containsKey(cityName)) { @@ -124,56 +191,32 @@ } } } - - //Export back to file - Document document= BeanToXMLUtil.convertBeanToXML(cityDB); - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - Transformer transformer = transformerFactory.newTransformer(); - DOMSource source = new DOMSource(document); - - ExportToCityFile("D:\\Gear City Mods\\Europe Expanded II\\Maps\\Europe Expanded II\\scripts\\City1900.xml", transformer, source); - ExportToCityFile("D:\\Gear City Mods\\Europe Expanded II\\Maps\\Europe Expanded II\\scripts\\City2020.xml", transformer, source); - - //Copy things over - Process proc = Runtime.getRuntime().exec("D:\\Gear City Mods\\Europe Expanded II\\desktopCopy.bat", null, new File("D:/Gear City Mods/Europe Expanded II/")); - int result = proc.waitFor(); - InputStream es = proc.getErrorStream(); - System.out.println("Result of copy process = " + result); - if (result != 0) { - System.out.println("Errors encountered while copying:"); - int count = 0; - for (;;) { - count = es.available(); - if (count == 0) { - break; - } - byte[] b = new byte[count]; - es.read(b); - String string = new String(b); - System.out.println(string); - } - } } static void AddRegionalCities(RegionDB region, Set citiesAlreadyExisting, CreateCityDB cityDB) { if (!region.getCountry().getName().isEmpty()) { //eventually, we'll have an optional filter for e.g. a France-only game List regionData = RegionalDemographicsDB.demographicsByRegion.get(region.getName()); List citiesInRegion = region.getCitiesInRegion(); - int totalPop = 0; - for (CityDB city : citiesInRegion) { - totalPop += city.getBaseUrbanPopulation(1900); - } - for (CityDB city : citiesInRegion) { - if (citiesAlreadyExisting.contains(city.getName())) { - //TODO: Update pop anyway, but don't duplicate - continue; + int[] years = { 1900, 1910 }; + for (int year : years ) { + int totalPop = 0; + for (CityDB city : citiesInRegion) { + totalPop += city.getBaseUrbanPopulation(year); } - int pop = city.getBaseUrbanPopulation(1900); - double percent = (pop + 0.0) / totalPop; - city.setPopPercentByYear(1900, percent); - city.setXMLID(cityDB.getCityList().size() + 1); //todo: less hacky - City xmlCity = new City(city, 1900, 1910); - cityDB.getCityList().add(xmlCity); + for (CityDB city : citiesInRegion) { + if (citiesAlreadyExisting.contains(city.getName())) { + //TODO: Update pop anyway, but don't duplicate + continue; + } + int pop = city.getBaseUrbanPopulation(year); + double percent = (pop + 0.0) / totalPop; + city.setPopPercentByYear(year, percent); + //TODO: Less hacky. This shouldn't really be in a loop + if (city.getXmlID() == 0) { + city.setXMLID(Main.nextXMLID); //todo: less hacky + Main.nextXMLID++; + } + } } } } @@ -187,7 +230,10 @@ for (UrbanPopulationDB demographics : urbanPopulation) { CityDB city = CityDB.cities.get(demographics.getName()); - city.setPopulation(demographics.getYear(), demographics.getPopulation()); + if (city != null) { + //Todo: Don't load demographics for disabled cities. + city.setPopulation(demographics.getYear(), demographics.getPopulation()); + } } }