Slide 1

Slide 1 text

ۭؒ৘ใ୳ࡧجૅ࿦ YAPC::Asia 2015 Mobile Factory Lunch Session

Slide 2

Slide 2 text

About me • kobaken (@kfly8) • Mobile Factory • Senior Engineer • like Perl, JS, Math • YAPC::Asia Staff (2013 - )

Slide 3

Slide 3 text

͝͸Μඒຯ͍͠Ͱ͔͢..ʁ

Slide 4

Slide 4 text

ൃ஫ͨ͠ਓ ↓

Slide 5

Slide 5 text

ඒຯ͔ͬͨ͠Β ౤ථ͍ͯͩ͘͠͞ʂ

Slide 6

Slide 6 text

CM

Slide 7

Slide 7 text

͜ͷϥϯνηογϣϯ͸ ͷఏڙͰ͓ૹΓ͠·͢

Slide 8

Slide 8 text

About Mobile Factory • ޒ൓ాͷWebܥITاۀ • ࠷্ۙ৔ͨ͠! • ओʹϞόΠϧήʔϜ΍ΞϓϦͷ։ൃ • ࣗ෼͸ීஈҐஔ৘ใήʔϜͷ։ൃ

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Ґஔ৘ใήʔϜͰ ར༻͍ͯ͠Δٕज़Λ جૅతͳ࿩͔Β͠·͢

Slide 11

Slide 11 text

Agenda • Ґஔ৘ใѻ͍࣌ͷΑ͋͘Δ୳ࡧͷཁ݅ • ֨ࢠ্ͰͷϥϯΫ෇͚ • ஍ਤ্ͰͷϥϯΫ෇͚

Slide 12

Slide 12 text

Α͋͘Δཁ݅

Slide 13

Slide 13 text

Α͋͘Δཁ݅ • ݱࡏ஍͔Βɺ࠷୹ڑ཭ʹ͋ΔӺ͸Ͳ͔͜ʁ

Slide 14

Slide 14 text

Α͋͘Δཁ݅ • ݱࡏ஍͔Βɺ࠷୹ڑ཭ʹ͋ΔӺ͸Ͳ͔͜ʁ • ޒ൓ాӺपล̏Ωϩݍ಺ͷITاۀ͸Ͳ͜ʁ

Slide 15

Slide 15 text

Α͋͘Δཁ݅ • ݱࡏ஍͔Βɺ࠷୹ڑ཭ʹ͋ΔӺ͸Ͳ͔͜ʁ • ޒ൓ాӺपล̏Ωϩݍ಺ͷITاۀ͸Ͳ͜ʁ

Slide 16

Slide 16 text

গ͠࿩Λ਺ֶΒ͘͢͠Δͱ…

Slide 17

Slide 17 text

ɹɹɹ ɹɹA B ɹ ɹɹC ɹɹ ɹɹɹD P

Slide 18

Slide 18 text

ɹɹɹ ɹɹA B ɹ ɹɹC ɹɹ ɹɹɹD P ఺P͔Β΋ͬͱ΋͍ۙ఺͸ʁ

Slide 19

Slide 19 text

A B C D P ఺P͔Β΋ͬͱ΋͍ۙ఺͸ʁ ڑ཭ a = ఺A - ఺P ڑ཭ b = ఺B - ఺P ڑ཭ c = ఺C - ఺P ڑ཭ d = ఺D - ఺P shift sort { $a <=> $b } (a,b,c,d)

Slide 20

Slide 20 text

ۙ๣ͷ໰͍߹Θͤ໰୊ʹͳΔ

Slide 21

Slide 21 text

ۭؒ৘ใʹ͓͚Δ ۙ๣ͷ໰͍߹Θͤ໰୊ͷಛ௃ • ۭؒʹ഑ஔ͞ΕΔ఺ʢྖҬʣͷදݱ͕Ϧετ • ྫ: (x1,y1, … z1) , (x2,y2, …, z2)

Slide 22

Slide 22 text

ۭؒ৘ใʹ͓͚Δ ۙ๣ͷ໰͍߹Θͤ໰୊ͷಛ௃ • ۭؒʹ഑ஔ͞ΕΔ఺ʢྖҬʣͷදݱ͕Ϧετ • ྫ: (x1,y1, … z1) , (x2,y2, …, z2) • ج఺͕มΘΔ • ౎౓ɺڑ཭ܭࢉ͢ΔΘ͚ʹ͸͍͔ͳ͍

Slide 23

Slide 23 text

ࣅͨྫ໰୊ ࣗ෼ͷϥϯΫʹ͍ۙਓΛ10݅औΓग़͢

Slide 24

Slide 24 text

