Class: RGeo::CoordSys::CRSToCRS

Inherits:
RGeo::CoordSys::CS::CoordinateTransform show all
Defined in:
lib/rgeo/coord_sys/crs_to_crs.rb,
ext/proj4_c_impl/main.c

Overview

This is a Ruby wrapper around a proj crs_to_crs A crs_to_crs transformation object is a pipeline between two known coordinate reference systems. proj.org/development/reference/functions.html#c.proj_create_crs_to_crs

It also inherits from the RGeo::CoordSys::CoordinateTransform abstract class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#source_csObject Also known as: from

Returns the value of attribute source_cs.



12
13
14
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 12

def source_cs
  @source_cs
end

#target_csObject Also known as: to

Returns the value of attribute target_cs.



12
13
14
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 12

def target_cs
  @target_cs
end

Class Method Details

._create(from, to) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'ext/proj4_c_impl/main.c', line 429

static VALUE cmethod_crs_to_crs_create(VALUE klass, VALUE from, VALUE to) {
  VALUE result;
  RGeo_Proj4Data *from_data;
  RGeo_Proj4Data *to_data;
  result = Qnil;
  PJ *from_pj;
  PJ *to_pj;
  PJ *gis_pj;
  PJ *crs_to_crs;
  RGeo_CRSToCRSData *data;

  TypedData_Get_Struct(from, RGeo_Proj4Data, &rgeo_proj4_data_type, from_data);
  TypedData_Get_Struct(to, RGeo_Proj4Data, &rgeo_proj4_data_type, to_data);
  from_pj = from_data->pj;
  to_pj = to_data->pj;
  crs_to_crs = proj_create_crs_to_crs_from_pj(local_proj_context, from_pj,
                                              to_pj, 0, NULL);

  // check for invalid transformation
  if (crs_to_crs == 0) {
    rb_raise(rb_eRGeoInvalidProjectionError,
             "CRSToCRS could not be created from input projections");
  }

  // necessary to use proj_normalize_for_visualization so that we
  // do not have to worry about the order of coordinates in every
  // coord system
  gis_pj = proj_normalize_for_visualization(local_proj_context, crs_to_crs);
  if (gis_pj) {
    proj_destroy(crs_to_crs);
    crs_to_crs = gis_pj;
  }
  data = ALLOC(RGeo_CRSToCRSData);
  if (data) {
    data->crs_to_crs = crs_to_crs;
    result = TypedData_Wrap_Struct(klass, &rgeo_crs_to_crs_data_type, data);
  }
  return result;
}

.create(from, to) ⇒ Object



15
16
17
18
19
20
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 15

def create(from, to)
  crs_to_crs = _create(from, to)
  crs_to_crs.source_cs = from
  crs_to_crs.target_cs = to
  crs_to_crs
end

Instance Method Details

#_area_of_useObject



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'ext/proj4_c_impl/main.c', line 520

static VALUE method_crs_to_crs_area_of_use_str(VALUE self) {
  VALUE result;
  RGeo_CRSToCRSData *crs_to_crs_data;
  PJ *crs_to_crs_pj;
  const char *str;

  result = Qnil;
  TypedData_Get_Struct(self, RGeo_CRSToCRSData, &rgeo_crs_to_crs_data_type,
                       crs_to_crs_data);
  crs_to_crs_pj = crs_to_crs_data->crs_to_crs;
  if (crs_to_crs_pj) {
    if (proj_get_area_of_use(local_proj_context, crs_to_crs_pj, NULL, NULL,
                             NULL, NULL, &str)) {
      result = rb_str_new2(str);
    }
  }
  return result;
}

#_as_textObject Also known as: to_wkt



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'ext/proj4_c_impl/main.c', line 500

static VALUE method_crs_to_crs_wkt_str(VALUE self) {
  VALUE result;
  RGeo_CRSToCRSData *crs_to_crs_data;
  PJ *crs_to_crs_pj;
  const char *str;

  result = Qnil;
  TypedData_Get_Struct(self, RGeo_CRSToCRSData, &rgeo_crs_to_crs_data_type,
                       crs_to_crs_data);
  crs_to_crs_pj = crs_to_crs_data->crs_to_crs;
  if (crs_to_crs_pj) {
    const char *const options[] = {"MULTILINE=NO", NULL};
    str = proj_as_wkt(local_proj_context, crs_to_crs_pj, WKT_TYPE, options);
    if (str) {
      result = rb_str_new2(str);
    }
  }
  return result;
}

#_identity?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'ext/proj4_c_impl/main.c', line 556

static VALUE method_crs_to_crs_identity(VALUE self, VALUE from, VALUE to) {
  VALUE result;
  RGeo_Proj4Data *from_data;
  RGeo_Proj4Data *to_data;
  result = Qnil;
  PJ *from_pj;
  PJ *to_pj;

  result = Qfalse;

  TypedData_Get_Struct(from, RGeo_Proj4Data, &rgeo_proj4_data_type, from_data);
  TypedData_Get_Struct(to, RGeo_Proj4Data, &rgeo_proj4_data_type, to_data);
  from_pj = from_data->pj;
  to_pj = to_data->pj;

  if (from_pj && to_pj) {
    if (proj_is_equivalent_to(from_pj, to_pj, PJ_COMP_EQUIVALENT)) {
      result = Qtrue;
    }
  }

  return result;
}

#_proj_typeObject



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'ext/proj4_c_impl/main.c', line 539

