Subscribe to this thread
Home - General / All posts - Computed fields like M8 intrinsic fields
rk
621 post(s)
#28-Nov-20 23:42

I tried to recreate M8 intrinsic fields in M9. Could be useful sometimes.

--SQL9

ALTER TABLE [T1] (

  ADD [Type (I)] VARCHAR AS [[ 

  CASE 

    WHEN GeomType([Geom]) = 1 THEN 'point' 

    WHEN GeomType([Geom]) = 2 THEN 'line' 

    WHEN GeomType([Geom]) = 3 THEN 'area' 

    ELSE 'Unknown' 

  END

   ]]

);

ALTER TABLE [T1] (

  ADD [Branches (I)] INT32 AS [[ GeomBranchCount([Geom]) ]]

);

ALTER TABLE [T1] (

  ADD [Coordinates (9)] INT32 AS [[ GeomCoordCount([Geom]) ]] 

);

ALTER TABLE [T1] (

  ADD [Coordinates (I)] INT32

    AS [[ CASE WHEN GeomType([Geom]) = 3 THEN GeomCoordCount([Geom]) - [Branches (I)] ELSE GeomCoordCount([Geom]END ]]

);

ALTER TABLE [T1] (

  ADD  [X (I)] FLOAT64

    AS [[ VectorValue(GeomCenter([Geom], 0), 0) ]]

);

ALTER TABLE [T1] (

  ADD   [Y (I)] FLOAT64

    AS [[ VectorValue(GeomCenter([Geom], 0), 1) ]]

);

ALTER TABLE [T1] (

  ADD   [Latitude (I)] FLOAT64

    WITH [[ VALUE @converter TABLE = CALL CoordConverterMake(CoordSystemDefaultLatLon(), ComponentCoordSystem([Drawing])); ]]

    AS [[ VectorValue(GeomCenter(CoordConvert(@converter, [Geom]), 0), 1) ]]

);

ALTER TABLE [T1] (

  ADD   [Longitude (I)] FLOAT64

    WITH [[ VALUE @converter TABLE = CALL CoordConverterMake(CoordSystemDefaultLatLon(), ComponentCoordSystem([Drawing])); ]]

    AS [[ VectorValue(GeomCenter(CoordConvert(@converter, [Geom]), 0), 0) ]]

);

ALTER TABLE [T1] (

  ADD [Bearing (I)] FLOAT64 

  WITH 

    [[ 

    VALUE @system NVARCHAR = ComponentFieldCoordSystem([T1]'Geom');

    VALUE @measure TABLE = CALL CoordMeasureMake(@system, 'Meter', FALSE);

    VALUE @unitDeg FLOAT64 = CoordUnitScale(CoordUnitByName('Degree'));

    ]] 

  AS [[ CoordMeasureBearing(@measure, [Geom]) / @unitDeg ]]

);

ALTER TABLE [T1] (

  ADD [Area (I)] FLOAT64

    WITH 

    [[ 

    VALUE @system NVARCHAR = ComponentFieldCoordSystem([T1]'Geom');

    VALUE @measure TABLE = CALL CoordMeasureMake(@system, 'Meter', FALSE); 

    ]]

    AS [[ CoordMeasureArea(@measure, [Geom]) ]]

);

ALTER TABLE [T1] (

  ADD [Length (m)] FLOAT64

    WITH 

    [[ 

    VALUE @system NVARCHAR = ComponentFieldCoordSystem([T1]'Geom');

    VALUE @measure TABLE = CALL CoordMeasureMake(@system, 'Meter', FALSE); 

    ]]

    AS [[ CoordMeasureLength(@measure, [Geom]) ]]

);

tjhb
10,094 post(s)
#28-Nov-20 23:51

Fantastic resource Riivo. Beautifully succinct code. Shows how to do much more. Just great, an example to follow. (Will try.)

tjhb
10,094 post(s)
#29-Nov-20 00:26

There is one thing I don’t agree with though—your use of Manifold 8 names for fields (columns).

That’s not necessary and (arguably) not helpful.

In particular, the “(I)” suffix is less than useful for 9.

I see why though—familiarity is good (and searchable).

But it is contrary to meaning, for 9.

rk
621 post(s)
#29-Nov-20 01:20

I agree.

I used the exact names as a reminder to get exactly the same values.

Actually my [Bearing] (+-180) does not deserve the (I) 0-360

[Length (I)] should probably use CoordUnit... as second argument . I wrote [Length (m)]

tjhb
10,094 post(s)
#29-Nov-20 02:50

Excellent resource, all in one place, searchable.

Not many people would know in advance how simple this is (and flexible).

(My favourite is how tight you managed to get Bearing [I].)

Sloots

678 post(s)
#29-Nov-20 12:19

Very Nice!

P.S. Not a very interesting comment, but hopefully it contributes to the feeling that this forum is alive and kicking!


http://www.mppng.nl/manifold/pointlabeler

adamw


10,447 post(s)
#30-Nov-20 13:16

Great!

We have been thinking about adding some means to add computed fields like that interactively in the Schema dialog - have a couple of templates and allow using them without coding.

We have also been thinking about adding a VALUE that represents the current table. Eg, @self. This way, you'd be able to write: VALUE @system NVARCHAR = ComponentFieldCoordSystem(@self, 'Geom') -- better than citing the name of the table, because now you can, say, copy and paste the table and everything work as expected.

Also, it is important to understand the limitations of computed fields constructed this way. Say, you have a drawing and add Latitude (I) / Longitude (I). They will populate with the values, OK, fine. If you now change the coordinate system, however, the values in the computed fields will not be recomputed. This is by design, computed fields only track changes to referenced fields. There is a hack though: you can overwrite the geom values and this will force Latitude (I) / Longitude (I) to recompute. Eg, scale Geom by XY = { -1,-1 } (to keep coordinate values the exact same and only change their sign), then scale them back by the same XY.

Manifold User Community Use Agreement Copyright (C) 2007-2021 Manifold Software Limited. All rights reserved.