ࣅͨྫ໰୊ ࣗ෼ͷϥϯΫʹ͍ۙਓΛ10݅औΓग़͢ ఺ͷදݱ͕ɺ̍ཁૉͷϦετ

Slide 25

Slide 25 text

ࣅͨྫ໰୊ ࣗ෼ͷϥϯΫʹ͍ۙਓΛ10݅औΓग़͢ ج఺͕มΘΔ ఺ͷදݱ͕ɺ̍ཁૉͷϦετ

Slide 26

Slide 26 text

ۙ๣ͷ໰͍߹Θͤ໰୊Λղ͘

Slide 27

Slide 27 text

؆୯ͳ໰୊͔Βղ͘

Slide 28

Slide 28 text

Question. ࣗ෼ͷϥϯΫʹ͍ۙ10݅͸ʁ

Slide 29

Slide 29 text

my @ranks = SELECT * FROM ranking WHERE rank BETWEEN my.rank - 10 AND my.rank + 10 ORDER BY rank;

Slide 30

Slide 30 text

sort { abs($a->score - $my->score) <=> abs($b->score - $my->score) } @ranks;

Slide 31

Slide 31 text

sort { abs($a->score - $my->score) <=> abs($b->score - $my->score) } @ranks; ީิΛऔΓग़͠ߜΓࠐΉ

Slide 32

Slide 32 text

̎࣍ݩҎ্ͷۭؒͰ΋ ͜ͷΑ͏ͳ୳ࡧ΁ ؼணͤ͞Δ

Slide 33

Slide 33 text

࡯ͨ͠ਓ͸͜ΜͳࣄΛߟ͑Δʁ my $hash = murmur_hash( $json->encode([lat, lng]) );

Slide 34

Slide 34 text

΋͏গ͠޻෉͍ͨ͠ • ୯७ʹҢ౓ܦ౓ʹɺHashؔ਺Λ͔͚Δͱ̍఺ Ͱͷ৘ใ͸ಘΒΕΔ͕ɺۙ๣ͷ৘ใ͸ࣦΘΕ ͍ͯΔ • ఺ಉ͕࢜ۙ͘ʹ͋Δ͔ʁͱ͍ͬͨ໰͍߹Θ w w ͤʹ౴͑ΒΕΔϥϯΫ෇͚͕ྑ͍

Slide 35

Slide 35 text

Ґஔ৘ใʹ͓͚Δݕࡧํ๏ • ࠲ඪͷϥϯΫ෇͚ͷྫ • Ң౓ܦ౓ͷ࠾൪ • GeoHex, GeoHash, Open Location Code • ࠷খ֎઀୹ܗ(MBR) ͱ R-Tree

Slide 36

Slide 36 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 2 x 2 ۭؒͰͷϥϯΫ෇͚

Slide 37

Slide 37 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 2 x 2 ۭؒͰͷϥϯΫ෇͚ • (0,0) -> 00 -> 0

Slide 38

Slide 38 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 2 x 2 ۭؒͰͷϥϯΫ෇͚ • (0,0) -> 00 -> 0 • (0,1) -> 01 -> 1

Slide 39

Slide 39 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 2 x 2 ۭؒͰͷϥϯΫ෇͚ • (0,0) -> 00 -> 0 • (0,1) -> 01 -> 1 • (1,0) -> 10 -> 2

Slide 40

Slide 40 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 2 x 2 ۭؒͰͷϥϯΫ෇͚ • (0,0) -> 00 -> 0 • (0,1) -> 01 -> 1 • (1,0) -> 10 -> 2 • (1,1) -> 11 -> 3

Slide 41

Slide 41 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 2 x 2 ۭؒͰͷϥϯΫ෇͚ • (0,0) -> 00 -> 0 • (0,1) -> 01 -> 1 • (1,0) -> 10 -> 2 • (1,1) -> 11 -> 3 1 3 2 0

Slide 42

Slide 42 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 2 x 2 ۭؒͰͷϥϯΫ෇͚ • (0,0) -> 00 -> 0 • (0,1) -> 01 -> 1 • (1,0) -> 10 -> 2 • (1,1) -> 11 -> 3 1 3 2 0 ࠲ඪΛ ̎ਐ਺දݱͱΈͳͯ͠ ͞Βʹ̍̌ਐ਺දݱ͢Δ

Slide 43

Slide 43 text

࠲ඪͷϥϯΫؔ਺ͷྫ Q. (1,0) ͔Β ϥϯΫ1Ҏ಺ͷ࠲ඪ͸? 1 3 2 0

Slide 44

Slide 44 text

