|
1 | 1 | # Metadata
|
| 2 | + |
2 | 3 | ## Meta
|
3 |
| -The `Meta` method provides metadata handling capabilities in GeometryBasics. Similarly to remove the metadata and keep only the geometry, use `metafree`, and for vice versa i.e., remove the geometry and keep the metadata use `meta`. |
4 | 4 |
|
| 5 | +The `Meta` method provides metadata handling capabilities in GeometryBasics. |
| 6 | +Similarly to remove the metadata and keep only the geometry, use `metafree`, and |
| 7 | +for vice versa i.e., remove the geometry and keep the metadata use `meta`. |
5 | 8 |
|
6 | 9 | ### Syntax
|
7 |
| -``` |
| 10 | + |
| 11 | +```julia |
8 | 12 | meta(geometry, meta::NamedTuple)
|
9 | 13 | meta(geometry; meta...)
|
10 | 14 |
|
11 | 15 | metafree(meta-geometry)
|
12 | 16 | meta(meta-geometry)
|
13 |
| -``` |
14 |
| -### Example |
15 |
| -```jldoctest |
16 |
| -using GeometryBasics |
17 |
| -p1 = Point(2.2, 3.6) |
18 |
| -
|
19 |
| -poi = meta(p1, city="Abuja", rainfall=1221.2) |
20 |
| -2-element PointMeta{2,Int64,Point{2,Int64},(:city, :rainfall),Tuple{String,Float64}} with indices SOneTo(2): |
21 |
| - 3 |
22 |
| - 1 |
23 |
| -
|
24 |
| -# metadata is stored in a NamedTuple and can be retrieved as such |
25 |
| -meta(poi) |
| 17 | +``` |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +```jldoctest meta |
| 22 | +julia> using GeometryBasics |
| 23 | +
|
| 24 | +julia> p1 = Point(2.2, 3.6) |
| 25 | +2-element Point{2,Float64} with indices SOneTo(2): |
| 26 | + 2.2 |
| 27 | + 3.6 |
| 28 | +
|
| 29 | +julia> poi = meta(p1, city="Abuja", rainfall=1221.2) |
| 30 | +2-element PointMeta{2,Float64,Point{2,Float64},(:city, :rainfall),Tuple{String,Float64}} with indices SOneTo(2): |
| 31 | + 2.2 |
| 32 | + 3.6 |
| 33 | +``` |
| 34 | + |
| 35 | +Metadata is stored in a NamedTuple and can be retrieved as such |
| 36 | + |
| 37 | +```jldoctest meta |
| 38 | +julia> meta(poi) |
26 | 39 | (city = "Abuja", rainfall = 1221.2)
|
| 40 | +``` |
| 41 | + |
| 42 | +Specific metadata attributes can be directly retrieved |
27 | 43 |
|
28 |
| -# specific metadata attributes can be directly retrieved |
29 |
| -poi.rainfall |
| 44 | +```jldoctest meta |
| 45 | +julia> poi.rainfall |
30 | 46 | 1221.2
|
31 | 47 |
|
32 |
| -metafree(poi) |
33 |
| -2-element Point{2,Int64} with indices SOneTo(2): |
34 |
| - 3 |
35 |
| - 1 |
| 48 | +julia> metafree(poi) |
| 49 | +2-element Point{2,Float64} with indices SOneTo(2): |
| 50 | + 2.2 |
| 51 | + 3.6 |
| 52 | +``` |
| 53 | + |
| 54 | +For other geometries metatypes are predefined |
36 | 55 |
|
37 |
| -# for other geometries metatypes are predefined |
38 |
| -multipoi = MultiPointMeta([p1], city="Abuja", rainfall=1221.2) |
39 |
| -1-element MultiPointMeta{Point{2,Int64},MultiPoint{2,Int64,Point{2,Int64},Array{Point{2,Int64},1}},(:city, :rainfall),Tuple{String,Float64}}: |
40 |
| -[3, 1] |
| 56 | +```jldoctest meta |
| 57 | +julia> multipoi = MultiPointMeta([p1], city="Abuja", rainfall=1221.2) |
| 58 | +1-element MultiPointMeta{Point{2,Float64},MultiPoint{2,Float64,Point{2,Float64},Array{Point{2,Float64},1}},(:city, :rainfall),Tuple{String,Float64}}: |
| 59 | + [2.2, 3.6] |
41 | 60 | ```
|
42 | 61 | In the above example we have also used geometry specific meta methods.
|
43 | 62 |
|
44 |
| -### Example |
45 |
| -```@jldoctest |
46 |
| -GeometryBasics.MetaType(Polygon) |
| 63 | + |
| 64 | +```jldoctest meta |
| 65 | +julia> GeometryBasics.MetaType(Polygon) |
47 | 66 | PolygonMeta
|
48 | 67 |
|
49 |
| -GeometryBasics.MetaType(Mesh) |
| 68 | +julia> GeometryBasics.MetaType(Mesh) |
50 | 69 | MeshMeta
|
51 | 70 | ```
|
52 | 71 | The metageometry objects are infact composed of the original geometry types.
|
53 |
| -```@jldoctest |
54 |
| -GeometryBasics.MetaFree(PolygonMeta) |
| 72 | + |
| 73 | +```jldoctest meta |
| 74 | +julia> GeometryBasics.MetaFree(PolygonMeta) |
55 | 75 | Polygon
|
56 | 76 |
|
57 |
| -GeometryBasics.MetaFree(MeshMeta) |
| 77 | +julia> GeometryBasics.MetaFree(MeshMeta) |
58 | 78 | Mesh
|
59 | 79 | ```
|
| 80 | + |
60 | 81 | ## MetaT
|
61 |
| -In GeometryBasics we can a have tabular layout for a collection of meta-geometries by putting them into a StructArray that extends the [Tables.jl](https://github.com/JuliaData/Tables.jl) API. |
62 | 82 |
|
63 |
| -In practice it's not necessary for the geometry or metadata types to be consistent. Eg: A geojson format can have heterogeneous geometries. |
64 |
| -Hence, such cases require automatic widening of the geometry data types to the most appropriate type. The MetaT method works around the fact that, a collection of geometries and metadata of different types can be represented tabularly whilst widening to the appropriate type. |
| 83 | +In GeometryBasics we can a have tabular layout for a collection of meta-geometries |
| 84 | +by putting them into a StructArray that extends the [Tables.jl](https://github.com/JuliaData/Tables.jl) API. |
| 85 | + |
| 86 | +In practice it's not necessary for the geometry or metadata types to be consistent. |
| 87 | +For example, a geojson format can have heterogeneous geometries. Hence, such cases require |
| 88 | +automatic widening of the geometry data types to the most appropriate type. |
| 89 | +The MetaT method works around the fact that, a collection of geometries and metadata |
| 90 | +of different types can be represented tabularly whilst widening to the appropriate type. |
| 91 | + |
65 | 92 | ### Syntax
|
66 |
| -``` |
| 93 | + |
| 94 | +```julia |
67 | 95 | MetaT(geometry, meta::NamedTuple)
|
68 | 96 | MetaT(geometry; meta...)
|
69 |
| -``` |
| 97 | +``` |
70 | 98 | Returns a `MetaT` that holds a geometry and its metadata `MetaT` acts the same as `Meta` method.
|
71 | 99 | The difference lies in the fact that it is designed to handle geometries and metadata of different/heterogeneous types.
|
72 | 100 |
|
73 |
| -eg: While a Point MetaGeometry is a `PointMeta`, the MetaT representation is `MetaT{Point}` |
| 101 | +For example, while a Point MetaGeometry is a `PointMeta`, the MetaT representation is `MetaT{Point}`. |
| 102 | + |
| 103 | +### Examples |
74 | 104 |
|
75 |
| -### Example |
76 |
| -```@jldoctest |
77 |
| -MetaT(Point(1, 2), city = "Mumbai") |
| 105 | +```jldoctest meta |
| 106 | +julia> MetaT(Point(1, 2), city = "Mumbai") |
78 | 107 | MetaT{Point{2,Int64},(:city,),Tuple{String}}([1, 2], (city = "Mumbai",))
|
79 | 108 | ```
|
80 | 109 |
|
81 | 110 | For a tabular representation, an iterable of `MetaT` types can be passed on to a `metatable` method.
|
82 | 111 |
|
83 | 112 | ### Syntax
|
84 |
| -```@jldoctest |
| 113 | + |
| 114 | +```julia |
85 | 115 | meta_table(iter)
|
86 |
| -``` |
87 |
| -### Example |
88 |
| -```@jldoctest |
89 |
| -using DataFrames |
90 |
| -# Create an array of 2 linestrings |
91 |
| -ls = [LineString([Point(i, i+1), Point(i-1,i+5)]) for i in 1:2] |
| 116 | +``` |
| 117 | + |
| 118 | +### Examples |
| 119 | + |
| 120 | + Create an array of 2 linestrings: |
| 121 | + |
| 122 | +```jldoctest meta |
| 123 | +julia> ls = [LineString([Point(i, i+1), Point(i-1,i+5)]) for i in 1:2]; |
| 124 | +
|
| 125 | +julia> coordinates.(ls) |
| 126 | +2-element Array{Array{Point{2,Int64},1},1}: |
| 127 | + [[1, 2], [0, 6]] |
| 128 | + [[2, 3], [1, 7]] |
| 129 | +``` |
| 130 | + |
| 131 | +Create a multi-linestring: |
| 132 | + |
| 133 | +```jldoctest meta |
| 134 | +julia> mls = MultiLineString(ls); |
92 | 135 |
|
93 |
| -# Create a MultiLineString |
94 |
| -mls = MultiLineString(ls) |
| 136 | +julia> coordinates.(mls) |
| 137 | +2-element Array{Array{Point{2,Int64},1},1}: |
| 138 | + [[1, 2], [0, 6]] |
| 139 | + [[2, 3], [1, 7]] |
| 140 | +``` |
| 141 | + |
| 142 | +Create a polygon: |
95 | 143 |
|
96 |
| -# Create a Polygon |
97 |
| -poly = Polygon(Point{2, Int}[(40, 40), (20, 45), (45, 30), (40, 40)]) |
| 144 | +```jldoctest meta |
| 145 | +julia> poly = Polygon(Point{2, Int}[(40, 40), (20, 45), (45, 30), (40, 40)]); |
98 | 146 |
|
99 |
| -# Put all of it in an Array |
100 |
| -geom = [ls..., mls, poly] |
101 |
| - |
102 |
| -# Generate some random metadata |
103 |
| -prop = [(country_states = "India$(i)", rainfall = (i*9)/2) for i in 1:4] |
104 |
| - |
105 |
| -# Create an Array of MetaT |
106 |
| -feat = [MetaT(i, j) for (i,j) = zip(geom, prop)] |
| 147 | +julia> coordinates(poly) |
| 148 | +4-element Array{Point{2,Int64},1}: |
| 149 | + [40, 40] |
| 150 | + [20, 45] |
| 151 | + [45, 30] |
| 152 | + [40, 40] |
| 153 | +``` |
107 | 154 |
|
108 |
| -# Generate a StructArray/Table |
109 |
| -sa = meta_table(feat) |
| 155 | +Put all geometries into an array: |
110 | 156 |
|
111 |
| -sa.main |
112 |
| -sa.country_states |
113 |
| -sa.rainfall |
| 157 | +```jldoctest meta |
| 158 | +julia> geom = [ls..., mls, poly]; |
114 | 159 | ```
|
115 | 160 |
|
116 |
| -### Disadvantages: |
117 |
| - * The MetaT is pretty generic in terms of geometry types, it's not subtype to geometries. eg : A `MetaT{Point, NamedTuple{Names, Types}}` is not subtyped to `AbstractPoint` like a `PointMeta` is. |
118 |
| - * This might cause problems on using `MetaT` with other constructors/methods inside or even outside GeometryBasics methods designed to work with the main `Meta` types. |
| 161 | +:Generate some random metadata: |
| 162 | + |
| 163 | +```jldoctest meta |
| 164 | +julia> prop = [(country_states = "India$(i)", rainfall = (i*9)/2) for i in 1:4] |
| 165 | +4-element Array{NamedTuple{(:country_states, :rainfall),Tuple{String,Float64}},1}: |
| 166 | + (country_states = "India1", rainfall = 4.5) |
| 167 | + (country_states = "India2", rainfall = 9.0) |
| 168 | + (country_states = "India3", rainfall = 13.5) |
| 169 | + (country_states = "India4", rainfall = 18.0) |
| 170 | +
|
| 171 | +julia> feat = [MetaT(i, j) for (i,j) = zip(geom, prop)]; # create an array of MetaT |
| 172 | +``` |
| 173 | + |
| 174 | +We can now generate a `StructArray` / `Table` with `meta_table`. Fields are accessed with `sa.main`, `sa.country_states`, `sa.rainfall`. |
| 175 | + |
| 176 | +```jldoctest meta |
| 177 | +julia> sa = meta_table(feat); |
| 178 | +``` |
| 179 | + |
| 180 | +### Disadvantages |
| 181 | + |
| 182 | + * The MetaT is pretty generic in terms of geometry types, it's not subtype to |
| 183 | + geometries. eg : A `MetaT{Point, NamedTuple{Names, Types}}` is not subtyped to |
| 184 | + `AbstractPoint` like a `PointMeta` is. |
119 | 185 |
|
| 186 | + * This might cause problems on using `MetaT` with other constructors/methods |
| 187 | + inside or even outside GeometryBasics methods designed to work with the main `Meta` types. |
0 commit comments