2017-08-02 2 views
0

まず、フラスコアプリを使用しています。 私はこのコードの後ろにJavaScriptからデータを収集:ここでPythonの別のクラスで定義された値を使用してデータを前面に返す(db.Model型の)クラスを定義する方法はありますか?

@app.route('/getFormData', methods=['POST']) 
def get_javascript_data(): 
    params = request.json 
    sunElevation = params['sunElevation'] 
    cloudCoverage = params['cloudCoverage'] 
    thresholdNDVI = params['thresholdNDVI'] 
    limitScene = params['limitScene'] 
    city = params['city'] 
    coords = params['coords'] 
    data_search = passData(sunElevation, cloudCoverage, thresholdNDVI, limitScene, city, coords) 
    run_script = passData.calcul(data_search) 
    return jsonify(data_search.data_dict) 

は私のCalculがのfonctionです:エンドNDVIデータで

class passData(): 
    def __init__(self, sunElevation, cloudCoverage, thresholdNDVI, limitScene, city, coords): 
     self.sunElevation = sunElevation 
     self.cloudCoverage = cloudCoverage 
     self.thresholdNDVI = thresholdNDVI 
     self.limitScene = limitScene 
     self.city = city 
     self.coords = coords 
     self.coords.append(self.coords[0]) 
     self.data_dict = [{'sunElevation':self.sunElevation, 
          'cloudCoverage':self.cloudCoverage, 
          'thresholdNDVI':self.thresholdNDVI, 
          'limitScene':self.limitScene, 
          'city':self.city, 
          'coords':self.coords 
          }] 

    def calcul(self): 
     main_script(self.sunElevation, self.cloudCoverage, self.thresholdNDVI, self.limitScene, self.city, self.coords) 

     return (print("traitement terminé")) 

はテーブル名「ndvi_ +都市の下に、データベースに格納されています' すべてああ、これはうまく動作しますが、私は結果を視覚化する前にこのデータを返送したい後、ここに私はそうするために

@app.route('/ndviAuto') 
def get_ndviAuto(): 
    query = select([NdviAuto.ogc_fid.label('ogc_fid'), func.ST_AsGeoJSON(func.ST_Transform(NdviAuto.wkb_geometry,4326)).label('wkb_geometry')]).where(NdviAuto.wkb_geometry!=None) 
    dataQuery = db.session.execute(query).fetchall() 
    data_all = [] 

    for ndvi in dataQuery: 
     ndvi = dict(ndvi) 
     data_all.append({ 
        'type': 'Feature', 
        'properties':{ 
          'id':ndvi['ogc_fid'], 
         }, 
         'geometry':json.loads(ndvi['wkb_geometry']) 
         }) 
    return jsonify(data_all) 

を使用するルートがある私は、このように定義NdviAutoクラスを作った:

class NdviAuto(db.Model): 
    __tablename__ = 'ndvi_' // + city ? 
    ogc_fid = db.Column(db.Integer, primary_key=True) 
    wkb_geometry = db.Column(Geometry(geometry_type='POLYGON', srid=32631)) 

私の問題は、自動的に正しい表を取得するために値 'city'を使用してこのクラスを構築する方法がわかりません。今、データを返信するルートは機能しますが、毎回、自分自身、テーブル名を変更する必要があります。 私はpostgreSQLデータベースを使用しています。

+0

あなたは「毎回を変更する必要がある」とはどういう意味ですか? *都市*はリクエストからリクエストに変更できますか?それともプログラムの生涯中に修正されますか? –

答えて

0

Declarativeクラスが作成された後に__tablename__属性を変更することはできません。つまり、変更することはできませんが、効果はありません。 Tableオブジェクトはすでに作成されており、属性とメタデータは__table__に割り当てられています。 Declarative Baseregistry of created classesを保持していますので、異なるテーブルでクラスを再定義するときに警告が表示されます。理由は、文字列クラス名を使用する将来の関係などが再定義を指すようになります。常にそうであるとは限らないが、動的なテーブル名を持つことは、DB設計の問題の兆候である。

既にダイナミックテーブルが用意されているので、軽量の宣言クラスまたはTableインスタンスの代わりに、軽量のtable()および構造を使用できます。正しい軽量テーブルを作成するヘルパー関数を作成し、それを使用します。

あなたのビューで
def ndvi_auto(city): 
    return table(
     'ndvi_{}'.format(city), 
     column('ogc_fid', db.Integer), 
     column('wkb_geometry', Geometry(geometry_type='POLYGON', 
             srid=32631))) 

、その後:

@app.route('/ndviAuto') 
def get_ndviAuto(): 
    ndvi_table = ndvi_auto('some_city') 

    query = select([ndvi_table.c.ogc_fid, 
        func.ST_AsGeoJSON(
         func.ST_Transform(
          ndvi_table.c.wkb_geometry, 
          4326 
         ) 
        ).label('wkb_geometry')]).\ 
     where(ndvi_table.c.wkb_geometry != None) 

    result = db.session.execute(query) 

    data = [{'type': 'Feature', 
      'properties': { 'id': ndvi.ogc_fid }, 
      'geometry': json.loads(ndvi.wkb_geometry)} 
      for ndvi in result] 

    return jsonify(data) 
+0

ありがとう私はこれを試してみよう! –

+0

テーブル名は実際どのくらいの頻度で変更されますか?要求ごとに、または何か別に? –

+0

毎回データが前面から収集され、新しいテーブルが作成されます(名前が 'ndvi_' + cityが作成され、アクセスする必要のあるテーブルが前面に表示されるため、テーブル名は変更されません) –

関連する問題