182 lines
8.7 KiB
Python
182 lines
8.7 KiB
Python
from lxml import etree
|
||
from difflib import SequenceMatcher
|
||
|
||
data=[]
|
||
class Data_farame():
|
||
def __init__(self,section_KPTR,cadastral_number,cadastral_quarter,object_type,land_plot_area,error_in_determining_area,availability_coordinates,explanations):
|
||
self.section_KPTR = section_KPTR #Раздел КПТР
|
||
self.cadastral_number = cadastral_number #Кадастровый номер
|
||
self.cadastral_quarter = cadastral_quarter #Кадастровый квартал
|
||
self.object_type = object_type #Вид объекта
|
||
self.land_plot_area = land_plot_area #Площадь ЗУ
|
||
self.error_in_determining_area = error_in_determining_area #Погрешность определения площади
|
||
self.availability_coordinates = availability_coordinates #наличие координат
|
||
self.explanations = explanations #Пояснения
|
||
|
||
|
||
class super_visor():
|
||
|
||
def find_best_match(self,input_str: str) -> str:
|
||
keywords = {
|
||
"FormParcels":"Образование ЗУ",
|
||
"SpecifyParcels":"Уточнение ЗУ",
|
||
"CadastralErrorsParcels":"Исправление ЗУ",
|
||
"ExistObjectsRealty":"Уточнение ОКС",
|
||
"CadastralErrorsOKS":"Исправление ОКС"
|
||
}
|
||
|
||
# Сравниваем коэффициент схожести (0.0 - 1.0) строки с каждым ключевым словом
|
||
best_match = max(keywords, key=lambda word: SequenceMatcher(None, input_str, word.lower()).ratio())
|
||
return keywords[best_match]
|
||
|
||
def chec_kad_number(self,atrebut):
|
||
if '@attributes' in atrebut:
|
||
if 'CadastralNumber' in atrebut['@attributes']:
|
||
return atrebut['@attributes']['CadastralNumber']
|
||
if 'CadastralNumber' in atrebut:
|
||
if '#text' in atrebut['CadastralNumber'][0]:
|
||
return atrebut['CadastralNumber'][0]['#text']
|
||
return "нет"
|
||
|
||
def get_cad_block(self,cad_block):
|
||
temp = ""
|
||
for item in cad_block:
|
||
temp += f"{item['#text']}, "
|
||
return temp
|
||
|
||
def chec_cad_block(self, atrebut):
|
||
if 'CadastralBlocks' in atrebut:
|
||
if 'CadastralBlock' in atrebut['CadastralBlocks'][0]:
|
||
return self.get_cad_block(atrebut['CadastralBlocks'][0]['CadastralBlock'])
|
||
if 'CadastralBlock' in atrebut:
|
||
return atrebut['CadastralBlock'][0]['#text']
|
||
return "нет"
|
||
|
||
def get_object_type(self,type:str):
|
||
if type == "002001002000":
|
||
return "Здание"
|
||
if type == "002001004000":
|
||
return "Сооружение"
|
||
if type == "002001005000":
|
||
return "Объект"
|
||
|
||
def chec_object_type(self, atrebut):
|
||
if 'ObjectType' in atrebut:
|
||
return self.get_object_type(atrebut['ObjectType'][0]['#text'])
|
||
return "нет"
|
||
|
||
def chec_area(self,atrebut):
|
||
if "Area" in atrebut:
|
||
if "Area" in atrebut['Area'][0]:
|
||
if '#text' in atrebut['Area'][0]['Area'][0]:
|
||
return atrebut['Area'][0]['Area'][0]['#text']
|
||
return "нет"
|
||
|
||
def chec_inaccuracy(self, atrebut):
|
||
if "Area" in atrebut:
|
||
if "Inaccuracy" in atrebut['Area'][0]:
|
||
if '#text' in atrebut['Area'][0]['Inaccuracy'][0]:
|
||
return atrebut['Area'][0]['Inaccuracy'][0]['#text']
|
||
return "нет"
|
||
|
||
def chec_koordinat(self,atrebut):
|
||
if 'EntitySpatial' in atrebut:
|
||
if 'SpatialElement' in atrebut['EntitySpatial'][0]:
|
||
return "да"
|
||
else:
|
||
return "нет"
|
||
else:
|
||
return "нет"
|
||
|
||
def chec_explanations(self,atrebut):
|
||
if 'Explanations' in atrebut:
|
||
return atrebut['Explanations'][0]['#text']
|
||
return "нет"
|
||
|
||
def chec_object(self, atrebut):
|
||
if '@attributes' in atrebut:
|
||
if 'CadastralNumber' in atrebut['@attributes']:
|
||
return True
|
||
if 'CadastralNumber' in atrebut:
|
||
if '#text' in atrebut['CadastralNumber'][0]:
|
||
return True
|
||
return False
|
||
|
||
def __init__(self, xml):
|
||
i = 0
|
||
for section_name, section_data in xml.get('Package', [{}])[0].items():
|
||
for group in section_data:
|
||
if isinstance(group, dict):
|
||
for item_type, item_list in group.items():
|
||
for obj in item_list:
|
||
if isinstance(obj, dict):
|
||
if self.chec_object(obj):
|
||
data.append(
|
||
Data_farame(
|
||
section_KPTR=self.find_best_match(section_name),
|
||
cadastral_number=self.chec_kad_number(obj),
|
||
cadastral_quarter=self.chec_cad_block(obj),
|
||
object_type=self.chec_object_type(obj),
|
||
land_plot_area=self.chec_area(obj),
|
||
error_in_determining_area=self.chec_inaccuracy(obj),
|
||
availability_coordinates=self.chec_koordinat(obj),
|
||
explanations=self.chec_explanations(obj)
|
||
)
|
||
)
|
||
|
||
else:
|
||
for inner_key, inner_items in obj.items():
|
||
for real_obj in inner_items:
|
||
if isinstance(real_obj, dict):
|
||
data.append(
|
||
Data_farame(
|
||
section_KPTR=self.find_best_match(section_name),
|
||
cadastral_number=self.chec_kad_number(real_obj),
|
||
cadastral_quarter=self.chec_cad_block(real_obj),
|
||
object_type=self.chec_object_type(real_obj),
|
||
land_plot_area=self.chec_area(real_obj),
|
||
error_in_determining_area=self.chec_inaccuracy(real_obj),
|
||
availability_coordinates=self.chec_koordinat(real_obj),
|
||
explanations=self.chec_explanations(real_obj)
|
||
)
|
||
)
|
||
|
||
pass
|
||
|
||
|
||
def lxml_to_dict(element):
|
||
result = {}
|
||
|
||
# Обрабатываем атрибуты (если есть)
|
||
if element.attrib:
|
||
result["@attributes"] = element.attrib
|
||
|
||
# Обрабатываем текстовое содержимое (если есть)
|
||
if element.text and element.text.strip():
|
||
result["#text"] = element.text.strip()
|
||
|
||
# Группируем все дочерние элементы в списки
|
||
children_by_tag = {}
|
||
for child in element:
|
||
child_tag = child.tag
|
||
if child_tag not in children_by_tag:
|
||
children_by_tag[child_tag] = []
|
||
children_by_tag[child_tag].append(lxml_to_dict(child))
|
||
|
||
# Добавляем в результат (всегда списками)
|
||
for tag, children in children_by_tag.items():
|
||
result[tag] = children # Всегда массив, даже если элемент один
|
||
|
||
return result
|
||
|
||
#tree = etree.parse('C:/Users/jze9/Downloads/12. ХМЛ КПТР в ексель/Исходные данные/Карта-план в хмл прмиер Копейск на кк 74 30 0104002/MapPlanTerritory_EA8F3032-3AF5-4FE2-9A0D-72BCCF94DF06.xml')
|
||
#tree = etree.parse('C:/Users/jze9/Downloads/12. ХМЛ КПТР в ексель/Исходные данные/Карта-план в хмл прмиер Копейск на кк 74 30 0104002/MapPlanTerritory_54F3719F-2ED8-48FF-8442-9813191EBA51.xml')
|
||
tree = etree.parse('C:/Users/jze9/Downloads/12. ХМЛ КПТР в ексель/Исходные данные/Карта-план в хмл прмиер Копейск на кк 74 30 0104002/MapPlanTerritory_E2DF5B75-E136-48DA-BB4C-8887BA4ABF46.xml')
|
||
xml_dict = lxml_to_dict(tree.getroot())
|
||
|
||
temp = super_visor(xml_dict)
|
||
|
||
|
||
pass
|
||
|