Marthee commited on
Commit
4edd254
·
verified ·
1 Parent(s): 7516de2

Update deploying_3_3.py

Browse files
Files changed (1) hide show
  1. deploying_3_3.py +218 -17
deploying_3_3.py CHANGED
@@ -41,6 +41,13 @@ Original file is located at
41
  # pip install ezdxf scipy
42
 
43
  """## Imports"""
 
 
 
 
 
 
 
44
 
45
  import numpy as np
46
  import cv2
@@ -65,6 +72,8 @@ import tsadropboxretrieval
65
  from ezdxf import bbox
66
  from math import sin, cos, radians
67
  import google_sheet_Legend
 
 
68
 
69
  """## Notes"""
70
 
@@ -876,7 +885,8 @@ def rotate_polygon(polygon, angle, pdfrotation,width,height,center_point=(0, 0))
876
 
877
 
878
  def Create_DF(dxfpath,datadoc,hatched_areas):
879
-
 
880
  FinalRatio= RetriveRatio(datadoc,dxfpath)
881
 
882
  # hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
@@ -923,7 +933,159 @@ def Create_DF(dxfpath,datadoc,hatched_areas):
923
  return SimilarAreaDictionary
924
  """### Draw on Image and PDF"""
925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926
  def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
 
927
  FinalRatio= RetriveRatio(datadoc,dxfpath)
928
 
929
  # hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
@@ -965,7 +1127,8 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
965
  for polygon in hatched_areas:
966
  cntPoints = []
967
  cntPoints1 = []
968
- shapee = []
 
969
 
970
  blackImgShapes = np.zeros(imgg.shape[:2], dtype="uint8")
971
  blackImgShapes= cv2.cvtColor(blackImgShapes, cv2.COLOR_GRAY2BGR)
@@ -983,16 +1146,40 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
983
 
984
  cv2.drawContours(blackImgShapes, [np.array(cntPoints)], -1, ([255,255,255]), thickness=-1)
985
  x, y, w, h = cv2.boundingRect(np.array(cntPoints))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
986
 
987
  for poi in np.array(cntPoints1):
988
- x1, y1 = poi
989
- p1 = fitz.Point(x1,y1)
990
- # p1 = fitz.Point(x1,y1)
991
- p1=p1*derotationMatrix
992
- shapee.append([p1[0],p1[1]])
993
-
994
- shapee=np.flip(shapee,1)
995
- shapee=rotate_polygon(shapee,rotationangle,rotationOld,width,height)
 
 
 
 
996
  tol=0
997
  condition1 = (SimilarAreaDictionary['Area'] >= polygon[1] - tol) & (SimilarAreaDictionary['Area'] <= polygon[1] +tol)
998
  condition2 = (SimilarAreaDictionary['Perimeter'] >= polygon[2] -tol) & (SimilarAreaDictionary['Perimeter'] <= polygon[2] +tol)
@@ -1080,20 +1267,20 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
1080
 
1081
  cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
1082
 
1083
- annot11 = page2.add_polygon_annot( points=shapee) # 'Polygon'
1084
  annot11.set_border(width=0.2)
1085
  annot11.set_colors(stroke=(int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255), fill= (int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255) )
1086
- annot11.set_info(content='Area='+str(polygon[1])+' m^2',subject='ADR Team')
1087
  annot11.set_opacity(0.8)
1088
  # annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
1089
  annot11.update()
1090
 
1091
 
1092
 
1093
- annot12 = page2.add_polygon_annot( points=shapee) # 'Polygon'
1094
- annot12.set_border(width=0.2)
1095
  annot12.set_colors(stroke=(int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255))
1096
- annot12.set_info(content='Perimeter='+str(polygon[2])+' m',subject='ADR Team')
1097
  annot12.set_opacity(0.8)
1098
  # annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
1099
  annot12.update()
@@ -1130,6 +1317,17 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
1130
  }).reset_index()
1131
 
1132
  SimilarAreaDictionary = grouped_df
 
 
 
 
 
 
 
 
 
 
 
1133
 
1134
  gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary , pdfname,pdfpath)
1135
  # dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
@@ -1139,7 +1337,8 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
1139
  # list1=pd.DataFrame(columns=['content', 'creationDate', 'id', 'modDate', 'name', 'subject', 'title'])
1140
  list1=pd.DataFrame(columns=['content', 'id', 'subject','color'])
1141
 
1142
- for page in doc:
 
1143
  # Iterate through annotations on the page
1144
  for annot in page.annots():
1145
  # Get the color of the annotation
@@ -1155,7 +1354,8 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
1155
  v='stroke'
1156
  x,y,z=int(annot_color.get(v)[0]*255),int(annot_color.get(v)[1]*255),int(annot_color.get(v)[2]*255)
