-
Notifications
You must be signed in to change notification settings - Fork 2
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
Task/wg 403 create asset geometry component #295
Changes from 7 commits
6470650
a4f228f
f2d0e9b
bc85e3d
4bd92c9
6d82ec5
efa3dee
e327f6a
01d2686
4989ee7
d4344a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
}); | ||
}); |
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, getFeatureType } 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: FeatureType = getFeatureType(selectedFeature); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you want to A) keep the variable name and then comment that you are considering only geometry types (subset of FeatureType). 🤔 or maybe: B) there is a difference currently with angular version, as here if its an something like an image or video feature, we don't show point coordinates (as its featureType would be (good tests by the way as covers this case well 💯 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I think B works well. This is updated with the latest push |
||
|
||
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things taken out here were styles that were not being applied by the browser