├── .gitignore ├── config.php ├── ReadFirst.txt ├── README.md ├── index.php └── tables.sql /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /download -------------------------------------------------------------------------------- /config.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 22 | $db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8"); 23 | return $db; 24 | }catch(PDOException $ex) 25 | { 26 | die(json_encode(array('outcome' => false, 'message' => 'Database connection failed'))); 27 | } 28 | } 29 | 30 | ?> 31 | -------------------------------------------------------------------------------- /ReadFirst.txt: -------------------------------------------------------------------------------- 1 | Description 2 | 3 | This scripts downloads the below files to the download folder:- 4 | 5 | 1.http://download.geonames.org/export/dump/allCountries.zip 6 | 2.http://download.geonames.org/export/dump/alternateNames.zip 7 | 3.http://download.geonames.org/export/dump/admin1CodesASCII.txt 8 | 4.http://download.geonames.org/export/dump/timeZones.txt 9 | 5.http://download.geonames.org/export/dump/featureCodes_en.txt 10 | 6.http://download.geonames.org/export/dump/iso-languagecodes.txt 11 | 7.http://download.geonames.org/export/dump/countryInfo.txt 12 | 13 | After downloading the files the 2 zipped files are allCountries.zip and alternateNames.zip are UNZIPPED by the script 14 | 15 | Then correspoding database tables are created for the above downloaded files to import 16 | Filename => database tables name 17 | 1.allCountries.txt ==> geo_name 18 | 2.alternateNames.txt ==> geo_alternateNames 19 | 3.admin1CodesASCII.txt ==> geo_admin1 20 | 4.timeZones.txt ==> geo_timeZones 21 | 5.featureCodes_en.txt ==> geo_featureCodes 22 | 6.iso-languagecodes.txt ==> geo_languagecodes 23 | 7.countryInfo.txt ==> geo_country 24 | 25 | Then the all the information are imported to their corresponding table 26 | 27 | Note:Please create the database first and then write the database name in the config.php file. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This script downloads the following files to the download folder: 2 | 3 | 1. http://download.geonames.org/export/dump/allCountries.zip 4 | 2. http://download.geonames.org/export/dump/alternateNames.zip 5 | 3. http://download.geonames.org/export/dump/admin1CodesASCII.txt 6 | 4. http://download.geonames.org/export/dump/timeZones.txt 7 | 5. http://download.geonames.org/export/dump/featureCodes_en.txt 8 | 6. http://download.geonames.org/export/dump/iso-languagecodes.txt 9 | 7. http://download.geonames.org/export/dump/countryInfo.txt 10 | 11 | After downloading the files, the 2 zipped files _allCountries.zip_ and _alternateNames.zip_ are unzipped by the script itself. Then correspoding database tables are created for the above downloaded files to import. 12 | 13 | | Filename | Database Tables Name | 14 | |-----------------------|--------------------------| 15 | | allCountries.txt | geo_name | 16 | | alternateNames.txt | geo_alternateNames | 17 | | admin1CodesASCII.txt | geo_admin1 | 18 | | timeZones.txt | geo_timeZones | 19 | | featureCodes\_en.txt | geo_featureCodes | 20 | | iso-languagecodes.txt | geo_languagecodes | 21 | | countryInfo.txt | geo_country | 22 | 23 | Then all the information is imported from files to their corresponding tables. 24 | 25 | Note: Please create the database first and then the database name in the config.php file 26 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | $file already exists in the download Folder
"; 27 | }else{ 28 | 29 | $ch = curl_init(); 30 | $source = "http://download.geonames.org/export/dump/$file "; 31 | curl_setopt($ch, CURLOPT_URL, $source); 32 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 33 | $data = curl_exec ($ch); 34 | curl_close ($ch); 35 | $file = fopen($destination, "w+"); 36 | fputs($file, $data); 37 | fclose($file); 38 | } 39 | 40 | } 41 | 42 | #**********************END OF CODE FOR DOWNLOADING*************************** 43 | 44 | 45 | 46 | 47 | #--------------CODE FOR UNZIPPING THE 2 ZIPPED FILES----------------------- 48 | $zip = new ZipArchive; 49 | $filename=array('allCountries.zip','alternateNames.zip'); 50 | foreach($filename as $file){ 51 | $file1 = preg_replace('"\.zip$"', '.txt', $file); 52 | if (file_exists(DPATH.$file1)) { 53 | echo "The file $file1 already exists in the download Folder
"; 54 | }else{ 55 | $res = $zip->open(DPATH.$file); 56 | if ($res === TRUE) { 57 | $zip->extractTo($path); 58 | $zip->close(); 59 | echo "$file UnZipped!
"; 60 | }else { 61 | echo "$file UnZipped! Failed
"; 62 | } 63 | } 64 | } 65 | #*********************END OF CODE FOR UNZIPPING THE 2 ZIPPED FILES********************* 66 | 67 | 68 | 69 | 70 | 71 | #---------------------CODE FOR TABLES CREATION AND DATABASE INSERTION---------------------- 72 | #Database base connection string 73 | 74 | #Code for truncating the already inserted database the tables 75 | $query = "SHOW TABLES"; 76 | $statement = $db->prepare($query); 77 | $statement->execute(); 78 | 79 | $data = $statement->fetchAll(PDO::FETCH_COLUMN); 80 | 81 | foreach($data as $table){ 82 | $query ="TRUNCATE ".$table; 83 | $statement1 = $db->prepare($query); 84 | $statement1->execute(); 85 | }; 86 | 87 | #Code for the creation of tables 88 | $query = file_get_contents('tables.sql'); 89 | 90 | $statement = $db->prepare($query); 91 | $statement->execute(); 92 | 93 | 94 | #Array of filename and the table name 95 | $tablenames = array( 96 | #txt name,table name 97 | array('allCountries.txt','geo_name'), 98 | array('alternateNames.txt','geo_alternateName'), 99 | array('admin1CodesASCII.txt','geo_admin1'), 100 | array('timeZones.txt','geo_timeZones'), 101 | array('featureCodes_en.txt','geo_featureCodes'), 102 | array('iso-languagecodes.txt','geo_languagecodes'), 103 | array('countryInfo.txt','geo_country') 104 | 105 | ); 106 | 107 | foreach($tablenames as $file){ 108 | 109 | $query = "load data infile '".DPATH.$file['0']."' IGNORE INTO TABLE ".$file['1']." CHARACTER SET UTF8;"; 110 | $statement = $db->prepare($query); 111 | $statement->execute(); 112 | } 113 | 114 | #Delete empty rows from countryInfo 115 | $query=" 116 | DELETE FROM geo_country where iso_alpha2 LIKE '#%'; 117 | DELETE FROM geo_languagecodes where iso_639_3='ISO'; 118 | DELETE FROM geo_timezones where countrycode='CountryCode'; 119 | "; 120 | $statement = $db->prepare($query); 121 | $statement->execute(); 122 | #******************END OF CODE FOR TABLES CREATION AND DATABASE INSERTION*************************** 123 | 124 | ?> 125 | -------------------------------------------------------------------------------- /tables.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE TABLE IF NOT EXISTS`geo_admin1` ( 3 | `code` VARCHAR(12) NOT NULL DEFAULT '', 4 | `name` TEXT NULL, 5 | `nameAscii` TEXT NULL, 6 | `geonameid` INT(11) NULL DEFAULT NULL, 7 | PRIMARY KEY (`code`) 8 | ) 9 | COLLATE='utf8_general_ci' 10 | ENGINE=InnoDB; 11 | 12 | 13 | CREATE TABLE IF NOT EXISTS `errors` ( 14 | `id` int(10) NOT NULL AUTO_INCREMENT, 15 | `error` varchar(400) NOT NULL DEFAULT '0', 16 | PRIMARY KEY (`id`) 17 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 18 | 19 | 20 | CREATE TABLE IF NOT EXISTS `geo_alternatename` ( 21 | `alternatenameId` int(11) NOT NULL, 22 | `geonameid` int(11) DEFAULT NULL, 23 | `isoLanguage` varchar(7) DEFAULT NULL, 24 | `alternateName` varchar(200) DEFAULT NULL, 25 | `isPreferredName` tinyint(1) DEFAULT NULL, 26 | `isShortName` tinyint(1) DEFAULT NULL, 27 | `isColloquial` tinyint(1) DEFAULT NULL, 28 | `isHistoric` tinyint(1) DEFAULT NULL, 29 | PRIMARY KEY (`alternatenameId`) 30 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 31 | 32 | 33 | 34 | 35 | 36 | CREATE TABLE IF NOT EXISTS `geo_continent` ( 37 | `code` char(2) DEFAULT NULL, 38 | `name` varchar(20) DEFAULT NULL, 39 | `geonameid` int(11) DEFAULT NULL 40 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 41 | 42 | 43 | 44 | 45 | 46 | CREATE TABLE IF NOT EXISTS `geo_country` ( 47 | `iso_alpha2` char(2) NOT NULL DEFAULT '', 48 | `iso_alpha3` char(3) DEFAULT NULL, 49 | `iso_numeric` int(11) DEFAULT NULL, 50 | `fips_code` varchar(3) DEFAULT NULL, 51 | `name` varchar(200) DEFAULT NULL, 52 | `capital` varchar(200) DEFAULT NULL, 53 | `areainsqkm` double DEFAULT NULL, 54 | `population` int(11) DEFAULT NULL, 55 | `continent` char(2) DEFAULT NULL, 56 | `tld` char(3) DEFAULT NULL, 57 | `currency` char(3) DEFAULT NULL, 58 | `currencyName` char(20) DEFAULT NULL, 59 | `Phone` char(10) DEFAULT NULL, 60 | `postalCodeFormat` char(20) DEFAULT NULL, 61 | `postalCodeRegex` char(90) DEFAULT NULL, 62 | `geonameId` int(11) DEFAULT NULL, 63 | `languages` varchar(200) DEFAULT NULL, 64 | `neighbours` char(20) DEFAULT NULL, 65 | `equivalentFipsCode` char(10) DEFAULT NULL, 66 | PRIMARY KEY (`iso_alpha2`), 67 | KEY `iso_alpha3` (`iso_alpha3`) 68 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 69 | 70 | 71 | 72 | 73 | 74 | CREATE TABLE IF NOT EXISTS `geo_featurecodes` ( 75 | `code` char(7) DEFAULT NULL, 76 | `name` varchar(200) DEFAULT NULL, 77 | `description` text 78 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 79 | 80 | 81 | 82 | 83 | 84 | CREATE TABLE IF NOT EXISTS `geo_languagecodes` ( 85 | `iso_639_3` char(4) DEFAULT NULL, 86 | `iso_639_2` varchar(50) DEFAULT NULL, 87 | `iso_639_1` varchar(50) DEFAULT NULL, 88 | `language_name` varchar(200) DEFAULT NULL 89 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 90 | 91 | 92 | 93 | 94 | CREATE TABLE IF NOT EXISTS `geo_name` ( 95 | `geonameid` INT(11) NOT NULL, 96 | `name` VARCHAR(200) NULL DEFAULT NULL, 97 | `asciiname` VARCHAR(200) NULL DEFAULT NULL, 98 | `alternatenames` VARCHAR(4000) NULL DEFAULT NULL, 99 | `latitude` DECIMAL(10,7) NULL DEFAULT NULL, 100 | `longitude` DECIMAL(10,7) NULL DEFAULT NULL, 101 | `fclass` CHAR(1) NULL DEFAULT NULL, 102 | `fcode` VARCHAR(10) NULL DEFAULT NULL, 103 | `country` VARCHAR(2) NULL DEFAULT NULL, 104 | `cc2` VARCHAR(60) NULL DEFAULT NULL, 105 | `admin1` VARCHAR(20) NULL DEFAULT NULL, 106 | `admin2` VARCHAR(80) NULL DEFAULT NULL, 107 | `admin3` VARCHAR(20) NULL DEFAULT NULL, 108 | `admin4` VARCHAR(20) NULL DEFAULT NULL, 109 | `population` INT(11) NULL DEFAULT NULL, 110 | `elevation` INT(11) NULL DEFAULT NULL, 111 | `gtopo30` INT(11) NULL DEFAULT NULL, 112 | `timezone` VARCHAR(40) NULL DEFAULT NULL, 113 | `moddate` DATE NULL DEFAULT NULL, 114 | PRIMARY KEY (`geonameid`), 115 | INDEX `country` (`country`), 116 | FULLTEXT INDEX `name_asciiname_alternatenames` (`name`, `asciiname`, `alternatenames`) 117 | ) 118 | COLLATE='utf8_general_ci' 119 | ENGINE=MyISAM; 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | CREATE TABLE IF NOT EXISTS `geo_timezones` ( 128 | `countrycode` VARCHAR(200) NULL DEFAULT NULL, 129 | `timeZoneId` VARCHAR(200) NOT NULL DEFAULT '', 130 | `GMT_offset` DECIMAL(3,1) NULL DEFAULT NULL, 131 | `DST_offset` DECIMAL(3,1) NULL DEFAULT NULL, 132 | `raw_offset` DECIMAL(3,1) NULL DEFAULT NULL, 133 | PRIMARY KEY (`timeZoneId`), 134 | INDEX `countrycode` (`countrycode`) 135 | ) 136 | COLLATE='utf8_general_ci' 137 | ENGINE=InnoDB; 138 | --------------------------------------------------------------------------------