Spaces:
Paused
Paused
Update deploying_3_3.py
Browse files- deploying_3_3.py +45 -108
deploying_3_3.py
CHANGED
|
@@ -93,8 +93,11 @@ This portion is used to convert vertices read from dxf to pixels in order to acc
|
|
| 93 |
|
| 94 |
"""PDF to image"""
|
| 95 |
|
| 96 |
-
def pdftoimg(datadoc):
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
page=doc[0]
|
| 99 |
pix = page.get_pixmap() # render page to an image
|
| 100 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
|
@@ -125,9 +128,12 @@ def get_paper_size_in_inches(width, height):
|
|
| 125 |
return size
|
| 126 |
return "Unknown Size"
|
| 127 |
|
| 128 |
-
def analyze_pdf(datadoc):
|
| 129 |
# Open the PDF file
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Iterate through pages and print their sizes
|
| 133 |
for page_number in range(len(pdf_document)):
|
|
@@ -182,9 +188,12 @@ def switch_case(argument):
|
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
-
def RetriveRatio(datadoc,dxfpath):
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
if(width > height ):
|
| 190 |
bigger=width
|
|
@@ -884,10 +893,12 @@ def rotate_polygon(polygon, angle, pdfrotation,width,height,center_point=(0, 0))
|
|
| 884 |
#loop 3la hatched areas and count the occurences of each shape w create a table bl hagat di
|
| 885 |
|
| 886 |
|
| 887 |
-
def Create_DF(dxfpath,datadoc,hatched_areas):
|
| 888 |
|
| 889 |
-
|
| 890 |
-
|
|
|
|
|
|
|
| 891 |
|
| 892 |
# hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
|
| 893 |
# print('hatched_areas',hatched_areas)
|
|
@@ -952,25 +963,6 @@ def adjustannotations(OutputPdfStage1):
|
|
| 952 |
|
| 953 |
# Iterate over pages
|
| 954 |
for page_index, page in enumerate(writer.pages):
|
| 955 |
-
|
| 956 |
-
# page.update({
|
| 957 |
-
# NameObject("/UserUnit"): FloatObject(1.0), # 1 unit = 1 real-world unit (e.g., 1 meter)
|
| 958 |
-
# NameObject("/VP"): ArrayObject([
|
| 959 |
-
# DictionaryObject({
|
| 960 |
-
# NameObject("/Type"): NameObject("/Viewport"),
|
| 961 |
-
# NameObject("/BBox"): ArrayObject([
|
| 962 |
-
# FloatObject(0), FloatObject(0), FloatObject(1000), FloatObject(1000)
|
| 963 |
-
# ]), # Bounding box for the viewport
|
| 964 |
-
# NameObject("/Measure"): DictionaryObject({
|
| 965 |
-
# NameObject("/Type"): NameObject("/Measure"),
|
| 966 |
-
# NameObject("/Subtype"): NameObject("/RL"),
|
| 967 |
-
# NameObject("/X"): FloatObject(1),
|
| 968 |
-
# NameObject("/Y"): FloatObject(1),
|
| 969 |
-
# NameObject("/U"): TextStringObject("m"), # Units (meters)
|
| 970 |
-
# }),
|
| 971 |
-
# })
|
| 972 |
-
# ])
|
| 973 |
-
# })
|
| 974 |
if "/Annots" in page:
|
| 975 |
annotations = page["/Annots"]
|
| 976 |
for annot_index, annot in enumerate(annotations):
|
|
@@ -1009,94 +1001,34 @@ def adjustannotations(OutputPdfStage1):
|
|
| 1009 |
writer.write(output_pdf_io)
|
| 1010 |
output_pdf_io.seek(0)
|
| 1011 |
|
|
|
|
| 1012 |
return output_pdf_io.read()
|
| 1013 |
|
| 1014 |
-
|
| 1015 |
-
# new_bytes_object.seek(0)
|
| 1016 |
-
# return new_bytes_object.read()
|
| 1017 |
-
|
| 1018 |
-
# def adjustannotations(OutputPdfStage1):
|
| 1019 |
-
# """
|
| 1020 |
-
# Adjusts annotations in the PDF to include measurement and scale information.
|
| 1021 |
-
|
| 1022 |
-
# Parameters:
|
| 1023 |
-
# OutputPdfStage1 (str): Path to the input PDF file.
|
| 1024 |
-
|
| 1025 |
-
# Returns:
|
| 1026 |
-
# bytes: The adjusted PDF data as bytes.
|
| 1027 |
-
# """
|
| 1028 |
-
# with open(OutputPdfStage1, "rb") as pdf_file:
|
| 1029 |
-
# reader = PdfReader(pdf_file)
|
| 1030 |
-
# writer = PdfWriter()
|
| 1031 |
-
|
| 1032 |
-
# # Append all pages from reader to writer
|
| 1033 |
-
# writer.append_pages_from_reader(reader)
|
| 1034 |
-
|
| 1035 |
-
# # Iterate over pages and add measurement details
|
| 1036 |
-
# for page_index, page in enumerate(writer.pages):
|
| 1037 |
-
# # Add scale settings at the page level
|
| 1038 |
-
# # page.update({
|
| 1039 |
-
# # NameObject("/UserUnit"): FloatObject(1.0), # 1 unit = 1 real-world unit
|
| 1040 |
-
# # NameObject("/VP"): ArrayObject([
|
| 1041 |
-
# # DictionaryObject({
|
| 1042 |
-
# # NameObject("/Type"): NameObject("/Viewport"),
|
| 1043 |
-
# # NameObject("/BBox"): ArrayObject([
|
| 1044 |
-
# # FloatObject(0), FloatObject(0), FloatObject(100), FloatObject(100)
|
| 1045 |
-
# # ]), # Bounding box for the viewport
|
| 1046 |
-
# # NameObject("/Measure"): DictionaryObject({
|
| 1047 |
-
# # NameObject("/Type"): NameObject("/Measure"),
|
| 1048 |
-
# # NameObject("/Subtype"): NameObject("/RL"),
|
| 1049 |
-
# # NameObject("/X"): FloatObject(1), # Horizontal scale
|
| 1050 |
-
# # NameObject("/Y"): FloatObject(1), # Vertical scale
|
| 1051 |
-
# # NameObject("/U"): TextStringObject("m"), # Units (meters)
|
| 1052 |
-
# # }),
|
| 1053 |
-
# # })
|
| 1054 |
-
# # ])
|
| 1055 |
-
# # })
|
| 1056 |
-
|
| 1057 |
-
# # Process annotations
|
| 1058 |
-
# if "/Annots" in page:
|
| 1059 |
-
# annotations = page["/Annots"]
|
| 1060 |
-
# for annot in annotations:
|
| 1061 |
-
# obj = annot.get_object()
|
| 1062 |
-
|
| 1063 |
-
# # Adjust polygon annotations with area measurements
|
| 1064 |
-
# if obj.get("/Subtype") == "/Polygon" and "/Contents" in obj and "sq m" in obj["/Contents"]:
|
| 1065 |
-
# obj.update({
|
| 1066 |
-
# NameObject("/Measure"): DictionaryObject({
|
| 1067 |
-
# NameObject("/Type"): NameObject("/Measure"),
|
| 1068 |
-
# NameObject("/Area"): DictionaryObject({
|
| 1069 |
-
# NameObject("/G"): FloatObject(1),
|
| 1070 |
-
# NameObject("/U"): TextStringObject("sq m"), # Area unit
|
| 1071 |
-
# }),
|
| 1072 |
-
# NameObject("/X"): FloatObject(1), # Horizontal scale
|
| 1073 |
-
# NameObject("/Y"): FloatObject(1), # Vertical scale
|
| 1074 |
-
# NameObject("/U"): TextStringObject("m"), # Units (meters)
|
| 1075 |
-
# }),
|
| 1076 |
-
# NameObject("/IT"): NameObject("/Area_Annotation"),
|
| 1077 |
-
# NameObject("/Subj"): TextStringObject("Area Measurement"),
|
| 1078 |
-
# })
|
| 1079 |
-
|
| 1080 |
-
# print(obj)
|
| 1081 |
-
# output_pdf_io = BytesIO()
|
| 1082 |
-
# writer.write(output_pdf_io)
|
| 1083 |
-
# output_pdf_io.seek(0) # Ensure buffer is at the start
|
| 1084 |
-
# return output_pdf_io
|
| 1085 |
|
| 1086 |
|
| 1087 |
-
def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
|
| 1088 |
OutputPdfStage1='BB Trial.pdf'
|
| 1089 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1090 |
|
| 1091 |
# hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
|
| 1092 |
# hatched_areas=remove_duplicate_shapes(new_hatched_areas)
|
| 1093 |
-
|
| 1094 |
-
|
|
|
|
|
|
|
| 1095 |
flipped_horizontal=flip(img)
|
| 1096 |
allcnts = []
|
| 1097 |
imgg = flipped_horizontal
|
| 1098 |
# imgtransparent1=imgg.copy()
|
| 1099 |
-
|
|
|
|
|
|
|
|
|
|
| 1100 |
page2 = doc[0]
|
| 1101 |
rotationOld=page2.rotation
|
| 1102 |
derotationMatrix=page2.derotation_matrix
|
|
@@ -1118,7 +1050,10 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
|
|
| 1118 |
allshapes=[]
|
| 1119 |
# Iterate through each polygon in metric units
|
| 1120 |
NewColors = []
|
| 1121 |
-
|
|
|
|
|
|
|
|
|
|
| 1122 |
i=0
|
| 1123 |
flagcolor = 0
|
| 1124 |
ColorCheck=[]
|
|
@@ -1328,8 +1263,10 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
|
|
| 1328 |
# doc2 = fitz.open(stream=OutputPdfStage2, filetype="pdf")
|
| 1329 |
|
| 1330 |
doc2 =fitz.open('pdf',OutputPdfStage2)
|
| 1331 |
-
|
| 1332 |
-
|
|
|
|
|
|
|
| 1333 |
# dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
|
| 1334 |
# md, res =dbxTeam.files_download(path= pdfpath+pdfname)
|
| 1335 |
# data = res.content
|
|
|
|
| 93 |
|
| 94 |
"""PDF to image"""
|
| 95 |
|
| 96 |
+
def pdftoimg(datadoc,pdf_content=0):
|
| 97 |
+
if pdf_content:
|
| 98 |
+
doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 99 |
+
else:
|
| 100 |
+
doc = fitz.open('pdf',datadoc)
|
| 101 |
page=doc[0]
|
| 102 |
pix = page.get_pixmap() # render page to an image
|
| 103 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
|
|
|
| 128 |
return size
|
| 129 |
return "Unknown Size"
|
| 130 |
|
| 131 |
+
def analyze_pdf(datadoc,pdf_content=0):
|
| 132 |
# Open the PDF file
|
| 133 |
+
if pdf_content:
|
| 134 |
+
pdf_document = fitz.open(stream=pdf_content, filetype="pdf")
|
| 135 |
+
else:
|
| 136 |
+
pdf_document = fitz.open('pdf',datadoc)
|
| 137 |
|
| 138 |
# Iterate through pages and print their sizes
|
| 139 |
for page_number in range(len(pdf_document)):
|
|
|
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
+
def RetriveRatio(datadoc,dxfpath,pdf_content=0):
|
| 192 |
+
if pdf_content:
|
| 193 |
+
width,height,paper_size = analyze_pdf (datadoc,pdf_content)
|
| 194 |
+
else:
|
| 195 |
+
width,height,paper_size = analyze_pdf (datadoc)
|
| 196 |
+
|
| 197 |
|
| 198 |
if(width > height ):
|
| 199 |
bigger=width
|
|
|
|
| 893 |
#loop 3la hatched areas and count the occurences of each shape w create a table bl hagat di
|
| 894 |
|
| 895 |
|
| 896 |
+
def Create_DF(dxfpath,datadoc,hatched_areas,pdf_content=0):
|
| 897 |
|
| 898 |
+
if pdf_content:
|
| 899 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath,pdf_content)
|
| 900 |
+
else:
|
| 901 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath)
|
| 902 |
|
| 903 |
# hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
|
| 904 |
# print('hatched_areas',hatched_areas)
|
|
|
|
| 963 |
|
| 964 |
# Iterate over pages
|
| 965 |
for page_index, page in enumerate(writer.pages):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 966 |
if "/Annots" in page:
|
| 967 |
annotations = page["/Annots"]
|
| 968 |
for annot_index, annot in enumerate(annotations):
|
|
|
|
| 1001 |
writer.write(output_pdf_io)
|
| 1002 |
output_pdf_io.seek(0)
|
| 1003 |
|
| 1004 |
+
print(f"Annotations updated and saved to {output_pdf_path}")
|
| 1005 |
return output_pdf_io.read()
|
| 1006 |
|
| 1007 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1008 |
|
| 1009 |
|
| 1010 |
+
def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0,pdf_content=0):
|
| 1011 |
OutputPdfStage1='BB Trial.pdf'
|
| 1012 |
+
if pdf_content:
|
| 1013 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath,pdf_content)
|
| 1014 |
+
else:
|
| 1015 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath)
|
| 1016 |
+
|
| 1017 |
|
| 1018 |
# hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
|
| 1019 |
# hatched_areas=remove_duplicate_shapes(new_hatched_areas)
|
| 1020 |
+
if pdf_content:
|
| 1021 |
+
img=pdftoimg(datadoc,pdf_content)
|
| 1022 |
+
else:
|
| 1023 |
+
img=pdftoimg(datadoc)
|
| 1024 |
flipped_horizontal=flip(img)
|
| 1025 |
allcnts = []
|
| 1026 |
imgg = flipped_horizontal
|
| 1027 |
# imgtransparent1=imgg.copy()
|
| 1028 |
+
if pdf_content:
|
| 1029 |
+
doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 1030 |
+
else:
|
| 1031 |
+
doc = fitz.open('pdf',datadoc)
|
| 1032 |
page2 = doc[0]
|
| 1033 |
rotationOld=page2.rotation
|
| 1034 |
derotationMatrix=page2.derotation_matrix
|
|
|
|
| 1050 |
allshapes=[]
|
| 1051 |
# Iterate through each polygon in metric units
|
| 1052 |
NewColors = []
|
| 1053 |
+
if pdf_content:
|
| 1054 |
+
SimilarAreaDictionary=Create_DF(dxfpath,datadoc,hatched_areas,pdf_content)
|
| 1055 |
+
else:
|
| 1056 |
+
SimilarAreaDictionary=Create_DF(dxfpath,datadoc,hatched_areas)
|
| 1057 |
i=0
|
| 1058 |
flagcolor = 0
|
| 1059 |
ColorCheck=[]
|
|
|
|
| 1263 |
# doc2 = fitz.open(stream=OutputPdfStage2, filetype="pdf")
|
| 1264 |
|
| 1265 |
doc2 =fitz.open('pdf',OutputPdfStage2)
|
| 1266 |
+
if pdf_content:
|
| 1267 |
+
gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary , pdfname,pdfpath,pdf_content)
|
| 1268 |
+
else:
|
| 1269 |
+
gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary , pdfname,pdfpath)
|
| 1270 |
# dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
|
| 1271 |
# md, res =dbxTeam.files_download(path= pdfpath+pdfname)
|
| 1272 |
# data = res.content
|