Skip to content

Commit 2beb546

Browse files
committed
Changed variable types to make it work
1 parent 35ef1a5 commit 2beb546

File tree

7 files changed

+163
-80
lines changed

7 files changed

+163
-80
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# osm-to-sql
2+
3+
---
4+
5+
Simple OSM XML data to SQL converter command. This command will create 7 SQL files with foreign key constraints. You can use these SQL files with MySQL/ SQLite or any other SQL server.
6+
7+
## Usage
8+
9+
This is the default help menu for the command line tool.
10+
11+
```
12+
13+
Usage:
14+
osm-to-sql [OPTIONS] -i <xml_file_path.xml> -d <output_directory>
15+
16+
OPTIONS:
17+
-i Input open street map file in XML format.
18+
-l Default varchar length to using in table creation. [250]
19+
-d Output directory to save output sql files.
20+
-r Maximum rows per one SQL insert query. [400]
21+
-h Prints help information
22+
23+
```
24+
25+
## Table mappings
26+
27+
All tables have foreign key constrations. And please import with the following order when you importing to your database server.
28+
29+
```
30+
1.nodes
31+
id,lat,lng,version,changeset,user,uid,visible,date_time
32+
33+
2.ways
34+
id,version,changeset,user,uid,visible,date_time
35+
36+
3.way_nodes
37+
way_id,node_id
38+
-- way_id = ways(id)
39+
-- node_id = nodes(id)
40+
41+
4.relations
42+
id,version,changeset,user,uid,visible,date_time
43+
44+
5.relation_members
45+
rm_id,relation_id,node_id,way_id,role
46+
-- relation_id = relations(id)
47+
-- node_id = nodes(id)
48+
-- way_id = ways(id)
49+
50+
6.tags
51+
id,name
52+
53+
7.ref_tags
54+
rt_id,tag_id,node_id,relation_id,way_id,value
55+
-- tag_id = tags(id)
56+
-- node_id = nodes(id)
57+
-- relation_id = relations(id)
58+
-- way_id = ways(id)
59+
60+
```
61+
62+
## Contirbutions
63+
64+
All contibutions and issued are welcome. Please help me to make this tool faster and powerfull.

src/argument.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::path::Path;
2+
use std::process;
23