࠲ඪͷϥϯΫؔ਺ͷྫ Q. (1,0) ͔Β ϥϯΫ1Ҏ಺ͷ࠲ඪ͸? A. (1,0) -> 10 -> 2. SELECT * FROM cord WHERE rank BETWEEN 1 AND 3 1 3 2 0

Slide 45

Slide 45 text

࠲ඪͷϥϯΫؔ਺ͷྫ Q. (1,0) ͔Β ϥϯΫ1Ҏ಺ͷ࠲ඪ͸? A. (1,0) -> 10 -> 2. SELECT * FROM cord WHERE rank BETWEEN 1 AND 3 1 3 2 0

Slide 46

Slide 46 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 3 x 3 ۭؒͷ৔߹ 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2

Slide 47

Slide 47 text

࠲ඪͷϥϯΫؔ਺ͷྫ • 3 x 3 ۭؒͷ৔߹ 0 1 2 3 4 5 6 7 8 ࠲ඪΛ ̏ਐ਺දݱͱΈͳͯ͠ ̍̌ਐ਺දݱ͢Δ

Slide 48

Slide 48 text

࠲ඪͷϥϯΫؔ਺ͷྫ • N x N ۭؒͷ৔߹ • Nਐ਺දهΛ10ਐ਺දه

Slide 49

Slide 49 text

࠲ඪͷϥϯΫؔ਺ͷྫ • N x N ۭؒͷ৔߹ • Nਐ਺දهΛ10ਐ਺දه • ͦͷଞͷϥϯΫؔ਺ͷྫ • Z-ordering

Slide 50

Slide 50 text

ࣗવ਺ͷϦετͷ ϥϯΫ෇͚͸Ͱ͖ͨ

Slide 51

Slide 51 text

͔͠͠

Slide 52

Slide 52 text

Ң౓ܦ౓ͷදݱ͸ίϨ ౦ژϏοΫαΠτ (latitude,longtitude) = (35.6295764, 139.794686)

Slide 53

Slide 53 text

Ң౓ܦ౓ͷදݱ͸ίϨ

Slide 54

Slide 54 text

Ң౓ܦ౓Λ ΋͏গࣗ͠વ਺ʹ ͍ۙܗʹ͢Δ

Slide 55

Slide 55 text

ͭ·ΓҢ౓ܦ౓ͷ EncodeΛ͍ͨ͠

Slide 56

Slide 56 text

Ң౓ܦ౓ͷ Encode ͢Δಓ۩ • GeoHex (v1,v2, v3) • GeoHash • Open Locatin Code https://github.com/google/open-location-code/ blob/master/docs/comparison.adoc

Slide 57

Slide 57 text

GeoHex v3 • created by @sa2da • MIT License • http://geohex.net/

Slide 58

Slide 58 text

use Geo::Hex::V3::XS; my $zone = Geo::Hex::V3::XS->new( lat => 35.6295764, lng => 139.794686, level => 10 ); $zone->code # => XM4885355114

Slide 59

Slide 59 text

GeoHex v3 encode example use Geo::Hex::V3::XS; my $zone = Geo::Hex::V3::XS->new( lat => 35.6295764, lng => 139.794686, level => 10 ); $zone->code # => XM4885355114 ϨϕϧΛ্͛Δͱਫ਼౓্͕ঢ

Slide 60

Slide 60 text

GeoHex v3 encode example use Geo::Hex::V3::XS; my $zone = Geo::Hex::V3::XS->new( lat => 35.6295764, lng => 139.794686, level => 10 ); $zone->code # => XM4885355114 ΄΅ࣗવ਺ͳܗͰ Encode w w

Slide 61

Slide 61 text

GeoHex v3 encode example use Geo::Hex::V3::XS; my $zone = Geo::Hex::V3::XS->new( lat => 35.6295764, lng => 139.794686, level => 10 ); $zone->code # => XM4885355114 ಄ͷ30ਐ਺දهͷ࢓༷Λআ͚͹ɺ ׬શͳࣗવ਺දه

Slide 62

Slide 62 text

ۙ๣ͷ৘ใ͸Ͳ͏͔ʁ

Slide 63

Slide 63 text

use Geo::Hex::V3::XS; Geo::Hex::V3::XS->new( lat => 35.6295764, lng => 139.794686, level => $_ )->code for 0..10;

Slide 64

Slide 64 text

use Geo::Hex::V3::XS; Geo::Hex::V3::XS->new( lat => 35.6295764, lng => 139.794686, level => $_ )->code for 0..10; # => XM XM4 XM48 XM488 XM4885 XM48853 XM488535 XM4885355 XM48853551 XM488535511 XM4885355114

Slide 65

Slide 65 text