1157
  list1.loc[len(list1)] =[annot.info['content'],annot.info['id'],annot.info['subject'],[x,y,z]]
1158
- return doc,image_new1, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas
 
1159
 
1160
  # doc.save('Testing(2.7).pdf')
1161
  # return doc,image_new1#, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas
@@ -1166,3 +1366,4 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
1166
  # doc,image_new1=mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio)
1167
  # cv2_imshow(image_new1)
1168
 
 
 
41
  # pip install ezdxf scipy
42
 
43
  """## Imports"""
44
+ import xml.etree.ElementTree as ET
45
+ from PyPDF2 import PdfReader, PdfWriter
46
+ from PyPDF2.generic import TextStringObject, NameObject, ArrayObject, FloatObject
47
+ from PyPDF2.generic import NameObject, TextStringObject, DictionaryObject, FloatObject, ArrayObject
48
+
49
+ from typing import NewType
50
+ from ctypes import sizeof
51
 
52
  import numpy as np
53
  import cv2
 
72
  from ezdxf import bbox
73
  from math import sin, cos, radians
74
  import google_sheet_Legend
75
+ from PyPDF2 import PdfReader
76
+ from io import BytesIO
77
 
78
  """## Notes"""
79
 
 
885
 
886
 
887
  def Create_DF(dxfpath,datadoc,hatched_areas):
888
+
889
+
890
  FinalRatio= RetriveRatio(datadoc,dxfpath)
891
 
892
  # hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
 
933
  return SimilarAreaDictionary
934
  """### Draw on Image and PDF"""
935
 
936
+ def adjustannotations(OutputPdfStage1):
937
+ input_pdf_path = OutputPdfStage1
938
+ output_pdf_path = "AnnotationAdjusted.pdf"
939
+
940
+ # Load the input PDF
941
+ pdf_bytes_io = BytesIO(OutputPdfStage1)
942
+
943
+ reader = PdfReader(pdf_bytes_io)
944
+ writer = PdfWriter()
945
+
946
+ # Append all pages to the writer
947
+ writer.append_pages_from_reader(reader)
948
+
949
+ # Add metadata (optional)
950
+ metadata = reader.metadata
951
+ writer.add_metadata(metadata)
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):
977
+ obj = annot.get_object()
978
+
979
+ print("obj", obj)
980
+ # print(obj.get("/IT"))
981
+
982
+ if obj.get("/Subtype") == "/Polygon":
983
+ print("AWL ANNOT IF")
984
+ # Check the /IT value to differentiate annotations
985
+ if "/Contents" in obj and "sq m" in obj["/Contents"]:
986
+ print("Tany IF")
987
+ obj.update({
988
+ NameObject("/Measure"): DictionaryObject({
989
+ NameObject("/Type"): NameObject("/Measure"),
990
+ NameObject("/Area"): DictionaryObject({
991
+ NameObject("/G"): FloatObject(1),
992
+ NameObject("/U"): TextStringObject("sq m"), # Unit of measurement for area
993
+ }),
994
+
995
+ NameObject("/X"): FloatObject(1), # Horizontal scale (e.g., 1 unit = 1 meter)
996
+ NameObject("/Y"): FloatObject(1), # Vertical scale
997
+
998
+ }),
999
+ NameObject("/IT"): NameObject("/Area_Annotation"), # Use more distinctive name
1000
+ NameObject("/Subj"): TextStringObject("Area Measurement"), # Intent explicitly for Area
1001
+ })
1002
+
1003
+
1004
+ print("After Update:", obj)
1005
+
1006
+ # # Save the modified PDF
1007
+
1008
+ output_pdf_io = BytesIO()
1009
+ writer.write(output_pdf_io)
1010
+ output_pdf_io.seek(0)
1011
+
1012
+ return output_pdf_io.read()
1013
+
1014
+ # writer.write(new_bytes_object) # This writes the modified PDF data to new_bytes_object
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
  FinalRatio= RetriveRatio(datadoc,dxfpath)
1090
 
1091
  # hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
 
1127
  for polygon in hatched_areas:
1128
  cntPoints = []
1129
  cntPoints1 = []
1130
+ shapeePerimeter = []
1131
+ shapeeArea = []
1132
 
1133
  blackImgShapes = np.zeros(imgg.shape[:2], dtype="uint8")
1134
  blackImgShapes= cv2.cvtColor(blackImgShapes, cv2.COLOR_GRAY2BGR)
 
1146
 
1147
  cv2.drawContours(blackImgShapes, [np.array(cntPoints)], -1, ([255,255,255]), thickness=-1)
1148
  x, y, w, h = cv2.boundingRect(np.array(cntPoints))
