active_record_mysql_spatial 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52dc505e977c105df50ab13b4c4bf1363d778ac41479b0ff578632c4d627a7a4
|
4
|
+
data.tar.gz: de9e452572739eeb2634e8ef919ba4683038b05f3fb9ad47bcd1435c6d036c0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3888cef99591d42373885f9f7c06913f0226a2df5eb5f2949f102565f2660a7a76b14896f6148aa881de5b4640ab6bd8d8842247d7e91f34b4cfc63cb81aca4
|
7
|
+
data.tar.gz: 8320501949446a2e6a316b4fac74d1bf026db27e23ec8c65808e5fd3e4766119205fc69c5755a516c04a1111d3f5b15f5e805f2d8b01642a8d0e60daac7e04c6
|
data/README.md
CHANGED
@@ -1,35 +1,102 @@
|
|
1
|
-
|
1
|
+
ActiveRecord extension for MySQL Spatial Data.
|
2
2
|
|
3
|
-
|
3
|
+
# Installation
|
4
4
|
|
5
|
-
|
5
|
+
```sh
|
6
|
+
gem 'active_record_mysql_spatial'
|
7
|
+
```
|
6
8
|
|
7
|
-
|
9
|
+
# Migration
|
8
10
|
|
9
|
-
|
11
|
+
This extension provides some data type methods for ActiveRecord migration and also register those types to the schema.
|
10
12
|
|
11
|
-
|
13
|
+
```rb
|
14
|
+
# frozen_string_literal: true
|
12
15
|
|
13
|
-
|
16
|
+
class CreatePositions < ActiveRecord::Migration[7.2]
|
17
|
+
def change
|
18
|
+
create_table :positions do |t|
|
19
|
+
t.linestring :ls
|
20
|
+
t.multilinestring :mls
|
21
|
+
t.point :pt
|
14
22
|
|
15
|
-
|
23
|
+
t.column :tls, :linestring
|
24
|
+
t.column :tmls, :multilinestring
|
25
|
+
t.column :tpt, :point
|
16
26
|
|
17
|
-
|
27
|
+
t.timestamps
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
18
32
|
|
19
|
-
|
33
|
+
Without this extension, even you use `t.column` to define the column for the table, you will receive the error message in the `schema.rb`
|
20
34
|
|
21
|
-
|
35
|
+
```rb
|
36
|
+
# Could not dump table "positions" because of following StandardError
|
37
|
+
# Unknown type 'linestring' for column 'tls'
|
38
|
+
```
|
22
39
|
|
23
|
-
|
40
|
+
# Usage
|
24
41
|
|
25
|
-
|
42
|
+
## Point
|
26
43
|
|
27
|
-
|
44
|
+
```rb
|
45
|
+
position = Position.create!(pt: { x: 1, y: 2 })
|
28
46
|
|
29
|
-
|
47
|
+
position = Position.create!(pt: [1, 2])
|
30
48
|
|
31
|
-
|
49
|
+
p position.pt.x # puts x
|
50
|
+
p position.pt.y # puts y
|
51
|
+
```
|
32
52
|
|
33
|
-
##
|
53
|
+
## Linestring
|
34
54
|
|
35
|
-
|
55
|
+
```rb
|
56
|
+
position = Position.create!(ls: [[1, 2], [2, 3]])
|
57
|
+
|
58
|
+
p position.ls.coordinates # puts all points
|
59
|
+
p position.ls.coordinates.first.x # puts x of first point
|
60
|
+
p position.ls.coordinates.last.y # puts y of last point
|
61
|
+
```
|
62
|
+
|
63
|
+
## Multilinestring
|
64
|
+
|
65
|
+
```rb
|
66
|
+
position = Position.create!(mls: [[[1, 2], [2, 3]]])
|
67
|
+
|
68
|
+
p position.mls.items # puts all linestrings
|
69
|
+
p position.mls.items.first.coordinates.first.x # puts x of first point of first linestring
|
70
|
+
p position.mls.items.last.coordinates.last.y # puts y of last point of last linestring
|
71
|
+
```
|
72
|
+
|
73
|
+
# Custom result class
|
74
|
+
|
75
|
+
In your business, you may need to use spatial data for a purpose. To load data and map to the semantic data for your business, create a class and override the method `cast_value`.
|
76
|
+
|
77
|
+
```rb
|
78
|
+
class YourClass < ActiveRecordMysqlSpatial::ActiveRecord::MySQL::Linestring
|
79
|
+
attr_reader :sum_x, :sum_y
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def cast_value(value)
|
84
|
+
super
|
85
|
+
|
86
|
+
@sum_x, @sum_y = @coordinates.reduce([0, 0]) do |sum, point|
|
87
|
+
sum[0] += point.x.to_i
|
88
|
+
sum[1] += point.y.to_i
|
89
|
+
sum
|
90
|
+
end
|
91
|
+
|
92
|
+
self
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# models/position.rb
|
97
|
+
class Position < ApplicationRecord
|
98
|
+
include ActiveRecordMysqlSpatial::ActsAsSpatial
|
99
|
+
|
100
|
+
acts_as_linestring :ls, serializer: YourClass
|
101
|
+
end
|
102
|
+
```
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module ActiveRecordMysqlSpatial
|
6
|
+
module ActsAsSpatial
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def acts_as_linestring(*columns, serializer: nil)
|
11
|
+
return if serializer.blank?
|
12
|
+
|
13
|
+
columns.each do |col|
|
14
|
+
attribute(col, serializer.new)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method :acts_as_point, :acts_as_linestring
|
19
|
+
alias_method :acts_as_multilinestring, :acts_as_linestring
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -9,6 +9,7 @@ require_relative 'active_record_mysql_spatial/active_record/mysql/point'
|
|
9
9
|
require_relative 'active_record_mysql_spatial/active_record/native_types'
|
10
10
|
require_relative 'active_record_mysql_spatial/active_record/quoting'
|
11
11
|
require_relative 'active_record_mysql_spatial/active_record/register_types'
|
12
|
+
require_relative 'active_record_mysql_spatial/acts_as_spatial'
|
12
13
|
require_relative 'active_record_mysql_spatial/geometry'
|
13
14
|
require_relative 'active_record_mysql_spatial/version'
|
14
15
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_mysql_spatial
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alpha
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- lib/active_record_mysql_spatial/active_record/native_types.rb
|
50
50
|
- lib/active_record_mysql_spatial/active_record/quoting.rb
|
51
51
|
- lib/active_record_mysql_spatial/active_record/register_types.rb
|
52
|
+
- lib/active_record_mysql_spatial/acts_as_spatial.rb
|
52
53
|
- lib/active_record_mysql_spatial/geometry.rb
|
53
54
|
- lib/active_record_mysql_spatial/version.rb
|
54
55
|
homepage: https://github.com/zgid123/active_record_mysql_spatial
|