Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPIKE: highlight withdrawals #2374

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ynr/apps/elections/templates/elections/sopn_for_ballot.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ <h2>
{% include "elections/includes/_sopn_debug.html" %}
{% endif %}


<div id="sopn-{{ object.ballot_paper_id }}" class="pdf_container"></div>

{% else %}
Expand All @@ -78,13 +79,14 @@ <h2>
{% url 'admin:official_documents_ballotsopn_change' object.sopn.id as url %}
You can <a href="{{ url }}">edit this in the admin interface</a> (e.g. to delete it)
{% endif %}

{% if object.sopn.uploaded_file.url|slice:"-3:" == "pdf" %}
<script type="module">
import { SOPN_VIEWER } from '/upload_document/sopn_viewer.js';
import {SOPN_VIEWER} from '/upload_document/sopn_viewer.js';

SOPN_VIEWER.ShowSOPNInline(
'{{ object.sopn.uploaded_file.url }}',
'{{ object.ballot_paper_id }}'
'{{ object.ballot_paper_id }}',
{{ object.sopn.awstextractparsedsopn.get_withdrawals_bboxes|safe }}
)
</script>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ var SOPN_VIEWER = (function () {

var module = {};

function load_page(pdf, container, page_num) {
function drawRectangles(context, rectangles) {
rectangles.forEach(rect => {
context.beginPath();
context.rect(rect.x, rect.y, rect.width, rect.height);
context.lineWidth = rect.lineWidth || 1;
context.strokeStyle = rect.color || 'red';
context.stroke();
});
}

function load_page(pdf, container, page_num, rectanglesPerPage) {
return pdf.getPage(page_num).then(function (page) {

var scale = 1.2;
Expand All @@ -25,6 +35,8 @@ var SOPN_VIEWER = (function () {
var context = canvas.getContext("2d");
canvas.height = viewport.height;
canvas.width = viewport.width;
console.log(canvas.height)
console.log(canvas.width)
var renderContext = {
canvasContext: context,
viewport: viewport
Expand All @@ -33,6 +45,12 @@ var SOPN_VIEWER = (function () {
var renderTask = page.render(renderContext);
return renderTask.promise.then(function () {
container.append(page_container);

if (rectanglesPerPage && rectanglesPerPage[page_num]) {
drawRectangles(context, rectanglesPerPage[page_num]);
}


return page.getTextContent({normalizeWhitespace: true});
}).then(function (textContent) {
var pdf_canvas = $(canvas),
Expand All @@ -57,14 +75,16 @@ var SOPN_VIEWER = (function () {
viewport: viewport,
textDivs: []
});


});

}

});
}

function ShowSOPNInline(sopn_url, ballot_paper_id, options) {
function ShowSOPNInline(sopn_url, ballot_paper_id, rectanglesPerPage) {
// The container element
var this_pdf_container = document.getElementById("sopn-" + ballot_paper_id);

Expand All @@ -73,7 +93,7 @@ var SOPN_VIEWER = (function () {
loadingTask.promise.then(function (pdf) {
var promise = Promise.resolve();
for (let page = 1; page <= pdf.numPages; page++) {
promise = promise.then(() => load_page(pdf, this_pdf_container, page));
promise = promise.then(() => load_page(pdf, this_pdf_container, page, rectanglesPerPage));
}
return promise;
}).then(null, function (error) {
Expand Down
42 changes: 42 additions & 0 deletions ynr/apps/sopn_parsing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,45 @@ def parse_raw_data(self):

def as_textractor_document(self):
return response_parser.parse(json.loads(self.raw_data))

def get_withdrawals_bboxes(self):
# headers = self.as_pandas.iloc[0].tolist()
# get colmun index from headers
column = "5"
column_values = self.as_pandas[column].tolist()
cells_with_value = []
for i, row in enumerate(column_values):
if row:
cells_with_value.append(i)
cells_with_value.pop(0)
# Deal with more than one page
textract_cells = []
for table in self.as_textractor_document().tables:
for cell in table.table_cells:
# if str(cell.col_index-1) != column:
# continue
if cell.row_index - 1 in cells_with_value:
textract_cells.append(cell)
print(textract_cells)

doc_height = 1429
doc_width = 1010

page = 1
box_data = {page: []}
for cell in textract_cells:
absolute_x = cell.x * doc_width
absolute_y = cell.y * doc_height
absolute_width = cell.width * doc_width
absolute_height = cell.height * doc_height
box_data[page].append(
{
"x": absolute_x,
"y": absolute_y,
"width": absolute_width,
"height": absolute_height,
"color": "red",
"lineWidth": 2,
},
)
return json.dumps(box_data)
Loading