Skip to content

Commit

Permalink
using geojson lib
Browse files Browse the repository at this point in the history
  • Loading branch information
nanli-emory committed Dec 12, 2024
1 parent bbcb89c commit 82acf1d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 51 deletions.
67 changes: 17 additions & 50 deletions histoqc/SaveModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from distutils.util import strtobool
from skimage import color, measure
from copy import deepcopy
from geojson import Polygon, Feature, FeatureCollection, dump

def blend2Images(img, mask):
if (img.ndim == 3):
Expand All @@ -19,34 +20,6 @@ def blend2Images(img, mask):
out = np.concatenate((mask, img, mask), 2)
return out


'''
the followings are helper functions for generating geojson
'''

feature_template = {
"type": "Feature",
"id": None,
"geometry": {
"type": None,
"coordinates": []
},
"properties": {
"objectType": "annotation"
}
}

feature_collection_template = {
"type": "FeatureCollection",
"features": []
}







def binaryMask2Geojson(s, mask):
# get the dimension of slide
(dim_width, dim_height) = s['os_handle'].dimensions
Expand Down Expand Up @@ -75,38 +48,32 @@ def binaryMask2Geojson(s, mask):
s["warnings"].append(msg)
return None

# copy feature collection template in geojson
feature_collection = deepcopy(feature_collection_template)
features = []
for i, contour in enumerate(contours):
first_child_idx = hierarchy[0, i, 2]
parent_idx = hierarchy[0, i, 3]

if (parent_idx != -1):
continue

# copy feature template in geojson
new_feature = deepcopy(feature_template)
# set id
new_feature["id"] = uuid.uuid4().hex
# scale up the coordinate
# points = np.asarray(np.flip(contour / [mask_height, mask_width] * [dim_height, dim_width]),dtype="int")

geometry = []
points = np.asarray(contour / [mask_height, mask_width] * [dim_height, dim_width],dtype="int")
points = np.append(points, [points[0]], axis=0)
points = points[:,0]

if first_child_idx == -1:
new_feature['geometry']['type'] = 'Polygon'
new_feature['geometry']['coordinates'].append(points.tolist())
else:
new_feature['geometry']['type'] = 'MultiPolygon'
new_feature['geometry']['coordinates'].append([points.tolist()])
points = points[:,0].tolist()
points = [tuple(p) for p in points]
geometry.append(points)
if first_child_idx != -1:
for child in children[i]:
child_points = np.asarray(child / [mask_height, mask_width] * [dim_height, dim_width],dtype="int")
child_points = np.append(child_points, [child_points[0]], axis=0)
child_points = child_points[:,0]
new_feature['geometry']['coordinates'].append([child_points.tolist()])
feature_collection['features'].append(new_feature)

child_points = child_points[:,0].tolist()
child_points = [tuple(p) for p in child_points]
geometry.append(child_points)
new_feature = Feature(id=uuid.uuid4().hex, geometry=Polygon(geometry),properties={"objectType": "annotation"})

features.append(new_feature)
feature_collection = FeatureCollection(features)

return feature_collection


Expand Down Expand Up @@ -193,7 +160,7 @@ def saveMask2Geojson(s, params):

# save mask as genjson file
with open(f"{s['outdir']}{os.sep}{s['filename']}_{suffix}.geojson", 'w') as f:
json.dump(geojson, f)
dump(geojson, f)



Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Pillow~=9.4.0
setuptools~=65.6.3
shapely~=2.0.1
flask~=2.3.2

geojson~=3.1.0
cohortfinder==1.0.1

0 comments on commit 82acf1d

Please sign in to comment.