-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-dotted-map.php
96 lines (66 loc) · 2.09 KB
/
svg-dotted-map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
error_reporting(E_ALL);
class DottedMap {
public $created_countries;
//
// Patterns generated by AI
//
private $patterns;
//
// How we want to replace them.
//
private $replace;
public function __construct() {
$this->patterns = array('/_[0-9]_/', '/_x2c_/', '/_x27_/');
$this->replace = array( '', '', '_' );
$this->created_countries = "";
}
private function _parse_id( $id ) {
return preg_replace( $this->patterns, $this->replace, strtolower( $id ) );
}
private function _append_country ( $id ) {
$this->created_countries .= $id . "\n";
}
public function parse_xml($xml, $node){
$paths = array();
$group = array();
$count = 0;
$group_id = $count;
$path;
$countryPoints = array();
foreach( $xml->children() as $region )
{
if( ( $region->getName() == "g" ) && ( $region->count() > 0 ) )
{
foreach( $region->children() as $country )
{
if( $country->getName() == "g" && ( $country->count() > 0 ) )
{
//
// Clenup the id for our json
//
$country_id = $this->_parse_id( $country[ "id" ] );
//
// Append the generated country
//
$this->_append_country( $country_id );
if( $country->path )
$paths[ $country_id ][ "path" ] = array( "d" => $country->path["d"] );
}
}//end foreach $region
}
}
return json_encode( $paths );
}
}
$dm = new DottedMap();
$xml = simplexml_load_file( "dotted-map.svg" );
$assoc = $dm->parse_xml( $xml, "root" );
$jsonData = "var country_paths = {$assoc};";
$pathData = fopen( "map-path-data.js", "w" );
fwrite( $pathData, $jsonData );
fclose( $pathData );
$countrData = fopen( "countries.txt", "w" );
fwrite( $countrData, $dm->created_countries );
fclose( $countryData );
?>