-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/wg 403 create asset geometry component (#295)
- Loading branch information
1 parent
4cad581
commit dc02685
Showing
5 changed files
with
166 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import GeometryAsset from './AssetGeometry'; | ||
import { mockImgFeature } from '@hazmapper/__fixtures__/featuresFixture'; | ||
|
||
jest.mock('@turf/turf', () => ({ | ||
...jest.requireActual('@turf/turf'), | ||
area: jest.fn(() => 1234.56), | ||
length: jest.fn(() => 5678.9), | ||
bbox: jest.fn(() => [10, 20, 30, 40]), | ||
})); | ||
|
||
jest.mock('@hazmapper/hooks', () => ({ | ||
useFeatureSelection: jest.fn(), | ||
})); | ||
|
||
describe('AssetGeometry', () => { | ||
const GeometryAssetProps = { | ||
selectedFeature: mockImgFeature, | ||
}; | ||
|
||
it('renders geometry for point on an image', () => { | ||
const { getByText } = render(<GeometryAsset {...GeometryAssetProps} />); | ||
expect(getByText('Geometry: Point')).toBeDefined(); | ||
expect(getByText('Latitude')).toBeDefined(); | ||
expect(getByText('Longitude')).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React from 'react'; | ||
import _ from 'lodash'; | ||
import * as turf from '@turf/turf'; | ||
import { Feature, FeatureType } from '@hazmapper/types'; | ||
|
||
interface AssetGeometryProps { | ||
selectedFeature: Feature; | ||
} | ||
|
||
const AssetGeometry: React.FC<AssetGeometryProps> = ({ selectedFeature }) => { | ||
if (!selectedFeature?.geometry) return null; | ||
|
||
const bbox = | ||
selectedFeature.geometry.type !== 'Point' | ||
? turf.bbox(selectedFeature) | ||
: null; | ||
|
||
const geometryType = selectedFeature.geometry.type; | ||
|
||
return ( | ||
<> | ||
{geometryType === FeatureType.Point && ( | ||
<table> | ||
<thead> | ||
<tr> | ||
<th className="text-center" colSpan={2}> | ||
Geometry: {_.startCase(selectedFeature.geometry.type)} | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>Latitude</td> | ||
<td>{turf.bbox(selectedFeature.geometry)[0]}</td> | ||
</tr> | ||
<tr> | ||
<td>Longitude</td> | ||
<td>{turf.bbox(selectedFeature.geometry)[1]}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
)} | ||
{(geometryType === FeatureType.Polygon || | ||
geometryType === FeatureType.MultiPolygon) && ( | ||
<table> | ||
<thead> | ||
<tr> | ||
<th className="text-center" colSpan={2}> | ||
Geometry: {_.startCase(selectedFeature.geometry.type)} | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>Area (m²)</td> | ||
<td>{turf.area(selectedFeature as turf.Feature).toFixed(2)}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
)} | ||
{(geometryType === FeatureType.LineString || | ||
geometryType === FeatureType.MultiLineString) && ( | ||
<table> | ||
<thead> | ||
<tr> | ||
<th className="text-center" colSpan={2}> | ||
Geometry: {_.startCase(selectedFeature.geometry.type)} | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>Length (m)</td> | ||
<td>{turf.length(selectedFeature as turf.Feature).toFixed(2)}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
)} | ||
{geometryType !== FeatureType.Point && bbox && ( | ||
<table> | ||
<thead> | ||
{selectedFeature.geometry.type === 'GeometryCollection' && ( | ||
<tr> | ||
<th className="text-center" colSpan={2}> | ||
Geometry: {_.startCase(selectedFeature.geometry.type)} | ||
</th> | ||
</tr> | ||
)} | ||
<tr> | ||
<th colSpan={3} className="text-center"> | ||
Bounding Box | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td></td> | ||
<td>Latitude</td> | ||
<td>Longitude</td> | ||
</tr> | ||
<tr> | ||
<td>Minimum</td> | ||
<td>{turf.bbox(selectedFeature.geometry)[1]}</td> | ||
<td>{turf.bbox(selectedFeature.geometry)[0]}</td> | ||
</tr> | ||
<tr> | ||
<td>Maximum</td> | ||
<td>{turf.bbox(selectedFeature.geometry)[3]}</td> | ||
<td>{turf.bbox(selectedFeature.geometry)[2]}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default AssetGeometry; |