@@ 69,6 69,8 @@ public class CityDB {
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);
@@ 121,7 121,7 @@ public class DBReader {
List<CityDB> 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()) {
@@ 42,9 42,15 @@ import org.w3c.dom.Document;
* @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<String> citiesAlreadyExisting = new HashSet<>();
CreateCityDB cityDB = null;
@@ 85,6 91,67 @@ public class Main {
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<String> citiesAlreadyExisting, CreateCityDB cityDB) {
//Update existing cities
for (String cityName : citiesAlreadyExisting) {
if (CityDB.cities.containsKey(cityName)) {
@@ 124,56 191,32 @@ public class Main {
}
}
}
-
- //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<String> citiesAlreadyExisting, CreateCityDB cityDB) {
if (!region.getCountry().getName().isEmpty()) { //eventually, we'll have an optional filter for e.g. a France-only game
List<RegionalDemographicsDB> regionData = RegionalDemographicsDB.demographicsByRegion.get(region.getName());
List<CityDB> 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 @@ public class Main {
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());
+ }
}
}