Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Feb 28, 2020
0 parents commit bdad5e1
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: crystal

script:
- crystal spec
- crystal tool format --check
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Anton Maminov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# GeoJSON

The [GeoJSON](https://tools.ietf.org/html/rfc7946) Format for Crystal

[![Build Status](https://travis-ci.org/geocrystal/geo_json.svg?branch=master)](https://travis-ci.org/geocrystal/geo_json)

## GeoJSON types

- [ ] Point
- [ ] MultiPoint
- [ ] LineString
- [ ] MultiLineString
- [ ] Polygon
- [ ] MultiPolygon
- [ ] GeometryCollection
- [ ] Feature
- [ ] FeatureCollection

## Installation

1. Add the dependency to your `shard.yml`:

```yaml
dependencies:
geo_json:
github: geocrystal/geo_json
```
2. Run `shards install`

## Usage

```crystal
require "geo_json"
```

TODO: Write usage instructions here

## Development

TODO: Write development instructions here

## Contributing

1. Fork it (<https://github.com/geocrystal/geo_json/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Anton Maminov](https://github.com/mamantoha) - creator and maintainer
9 changes: 9 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: geo_json
version: 0.0.1

authors:
- Anton Maminov <[email protected]>

crystal: 0.33.0

license: MIT
9 changes: 9 additions & 0 deletions spec/coordinates_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "./spec_helper"

describe GeoJSON::Coordinates do
it "raises an exception if coordinates are invalid" do
expect_raises Exception, "GeoJSON::Coordinates must have two coordinates" do
GeoJSON::Coordinates.new([1.0, 1.0, 0.0])
end
end
end
7 changes: 7 additions & 0 deletions spec/geo_json_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "./spec_helper"

describe GeoJSON do
it "should have version" do
GeoJSON::VERSION.should_not be_nil
end
end
34 changes: 34 additions & 0 deletions spec/point_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "./spec_helper"

describe GeoJSON::Point do
longitude = -80.1347334
latitude = 25.7663562
position = [longitude, latitude]

point_json = {
"type" => "Point",
"coordinates" => position,
}.to_json

it "parses json" do
point = GeoJSON::Point.from_json(point_json)

point.should be_a(GeoJSON::Point)
point.type.should eq("Point")
point.coordinates.should be_a(GeoJSON::Coordinates)
point.longitude.should eq(longitude)
point.latitude.should eq(latitude)
end

it "creates object" do
point = GeoJSON::Point.new(longitude: longitude, latitude: latitude)

point.should be_a(GeoJSON::Point)
point.type.should eq("Point")
point.coordinates.should be_a(GeoJSON::Coordinates)
point.longitude.should eq(longitude)
point.latitude.should eq(latitude)

point.to_json.should eq(point_json)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/geo_json"
6 changes: 6 additions & 0 deletions src/geo_json.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "json"
require "./geo_json/**"

module GeoJSON
VERSION = "0.0.1"
end
31 changes: 31 additions & 0 deletions src/geo_json/coordinates.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module GeoJSON
class Coordinates
getter coordinates : Array(Float64)

def initialize(@coordinates : Array(Float64))
raise_if_invalid
end

def initialize(parser : JSON::PullParser)
@coordinates = Array(Float64).new(parser)

raise_if_invalid
end

def longitude
coordinates[0]
end

def latitude
coordinates[1]
end

private def raise_if_invalid
if coordinates.size != 2
raise "GeoJSON::Coordinates must have two coordinates"
end
end

delegate to_json, to: coordinates
end
end
19 changes: 19 additions & 0 deletions src/geo_json/object.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module GeoJSON
# GeoJSON Object
#
# A GeoJSON object represents a Geometry, Feature, or collection of Features.
#
# * A GeoJSON object is a JSON object.
# * A GeoJSON object has a member with the name "type". The value of
# the member MUST be one of the GeoJSON types.
# `Point`, `MultiPoint`, `LineString`, `MultiLineString`, `Polygon`, `MultiPolygon`,
# `GeometryCollection`, `Feature`, and `FeatureCollection`
# * A GeoJSON object MAY have a "bbox" member, the value of which MUST
# be a bounding box array
# * A GeoJSON object MAY have other members
abstract class Object
include JSON::Serializable

abstract def type : String
end
end
14 changes: 14 additions & 0 deletions src/geo_json/point.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module GeoJSON
class Point < Object
property type : String = "Point"

property coordinates : Coordinates

def initialize(*, longitude, latitude)
@coordinates = Coordinates.new([longitude, latitude])
end

delegate longitude, to: coordinates
delegate latitude, to: coordinates
end
end

0 comments on commit bdad5e1

Please sign in to comment.