Rails发送Polygon作为参数

时间:2016-01-08 17:34:48

标签: ruby-on-rails polygon postgis

我试图使用testunit来测试一个函数,该函数创建了一个带有多边形的记录,但它不断抛出错误(甚至没有失败)。

这是我的功能:

def create
        @named_location = NamedLocation.new(params[:named_location])
        @named_location.account = Account.find(params[:account_id])
        @named_location.save!

        respond_to do |format|
          format.json { render status: ok }
        end
      end

def multi_polygon_tiny_circle
      Geometry.from_ewkt("SRID=4326;MULTIPOLYGON(((-102.038654878937 32.064109,-102.038654881263 32.0641089763817,-102.038654888152 32.0641089536711,-102.03865489934 32.0641089327409,-102.038654914395 32.0641089143953,-102.038654932741 32.0641088993396,-102.038654953671 32.0641088881521,-102.038654976382 32.0641088812629,-102.038655 32.0641088789367,-102.038655023618 32.0641088812629,-102.038655046329 32.0641088881521,-102.038655067259 32.0641088993396,-102.038655085605 32.0641089143953,-102.03865510066 32.0641089327409,-102.038655111848 32.0641089536711,-102.038655118737 32.0641089763817,-102.038655121063 32.064109,-102.038655118737 32.0641090236183,-102.038655111848 32.0641090463289,-102.03865510066 32.0641090672592,-102.038655085605 32.0641090856047,-102.038655067259 32.0641091006604,-102.038655046329 32.0641091118479,-102.038655023618 32.0641091187371,-102.038655 32.0641091210633,-102.038654976382 32.0641091187371,-102.038654953671 32.0641091118479,-102.038654932741 32.0641091006604,-102.038654914395 32.0641090856047,-102.03865489934 32.0641090672592,-102.038654888152 32.0641090463289,-102.038654881263 32.0641090236183,-102.038654878937 32.064109)))")
    end

这是我的考验:

def setup
        @account = Factory.create(:account)

        @named_location_attributes = {"name"=>"This is name",
                                      "named_location_type_id"=>"1067",
                                      "distance"=>"0.020",
                                      "contact_name"=>"This is name",
                                      "contact_email"=>"this is email",
                                      "phone"=>"this is phone",
                                      "address1"=>"this is address 1",
                                      "address2"=>"this is address 2",
                                      "city"=>"this is city",
                                      "state"=>"this is state",
                                      "zip"=>"this is CP",
                                      "country"=>"this is country",
                                      "polygons"=> GeometryFactory.multi_polygon_tiny_circle
                                    }
      end

      test 'POST \'create\' with valid parameters responds with 200 and creates the named location' do
        expected_named_location_count = NamedLocation.count + 1
        with_api_key :post, :create, named_location: @named_location_attributes, account_id: @account.id

        assert_equal expected_named_location_count, NamedLocation.count
        assert_response 200
      end
    end

我的控制台输出如下:

Error: test_POST_'create'_with_valid_parameters_responds_with_200_and_creates_the_named_location(Api::V3::NamedLocationsControllerTest): TypeError: no implicit conversion of GeoRuby::SimpleFeatures::MultiPolygon into String.

这是我在使用此功能时发送参数的方式(不是测试):

    {"named_location"=>{"name"=>"This is name",
 "named_location_type_id"=>"1067",
 "distance"=>"0.020",
 "contact_name"=>"This is name",
 "contact_email"=>"this is email",
 "phone"=>"this is phone",
 "address1"=>"this is address 1",
 "address2"=>"this is address 2",
 "city"=>"this is city",
 "state"=>"this is state",
 "zip"=>"this is CP",
 "country"=>"this is country",
 "polygons"=>"{\"polygons\":[\"SRID=4326;POLYGON((-106.43749999966 52.799595352219,
-105.48218969269 53.111583065001,
-104.61965538012 53.506495675742,
-103.87113551932 53.972405927736,
-103.25506115345 54.495583938857,
-102.78660207802 55.061062997275,
-102.47729331022 55.653215704632,
-102.33475105843 56.256304911422,
-102.36248518596 56.854979447184,
-102.55981278656 57.434692715254,
-102.9218749998 57.982031666541,
-103.43975665238 58.484953241302,
-104.10070577925 58.932933934658,
-104.88844761936 59.317044852852,
-105.78358535423 59.629969018553,
-106.76407772204 59.865979668914,
-107.80578174662 60.02089808296,
-108.88304721768 60.0920474701,
-109.96934828407 60.07821610921,
-111.03793660832 59.97963868578,
-112.06249999944 59.798000005,
-113.0178103064 59.536460265427,
-113.88034461897 59.199696127133,
-114.62886447978 58.793947173851,
-115.24493884565 58.327053374636,
-115.71339792107 57.808466192486,
-116.02270668888 57.249214517383,
-116.16524894067 56.661807069605,
-116.13751481313 56.060055675387,
-115.94018721254 55.45880898897,
-115.5781249993 54.873593619734,
-115.06024334672 54.320168627189,
-114.39929421984 53.814009013379,
-113.61155237974 53.369742975217,
-112.71641464487 53.000575043017,
-111.73592227706 52.717731779596,
-110.69421825247 52.529967777738,
-109.61695278142 52.443167079435,
-108.53065171503 52.460069120074,
-107.46206339078 52.580139516623,
-106.43749999966 52.799595352219))\"]}"},
 "commit"=>"Save Location"}

1 个答案:

答案 0 :(得分:0)

我的问题是我没有以正确的格式创建多边形。我刚刚创建了一个JSON(硬编码),它工作正常。 multi_polygon_tiny_circle没有创建我需要的JSON