static VALUE method_crs_to_crs_proj_type(VALUE self) {
  VALUE result;
  RGeo_CRSToCRSData *crs_to_crs_data;
  PJ *crs_to_crs_pj;
  int proj_type;

  result = Qnil;
  TypedData_Get_Struct(self, RGeo_CRSToCRSData, &rgeo_crs_to_crs_data_type,
                       crs_to_crs_data);
  crs_to_crs_pj = crs_to_crs_data->crs_to_crs;
  if (crs_to_crs_pj) {
    proj_type = proj_get_type(crs_to_crs_pj);
    result = INT2FIX(proj_type);
  }
  return result;
}

#_transform_coords(x, y, z) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'ext/proj4_c_impl/main.c', line 469

static VALUE method_crs_to_crs_transform(VALUE self, VALUE x, VALUE y,
                                         VALUE z) {
  VALUE result;
  RGeo_CRSToCRSData *crs_to_crs_data;
  PJ *crs_to_crs_pj;
  double xval, yval, zval;
  PJ_COORD input;
  PJ_COORD output;

  result = Qnil;
  TypedData_Get_Struct(self, RGeo_CRSToCRSData, &rgeo_crs_to_crs_data_type,
                       crs_to_crs_data);
  crs_to_crs_pj = crs_to_crs_data->crs_to_crs;
  if (crs_to_crs_pj) {
    xval = rb_num2dbl(x);
    yval = rb_num2dbl(y);
    zval = NIL_P(z) ? 0.0 : rb_num2dbl(z);

    input = proj_coord(xval, yval, zval, HUGE_VAL);
    output = proj_trans(crs_to_crs_pj, PJ_FWD, input);

    result = rb_ary_new2(NIL_P(z) ? 2 : 3);
    rb_ary_push(result, DBL2NUM(output.xyz.x));
    rb_ary_push(result, DBL2NUM(output.xyz.y));
    if (!NIL_P(z)) {
      rb_ary_push(result, DBL2NUM(output.xyz.z));
    }
  }
  return result;
}

#area_of_useObject



36
37
38
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 36

def area_of_use
  _area_of_use
end

#identity?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 40

def identity?
  _identity?(source_cs, target_cs)
end

#inspectObject



107
108
109
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 107

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} @source_cs=#{source_cs.original_str} @target_cs=#{target_cs.original_str}>"
end

#transform(from_geometry, to_factory) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 58

def transform(from_geometry, to_factory)
  case from_geometry
  when Feature::Point
    transform_point(from_geometry, to_factory)
  when Feature::Line
    to_factory.line(from_geometry.points.map { |p| transform_point(p, to_factory) })
  when Feature::LinearRing
    transform_linear_ring(from_geometry, to_factory)
  when Feature::LineString
    to_factory.line_string(from_geometry.points.map { |p_| transform_point(p_, to_factory) })
  when Feature::Polygon
    transform_polygon(from_geometry, to_factory)
  when Feature::MultiPoint
    to_factory.multi_point(from_geometry.map { |p| transform_point(p, to_factory) })
  when Feature::MultiLineString
    to_factory.multi_line_string(from_geometry.map { |g| transform(g, to_factory) })
  when Feature::MultiPolygon
    to_factory.multi_polygon(from_geometry.map { |p| transform_polygon(p, to_factory) })
  when Feature::GeometryCollection
    to_factory.collection(from_geometry.map { |g| transform(g, to_factory) })
  end
end

#transform_coords(x, y, z) ⇒ Object

transform the coordinates from the initial CRS to the destination CRS



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 45

def transform_coords(x, y, z)
  if from._radians? && from._geographic?
    x *= ImplHelper::Math::DEGREES_PER_RADIAN
    y *= ImplHelper::Math::DEGREES_PER_RADIAN
  end
  result = _transform_coords(x, y, z)
  if result && to._radians? && to._geographic?
    result[0] *= ImplHelper::Math::RADIANS_PER_DEGREE
    result[1] *= ImplHelper::Math::RADIANS_PER_DEGREE
  end
  result
end

#transform_linear_ring(from_ring_, to_factory_) ⇒ Object



97
98
99
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 97

def transform_linear_ring(from_ring_, to_factory_)
  to_factory_.linear_ring(from_ring_.points[0..-2].map { |p_| transform_point(p_, to_factory_) })
end

#transform_point(from_point, to_factory) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 81

def transform_point(from_point, to_factory)
  from_factory_ = from_point.factory
  from_has_z_ = from_factory_.property(:has_z_coordinate)
  from_has_m_ = from_factory_.property(:has_m_coordinate)
  to_has_z_ = to_factory.property(:has_z_coordinate)
  to_has_m_ = to_factory.property(:has_m_coordinate)
  coords_ = transform_coords(from_point.x, from_point.y, from_has_z_ ? from_point.z : nil)
  return unless coords_
  extras_ = []
  extras_ << coords_[2].to_f if to_has_z_
  if to_has_m_
    extras_ << from_has_m_ ? from_point.m : 0.0
  end
  to_factory.point(coords_[0], coords_[1], *extras_)
end

#transform_polygon(from_polygon_, to_factory_) ⇒ Object



101
102
103
104
105
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 101

def transform_polygon(from_polygon_, to_factory_)
  ext_ = transform_linear_ring(from_polygon_.exterior_ring, to_factory_)
  int_ = from_polygon_.interior_rings.map { |r_| transform_linear_ring(r_, to_factory_) }
  to_factory_.polygon(ext_, int_)
end

#transform_typeObject



32
33
34
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 32

def transform_type
  PROJ_TYPES[_proj_type]
end

#wkt_typenameObject



27
28
29
30
# File 'lib/rgeo/coord_sys/crs_to_crs.rb', line 27

def wkt_typename
  wkt_str = to_wkt
  wkt_str[0, wkt_str.index("[")]
end