Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce point!, line_string!, and polygon! macros. #352

Merged
merged 1 commit into from
Apr 21, 2019
Merged

Conversation

frewsxcv
Copy link
Member

@frewsxcv frewsxcv commented Apr 1, 2019

Shorthand way to construct Points, LineStrings, and Polygons. Constructors require explicit x/y parameters to prevent x/y or y/x ambiguity.

Closes #336

point!

Creates a Point from the given coordinates.

point!(«(x,y)»)

Examples

Creating a Point, supplying x/y values:

use geo::point;

let p = point!(x: 181.2, y: 51.79);

assert_eq!(p, geo::Point(geo::Coordinate {
    x: 181.2,
    y: 51.79,
}));

line_string!

Creates a LineString containing the given coordinates.

line_string![«Coordinate|(x,y)»,]

Examples

Creating a LineString, supplying x/y values:

use geo::line_string;

let ls = line_string![
    (x: -21.95156, y: 64.1446),
    (x: -21.951, y: 64.14479),
    (x: -21.95044, y: 64.14527),
    (x: -21.951445, y: 64.145508)
];

assert_eq!(ls[1], geo::Coordinate {
    x: -21.951,
    y: 64.14479
});

Creating a LineString, supplying Coordinates:

use geo::line_string;

let coord1 = geo::Coordinate { x: -21.95156, y: 64.1446 };
let coord2 = geo::Coordinate { x: -21.951, y: 64.14479 };
let coord3 = geo::Coordinate { x: -21.95044, y: 64.14527 };
let coord4 = geo::Coordinate { x: -21.951445, y: 64.145508 };

let ls = line_string![coord1, coord2, coord3, coord4];

assert_eq!(ls[1], geo::Coordinate {
    x: -21.951,
    y: 64.14479
});

polygon!

Creates a Polygon containing the given coordinates.

polygon![«Coordinate|(x,y)»,]
// or
polygon!(
    exterior: [«Coordinate|(x,y)»,],
    interiors: [
        [«Coordinate|(x,y)»,],
        …
    ],
)

Examples

Creating a Polygon without interior rings, supplying x/y values:

use geo::polygon;

let poly = polygon![
    (x: -111., y: 45.),
    (x: -111., y: 41.),
    (x: -104., y: 41.),
    (x: -104., y: 45.),
];

assert_eq!(
    poly.exterior()[1],
    geo::Coordinate { x: -111., y: 41. },
);

Creating a Polygon, supplying x/y values:

use geo::polygon;

let poly = polygon!(
    exterior: [
        (x: -111., y: 45.),
        (x: -111., y: 41.),
        (x: -104., y: 41.),
        (x: -104., y: 45.),
    ],
    interiors: [
        [
            (x: -110., y: 44.),
            (x: -110., y: 42.),
            (x: -105., y: 42.),
            (x: -105., y: 44.),
        ],
    ],
);

assert_eq!(
    poly.exterior()[1],
    geo::Coordinate { x: -111., y: 41. },
);

@frewsxcv
Copy link
Member Author

frewsxcv commented Apr 8, 2019

We may be able to symlink this file into geo-types so the macros are available in geo-types as well. rust-lang/cargo#3896

@frewsxcv
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Apr 21, 2019
352: Introduce point!, line_string!, and polygon! macros. r=frewsxcv a=frewsxcv

Shorthand way to construct `Point`s, `LineString`s, and `Polygon`s. Constructors require explicit x/y parameters to prevent x/y or y/x ambiguity.

Closes #336

# `point!`

Creates a `Point` from the given coordinates.

```rust
point!(«(x,y)»)
```

## Examples

Creating a `Point`, supplying x/y values:

```rust
use geo::point;

let p = point!(x: 181.2, y: 51.79);

assert_eq!(p, geo::Point(geo::Coordinate {
    x: 181.2,
    y: 51.79,
}));
```

# `line_string!`

Creates a `LineString` containing the given coordinates.

```rust
line_string![«Coordinate|(x,y)», …]
```

## Examples

Creating a `LineString`, supplying x/y values:

```rust
use geo::line_string;

let ls = line_string![
    (x: -21.95156, y: 64.1446),
    (x: -21.951, y: 64.14479),
    (x: -21.95044, y: 64.14527),
    (x: -21.951445, y: 64.145508)
];

assert_eq!(ls[1], geo::Coordinate {
    x: -21.951,
    y: 64.14479
});
```

Creating a `LineString`, supplying `Coordinate`s:

```rust
use geo::line_string;

let coord1 = geo::Coordinate { x: -21.95156, y: 64.1446 };
let coord2 = geo::Coordinate { x: -21.951, y: 64.14479 };
let coord3 = geo::Coordinate { x: -21.95044, y: 64.14527 };
let coord4 = geo::Coordinate { x: -21.951445, y: 64.145508 };

let ls = line_string![coord1, coord2, coord3, coord4];

assert_eq!(ls[1], geo::Coordinate {
    x: -21.951,
    y: 64.14479
});
```

# `polygon!`

Creates a `Polygon` containing the given coordinates.

```rust
polygon![«Coordinate|(x,y)», …]
// or
polygon!(
    exterior: [«Coordinate|(x,y)», …],
    interiors: [
        [«Coordinate|(x,y)», …],
        …
    ],
)
```

## Examples

Creating a `Polygon` without interior rings, supplying x/y values:

```rust
use geo::polygon;

let poly = polygon![
    (x: -111., y: 45.),
    (x: -111., y: 41.),
    (x: -104., y: 41.),
    (x: -104., y: 45.),
];

assert_eq!(
    poly.exterior()[1],
    geo::Coordinate { x: -111., y: 41. },
);
```

Creating a `Polygon`, supplying x/y values:

```rust
use geo::polygon;

let poly = polygon!(
    exterior: [
        (x: -111., y: 45.),
        (x: -111., y: 41.),
        (x: -104., y: 41.),
        (x: -104., y: 45.),
    ],
    interiors: [
        [
            (x: -110., y: 44.),
            (x: -110., y: 42.),
            (x: -105., y: 42.),
            (x: -105., y: 44.),
        ],
    ],
);

assert_eq!(
    poly.exterior()[1],
    geo::Coordinate { x: -111., y: 41. },
);
```

Co-authored-by: Corey Farwell <coreyf@rwell.org>
@bors
Copy link
Contributor

bors bot commented Apr 21, 2019

Build succeeded

@bors bors bot merged commit feeeff5 into master Apr 21, 2019
@frewsxcv frewsxcv deleted the frewsxcv-macros branch April 21, 2019 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Macro-based constructors to remove xy vs. yx ambiguity
1 participant