From 32b53984fc9d91b16c041a4205d2938da2190c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Cede=C3=B1o=20Monta=C3=B1a?= Date: Sat, 29 May 2021 00:21:35 +0000 Subject: [PATCH] Update mapa_col.pde --- mapa_col.pde | 313 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 mapa_col.pde diff --git a/mapa_col.pde b/mapa_col.pde new file mode 100644 index 0000000..7440946 --- /dev/null +++ b/mapa_col.pde @@ -0,0 +1,313 @@ +/** + * Load and Display a *.svg shape. + * Map of Colombia by Ricardo Cedeño Montaña + * + * + Ricardo Cedeño Montaña + Universidad de Antioquia + Facultad de Comunicaciones + CC-SA-BY 2019 + + update 27.3.2019 + uptade 26.5.2021 +*/ + + +/* +1. create a shape object for the entire map, + one for departamento and one for municipios +2. create a shape object for a municipio +*/ + +import processing.svg.*;//this library exports to svg + +PShape Colombia; +PShape Departamentos, Departamento, Municipios, Municipio; +PShape [] shMun; + +// text files to store the name departments and +// their municipios +PrintWriter output, output1; + +/* +1. create an xml variable to read the svg map as an xml file + in order to extract all labels and ids. +2. create lists of departamentos, municipios and their ids + extracted from the svg as xml +*/ + +XML xmlCol; +String [] listDptos, listMun; +String [] listDepLabel, listDepId, listDepMunLabel, + listMunLabel, listMunId, listDepMunId, + listStyle, listClass, capitals, pathNodes; +int [] svgX, svgY, svgM, svgN; + + +//position, width, height, scale of the map, and scale factor for departamentos +int xPos = 0, yPos=0, svgW, svgH; +float mapScale = 1; +int scaleFac = 3; + +void setup() { + size(2948, 4000); + background(255); + output = createWriter("departamentos"); + output1 = createWriter("municipios"); + + // The SVG file has to exist in the data directory + // for the sketch to load it properly + + //Read the SVG as shape + Colombia = loadShape("mapa_col.svg"); + svgW = int(Colombia.getWidth()); + svgH = int(Colombia.getHeight()); + + //read the SVG as an XML file + //extract the name of all municipios and the departmentos + //they belong to and organize the names in arrays + //Print the values into separate text files + + xmlCol = loadXML("mapa_col.svg"); + + XML[] xmlDep = xmlCol.getChildren("g/path"); //reads the node called Departamentos + listDepId = new String[xmlDep.length]; + + for (int i = 0; i < xmlDep.length; i++) { + listDepId[i] = xmlDep[i].getString("id"); + output.println(listDepId[i]);//write departamentos + output.flush(); // write the file + } + output.close(); // close the file + + XML[] xmlMun = xmlCol.getChildren("g/g/path");//read the node called Municipios + + listMunLabel = new String[xmlMun.length]; + listMunId = new String[xmlMun.length]; + listDepMunLabel = new String[xmlMun.length]; + listClass = new String[xmlMun.length]; + capitals = new String[xmlDep.length-1];//-1 bc bogota doesn't have a capital + listDepMunId = new String[xmlDep.length-1];//-1 bc bogota doesn't have a capital + svgX = new int[xmlDep.length-1];//-1 bc bogota doesn't have a capital + svgY = new int[xmlDep.length-1];//-1 bc bogota doesn't have a capital + svgM = new int[xmlDep.length-1];//-1 bc bogota doesn't have a capital + svgN = new int[xmlDep.length-1];//-1 bc bogota doesn't have a capital + //println(listMunId.length); + + + int add = 0;//counter + for (int i = 0; i < xmlMun.length; i++) { + + listDepMunLabel[i] = xmlMun[i].getParent().getString("inkscape:label"); + + listMunLabel[i] = xmlMun[i].getString("inkscape:label"); + listClass[i] = xmlMun[i].getString("class"); + + listMunId[i] = listMunLabel[i]+" " + +listClass[i]+" " + +listDepMunLabel[i]; + + if(listClass[i].contains("capital")){// extract the path id for each capital + capitals[add] = xmlMun[i].getString("id"); + listDepMunId[add] = xmlMun[i].getParent().getString("id"); //<>// + //extract x and y positions of each group and map them to the sketch size. + svgX[add] = int(xmlMun[i].getParent().getString("xPos")); + svgY[add] = int(xmlMun[i].getParent().getString("yPos")); + svgM[add] = int(map(svgX[add], 0, svgW, 0, width))*scaleFac; + svgN[add] = int(map(svgY[add], 0, svgH, 0, height))*scaleFac; + + add++; + } + + output1.println(listMunLabel[i]);//write only the name of municipios + output1.flush(); + } + + saveStrings("municipios detalle", listMunId);//write details of each municipio + saveStrings("municipios id", capitals);//id path + output1.close(); + + + /** functions to draw either deps or muns + * comment and uncomment to draw what you need + * Default option is municipios + */ + + //drawDepartamentos(); + drawMunicipios(); + //drawCapitals(capitals); + //drawOneDpto(i, svgM[i], svgN[i], scaleFac); + //drawComunas(); + + //for(int i=0;i<32;i++){ + //beginRecord(SVG, "col_dpto_"+i+".svg"); + /*export each departmento to svg + you lose all attributes of the original map e.g. names of municipios. + */ + //drawOneDpto(i, svgM[i], svgN[i], scaleFac); + //} + +} + +/* =========== Functions =========== */ + +void drawDepartamentos(){ + // go through all departamentos node from the SVG + // Draw nodes on the sketch + Departamentos = Colombia.getChild("Departamentos"); + + listDptos = new String[Departamentos.getChildren().length]; + for (int i=0; i