4+
#[derive(Debug, Clone)]
35
pub struct Arguments {
46
// XML file downloaded from geofabrick
57
pub input_file: String,
@@ -40,21 +42,22 @@ impl Default for Arguments {
4042

4143
fn get_arguments_help()->String{
4244
String::from("
43-
Usage:
44-
osm-to-sql [OPTIONS] -i <xml_file_path.xml> -d <output_directory>
45-
46-
OPTIONS:
47-
-i Input open street map file in XML format.
48-
-l Default varchar length to using in table creation. [250]
49-
-d Output directory to save output sql files.
50-
-r Maximum rows per one SQL insert query. [400]
51-
-h Prints help information
45+
Usage:
46+
osm-to-sql [OPTIONS] -i <xml_file_path.xml> -d <output_directory>
47+
48+
OPTIONS:
49+
-i Input open street map file in XML format.
50+
-l Default varchar length to using in table creation. [250]
51+
-d Output directory to save output sql files.
52+
-r Maximum rows per one SQL insert query. [400]
53+
-h Prints help information
5254
")
5355
}
5456

5557
fn show_arguments_error(e:String){
56-
panic!("Invalid Argument(s) : {}
58+
eprintln!("Invalid Argument(s) : {}
5759
{}",e.clone(),get_arguments_help());
60+
process::exit(128);
5861
}
5962

6063
impl Arguments {
@@ -66,23 +69,24 @@ impl Arguments {
6669

6770
for arg in args {
6871
match previous_arg.as_ref() {
69-
"i"=> formated_args.input_file = arg.clone(),
70-
"l"=> formated_args.varchar_length = arg.clone().parse().unwrap(),
71-
"d"=> formated_args.output_dir = arg.clone(),
72-
"r"=> formated_args.maximum_rows_per_query = arg.clone().parse().unwrap(),
73-
"h"=> formated_args.help = true,
72+
"-i"=> formated_args.input_file = arg.clone(),
73+
"-l"=> formated_args.varchar_length = arg.clone().parse().unwrap(),
74+
"-d"=> formated_args.output_dir = arg.clone(),
75+
"-r"=> formated_args.maximum_rows_per_query = arg.clone().parse().unwrap(),
76+
"-h"=> formated_args.help = true,
7477
_=> {},
7578
}
7679

77-
if arg=="h" {
80+
if arg=="-h" {
7881
formated_args.help = true;
7982
}
8083

8184
previous_arg = arg;
8285
}
8386

8487
if formated_args.help {
85-
panic!("{}",get_arguments_help());
88+
println!("{}",get_arguments_help());
89+
process::exit(0);
8690
}
8791

8892
if formated_args.output_dir!=empty_string && !Path::new(&formated_args.output_dir).exists() {

src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ fn main() {
3535

3636
let result = Reader::from_file(&Path::new(&formated_arg.input_file));
3737

38-
let mut nodes_table = SqlFile {..SqlFile::new_node_file(formated_arg.output_dir.clone(),formated_arg.maximum_rows_per_query)};
39-
let mut tags_table = SqlFile {..SqlFile::new_tag_file(formated_arg.output_dir.clone(),formated_arg.maximum_rows_per_query)};
40-
let mut ways_table = SqlFile {..SqlFile::new_main_file("ways", formated_arg.output_dir.clone(), formated_arg.maximum_rows_per_query)};
41-
let mut way_nodes_table = SqlFile {..SqlFile::new_way_nodes_file(formated_arg.output_dir.clone(),formated_arg.maximum_rows_per_query)};
42-
let mut relations_table = SqlFile {..SqlFile::new_main_file("relations", formated_arg.output_dir.clone(), formated_arg.maximum_rows_per_query)};
43-
let mut relation_members_table = SqlFile {..SqlFile::new_relation_members_file(formated_arg.output_dir.clone(), formated_arg.maximum_rows_per_query)};
44-
let mut ref_tags_table = SqlFile {..SqlFile::new_ref_tags_file(formated_arg.output_dir.clone(), formated_arg.maximum_rows_per_query)};
38+
let mut nodes_table = SqlFile {..SqlFile::new_node_file(formated_arg.clone())};
39+
let mut tags_table = SqlFile {..SqlFile::new_tag_file(formated_arg.clone())};
40+
let mut ways_table = SqlFile {..SqlFile::new_main_file(formated_arg.clone(),"ways")};
41+
let mut way_nodes_table = SqlFile {..SqlFile::new_way_nodes_file(formated_arg.clone())};
42+
let mut relations_table = SqlFile {..SqlFile::new_main_file(formated_arg.clone(),"relations")};
43+
let mut relation_members_table = SqlFile {..SqlFile::new_relation_members_file(formated_arg.clone())};
44+
let mut ref_tags_table = SqlFile {..SqlFile::new_ref_tags_file(formated_arg.clone())};
4545

4646
// let mut count = 0;
4747
let mut buf = Vec::new();
4848
let mut used_tags: Vec<String> = vec![];
49-
let mut last_ref_id: i32 =0;
49+
let mut last_ref_id: i64 =0;
5050
let mut last_ref_type: &str ="node";
5151

5252
match result {
@@ -204,7 +204,7 @@ fn main() {
204204

205205
way_nodes_table.insert_to_way_nodes_file(WayNode {
206206
way_id:last_ref_id,
207-
node_id: attr.value.parse::<i32>().unwrap()
207+
node_id: attr.value.parse::<i64>().unwrap()
208208
})
209209
},
210210
Err(e)=>panic!("Error reading ref attribute in nd tag: {:?}",e)
@@ -258,7 +258,7 @@ fn main() {
258258
relation_members_table.insert_to_relation_members_file(RelationMember {
259259
role:role,
260260
ref_type: ref_attr.value,
261-
ref_id: Attr::from_quick_xml(ref_attribute).value.parse::<i32>().unwrap(),
261+
ref_id: Attr::from_quick_xml(ref_attribute).value.parse::<i64>().unwrap(),
262262
relation_id: last_ref_id
263263
})
264264
},

src/main_info.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::str;
33

44
pub struct MainInfo {
55
pub changeset:i32,
6-
pub id: i32,
7-
pub version: i8,
6+
pub id: i64,
7+
pub version: i16,
88
pub timestamp: String,
99
pub user: String,
1010
pub uid: i32,
@@ -29,11 +29,11 @@ impl MainInfo {
2929
pub fn set_attribute(&mut self, attr:crate::main_info::Attr)->bool{
3030
match attr.name.as_ref() {
3131
"id"=>{
32-
self.id = attr.value.parse::<i32>().unwrap();
32+
self.id = attr.value.parse::<i64>().unwrap();
3333
return true;
3434
} ,
3535
"version" => {
36-
self.version = attr.value.parse::<i8>().unwrap();
36+
self.version = attr.value.parse::<i16>().unwrap();
3737
return true;
3838
},
3939
"changeset"=>{
@@ -93,6 +93,6 @@ pub struct Tag {
9393
pub struct UsedTag {
9494
pub tag: Tag,
9595
pub value: String,
96-
pub ref_id: i32,
96+
pub ref_id: i64,
9797
pub ref_type:&'static str
9898
}

src/relation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl Default for Relation {
1313

1414
pub struct RelationMember {
1515
pub ref_type: String,
16-
pub ref_id: i32,
17-
pub relation_id: i32,
16+
pub ref_id: i64,
17+
pub relation_id: i64,
1818
pub role: String
1919
}

0 commit comments

Comments
 (0)