-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseparate_layers.py
More file actions
172 lines (130 loc) · 5.44 KB
/
separate_layers.py
File metadata and controls
172 lines (130 loc) · 5.44 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
"""
Separate data into different layers based on their types
"""
import asyncio
import asyncpg
import json
async def separate_turkaman_data():
"""Separate turkaman_map data into different tables based on fclass"""
# Database connection
conn = await asyncpg.connect(
host="localhost",
port=5434,
user="postgres",
password="postgres",
database="gis_backend"
)
try:
print("🔧 شروع تقسیمبندی دادههای turkaman_map")
print("=" * 50)
# Get all unique fclass values
fclass_query = """
SELECT DISTINCT fclass, COUNT(*) as count
FROM turkaman_map
WHERE fclass IS NOT NULL
GROUP BY fclass
ORDER BY count DESC
"""
fclass_rows = await conn.fetch(fclass_query)
print(f"📊 {len(fclass_rows)} نوع خط مختلف یافت شد:")
for row in fclass_rows:
print(f" - {row['fclass']}: {row['count']} مورد")
print()
# Create separate tables for each fclass
for row in fclass_rows:
fclass = row['fclass']
count = row['count']
# Create table name
table_name = f"line_{fclass.lower().replace(' ', '_')}"
print(f"🔄 ایجاد جدول {table_name} ({count} مورد)...")
# Create table
create_table_query = f"""
CREATE TABLE IF NOT EXISTS {table_name} AS
SELECT * FROM turkaman_map
WHERE fclass = $1
"""
await conn.execute(create_table_query, fclass)
# Add spatial index
add_index_query = f"""
CREATE INDEX IF NOT EXISTS idx_{table_name}_geom
ON {table_name} USING GIST (geom)
"""
await conn.execute(add_index_query)
# Verify count
verify_query = f"SELECT COUNT(*) FROM {table_name}"
actual_count = await conn.fetchval(verify_query)
print(f" ✅ جدول {table_name} ایجاد شد ({actual_count} مورد)")
print("\n🎉 تقسیمبندی تکمیل شد!")
# Show summary
print("\n📋 خلاصه جداول ایجاد شده:")
print("=" * 35)
for row in fclass_rows:
fclass = row['fclass']
table_name = f"line_{fclass.lower().replace(' ', '_')}"
# Get actual count
count_query = f"SELECT COUNT(*) FROM {table_name}"
actual_count = await conn.fetchval(count_query)
print(f" - {table_name}: {actual_count} مورد")
finally:
await conn.close()
async def separate_points_data():
"""Separate my_points data if needed"""
# Database connection
conn = await asyncpg.connect(
host="localhost",
port=5434,
user="postgres",
password="postgres",
database="gis_backend"
)
try:
print("\n🔧 بررسی دادههای my_points")
print("=" * 30)
# Check if my_points needs separation
check_query = """
SELECT bcnshp, COUNT(*) as count
FROM my_points
WHERE bcnshp IS NOT NULL
GROUP BY bcnshp
"""
bcnshp_rows = await conn.fetch(check_query)
if len(bcnshp_rows) > 1:
print(f"📊 {len(bcnshp_rows)} نوع فانوس مختلف یافت شد:")
for row in bcnshp_rows:
print(f" - نوع {row['bcnshp']}: {row['count']} مورد")
# Create separate tables
for row in bcnshp_rows:
bcnshp = row['bcnshp']
count = row['count']
table_name = f"beacon_type_{int(bcnshp)}"
print(f"🔄 ایجاد جدول {table_name} ({count} مورد)...")
create_table_query = f"""
CREATE TABLE IF NOT EXISTS {table_name} AS
SELECT * FROM my_points
WHERE bcnshp = $1
"""
await conn.execute(create_table_query, bcnshp)
# Add spatial index
add_index_query = f"""
CREATE INDEX IF NOT EXISTS idx_{table_name}_geom
ON {table_name} USING GIST (geom)
"""
await conn.execute(add_index_query)
print(f" ✅ جدول {table_name} ایجاد شد")
else:
print("❌ دادههای my_points نیازی به تقسیم ندارند")
print(f" همه {bcnshp_rows[0]['count']} مورد از نوع {bcnshp_rows[0]['bcnshp']} هستند")
finally:
await conn.close()
async def main():
"""Main function"""
print("🗺️ تقسیمبندی دادههای جغرافیایی")
print("=" * 40)
# Separate turkaman_map data
await separate_turkaman_data()
# Separate my_points data
await separate_points_data()
print("\n✅ تمام عملیات تکمیل شد!")
if __name__ == "__main__":
asyncio.run(main())