XM XM4 XM48 XM488 XM4885 XM48853 XM488535 XM4885355 XM48853551 XM488535511 XM4885355114

Slide 66

Slide 66 text

XM XM4 XM48 XM488 XM4885 XM48853 XM488535 XM4885355 XM48853551 XM488535511 XM4885355114 ֦େϨϕϧʢਫ਼౓ʣΛ্͛Δ ͱɺίʔυ͕ܻ͚̍ͩ૿͑Δ

Slide 67

Slide 67 text

ۙ๣΋େৎ෉ʂ

Slide 68

Slide 68 text

ͳͷͰ

Slide 69

Slide 69 text

ݱࡏ஍͔Β΋ͬͱ΋͍ۙӺ͸ʁ

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

SELECT name FROM station WHERE geohex LIKE ‘XM4885355114’ # => Empty set

Slide 73

Slide 73 text

SELECT name FROM station WHERE geohex LIKE ‘XM488535511_’ # => Empty set

Slide 74

Slide 74 text

SELECT name FROM station WHERE geohex LIKE ‘XM48853551__’ # => Empty set

Slide 75

Slide 75 text

SELECT name FROM station WHERE geohex LIKE ‘XM4885355___’ # => Empty set

Slide 76

Slide 76 text

SELECT name FROM station WHERE geohex LIKE ‘XM488535____’ # => 国際展示場正門

Slide 77

Slide 77 text

SELECT name FROM station WHERE geohex LIKE ‘XM48853_____’ # => テレコムセンター 船の科学館 青海 国際展示場正門 新木場

Slide 78

Slide 78 text

ݱࡏ஍͔Β΋ͬͱ΋͍ۙӺ͸ʁ A. ࠃࡍలࣔ৔ਖ਼໳

Slide 79

Slide 79 text

GeoHex v3 Encodeʹ͍ͭͯ

Slide 80

Slide 80 text

GeoHex v3 Encodeʹ͍ͭͯ • Web ϝϧΧτϧ஍ਤΛਖ਼࿡֯ܗͰ۠෼ • WebϝϧΧτϧͱ͸ɺϝϧΧτϧ஍ਤΛϒϥ ΢βͷpositionදݱʹࣹӨͨ͠΋ͷ

Slide 81

Slide 81 text

GeoHex v3 Encodeʹ͍ͭͯ • Web ϝϧΧτϧ஍ਤΛਖ਼࿡֯ܗͰ۠෼ • WebϝϧΧτϧͱ͸ɺϝϧΧτϧ஍ਤΛϒϥ ΢βͷpositionදݱʹࣹӨͨ͠΋ͷ x = longtitude * R / 180; y = ln(tan((90 + latitude) * pi / 360)) * (pi / 180) * R / 180 R = ஍ٿͷ൒ܘ = 20037508.34

Slide 82

Slide 82 text

GeoHex v3 Encodeʹ͍ͭͯ

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

GeoHex v3 ͷ࠾൪ϧʔϧ

Slide 90

Slide 90 text

GeoHex v3 ͷ࠾൪ϧʔϧ 3x3ͷਖ਼࿡֯ܗ͕ ࠾൪ͷܻ̍ΛܾΊΔ

Slide 91

Slide 91 text

GeoHex v3 ͷ࠾൪ϧʔϧ

Slide 92

Slide 92 text

GeoHex v3 ͷ࠾൪ϧʔϧ

Slide 93

Slide 93 text

GeoHex v3 ͷ࠾൪ϧʔϧ ࠲ඪΛ 3ਐ਺දهͱΈͳͯ͠ ̍̌ਐ਺දݱ

Slide 94

Slide 94 text

GeoHex v3 ͷϨϕϧࠩ

Slide 95

Slide 95 text

GeoHex v3 ͷϨϕϧࠩ ̏ഒͷαΠζͷਖ਼࿡֯ܗ͕ ্̍ͭͷϨϕϧʹͳΔ

Slide 96

Slide 96 text

GeoHex v3 ͷಛ௃ • Ϛεͷݸ਺Ͱڑ཭දݱͰ͖Δ • ྡΓ߹͏Ϛε໨͕දݱ͠΍͍͢

Slide 97

Slide 97 text

·ͱΊ

Slide 98

Slide 98 text

·ͱΊ • ۙ๣ͷ໰͍߹Θ͕ͤయܕతͳཁ݅ • Ң౓ܦ౓ͷϥϯΫ෇͚ͷख๏঺հ • ਐ਺ͷಡΈସ͑ • GeoHex

Slide 99

Slide 99 text

ϞόΠϧϑΝΫτϦʔ͸ ۭؒ৘ใʹڵຯͷ͋Δ ΤϯδχΞืूதͰ͢!