1149
+ firstpoint = 0
1150
+ for poi in np.array(cntPoints1):
1151
+ if firstpoint == 0:
1152
+ x2, y2 = poi
1153
+ p2 = fitz.Point(x2,y2)
1154
+ # p1 = fitz.Point(x1,y1)
1155
+ p2=p2*derotationMatrix
1156
+ shapeePerimeter.append([p2[0],p2[1]])
1157
+ firstpoint = 1
1158
+ else:
1159
+ x1, y1 = poi
1160
+ p1 = fitz.Point(x1,y1)
1161
+ # p1 = fitz.Point(x1,y1)
1162
+ p1=p1*derotationMatrix
1163
+ print("P1 = ",p1)
1164
+ shapeePerimeter.append([p1[0],p1[1]])
1165
+
1166
+ shapeePerimeter.append([p2[0],p2[1]])
1167
+ shapeePerimeter=np.flip(shapeePerimeter,1)
1168
+ shapeePerimeter=rotate_polygon(shapeePerimeter,rotationangle,rotationOld,width,height)
1169
 
1170
  for poi in np.array(cntPoints1):
1171
+ x1, y1 = poi
1172
+ p1 = fitz.Point(x1,y1)
1173
+ # p1 = fitz.Point(x1,y1)
1174
+ p1=p1*derotationMatrix
1175
+ print("P1 = ",p1)
1176
+ shapeeArea.append([p1[0],p1[1]])
1177
+
1178
+ shapeeArea.append([p2[0],p2[1]])
1179
+ shapeeArea=np.flip(shapeeArea,1)
1180
+ shapeeArea=rotate_polygon(shapeeArea,rotationangle,rotationOld,width,height)
1181
+
1182
+
1183
  tol=0
1184
  condition1 = (SimilarAreaDictionary['Area'] >= polygon[1] - tol) & (SimilarAreaDictionary['Area'] <= polygon[1] +tol)
1185
  condition2 = (SimilarAreaDictionary['Perimeter'] >= polygon[2] -tol) & (SimilarAreaDictionary['Perimeter'] <= polygon[2] +tol)
 
1267
 
1268
  cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
1269
 
1270
+ annot11 = page2.add_polygon_annot( points=shapeeArea) # 'Polygon'
1271
  annot11.set_border(width=0.2)
1272
  annot11.set_colors(stroke=(int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255), fill= (int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255) )
1273
+ annot11.set_info(content=str(polygon[1])+' sq m',subject='Area Measurement', title="ADR Team")
1274
  annot11.set_opacity(0.8)
1275
  # annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
1276
  annot11.update()
1277
 
1278
 
1279
 
1280
+ annot12 = page2.add_polyline_annot( points=shapeePerimeter ) # 'Polygon'
1281
+ annot12.set_border(width=0.8)
1282
  annot12.set_colors(stroke=(int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255))
1283
+ annot12.set_info(content=str(polygon[2])+' m',subject='Perimeter Measurement', title="ADR Team")
1284
  annot12.set_opacity(0.8)
1285
  # annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
1286
  annot12.update()
 
1317
  }).reset_index()
1318
 
1319
  SimilarAreaDictionary = grouped_df
1320
+ # doc.save(OutputPdfStage1)
1321
+ modified_pdf_data = doc.tobytes()
1322
+ OutputPdfStage2=adjustannotations(modified_pdf_data)
1323
+
1324
+ # with open("Adjusted_PDF.pdf", "wb") as f:
1325
+ # f.write(OutputPdfStage2)
1326
+
1327
+ # doc2 = fitz.open(stream=OutputPdfStage2, filetype="pdf")
1328
+ # doc2 = fitz.open(stream=OutputPdfStage2, filetype="pdf")
1329
+
1330
+ doc2 =fitz.open('pdf',OutputPdfStage2)
1331
 
1332
  gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary , pdfname,pdfpath)
1333
  # dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
 
1337
  # list1=pd.DataFrame(columns=['content', 'creationDate', 'id', 'modDate', 'name', 'subject', 'title'])
1338
  list1=pd.DataFrame(columns=['content', 'id', 'subject','color'])
1339
 
1340
+ # for page in doc:
1341
+ for page in doc2:
1342
  # Iterate through annotations on the page
1343
  for annot in page.annots():
1344
  # Get the color of the annotation
 
1354
  v='stroke'
1355
  x,y,z=int(annot_color.get(v)[0]*255),int(annot_color.get(v)[1]*255),int(annot_color.get(v)[2]*255)
1356
  list1.loc[len(list1)] =[annot.info['content'],annot.info['id'],annot.info['subject'],[x,y,z]]
1357
+ print('LISTTT',list1)
1358
+ return doc2,image_new1, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas
1359
 
1360
  # doc.save('Testing(2.7).pdf')
1361
  # return doc,image_new1#, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas
 
1366
  # doc,image_new1=mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio)
1367
  # cv2_imshow(image_new1)
1368
 
1369
+