Program za izračun geometrijskih karakteristik poligonalnih prerezov. Uporaba SQLite relacijske baze - branje in zapisovanje podatkov.

Povezave:

Izvorna koda

geokar.py

#! /usr/bin/env python

import geometrijske_karakteristike as gk
import sys
import sqlite3
import collections

def izpisi_vrednost(naslov, vrednost):
    return "{:>5} = {:.3f}\n".format(naslov, vrednost)


def vnos_podatkov():
    db = sqlite3.connect(sys.argv[1])
    cursor = db.cursor()
    cursor.execute("SELECT * FROM tocke")
    tocke = cursor.fetchall()

    db.close()

    n = len(tocke)
    x = []
    y = []

    for tocka in tocke:
        x.insert(tocka[0] - 1, float(tocka[1]))
        y.insert(tocka[0] - 1, float(tocka[2]))

    x.append(x[0])
    y.append(y[0])

    return n, x, y


def izracun(n, x, y):
    vrednosti = collections.OrderedDict()
    vrednosti["Ax"] = gk.Ax(n, x, y)
    vrednosti["Sx"] = gk.Sx(n, x, y)
    vrednosti["Sy"] = gk.Sy(n, x, y)
    vrednosti["Iy"] = gk.Iy(n, x, y)
    vrednosti["Ixy"] = gk.Ixy(n, x, y)
    vrednosti["xt"] = gk.xt(n, x, y)
    vrednosti["yt"] = gk.yt(n, x, y)
    vrednosti["Ixt"] = gk.Ixt(n, x, y)
    vrednosti["Iyt"] = gk.Iyt(n, x, y)
    vrednosti["Ixyt"] = gk.Ixyt(n, x, y)
    return vrednosti

def izpis(vrednosti):
    s = ""
    for k in vrednosti:
        s = s + izpisi_vrednost(k, vrednosti[k])

    izpis_na_zaslon(s)
    izpis_v_datoteko(s)


def izpis_na_zaslon(s):
    print(s)


def izpis_v_datoteko(s):
    dat = open(sys.argv[2], "w")
    dat.write(s)
    dat.close()


def main():
    # Vnos podatkov
    print("Vnos podatkov ...")
    n, x, y = vnos_podatkov()

    # Izračun količin
    print("Izračun ...")
    vrednosti = izracun(n, x, y)

    # Izpis rezultatov
    print("Izpis ...")
    izpis(vrednosti)


if __name__ == "__main__": main()

geometrijske_karakteristike.py

#! /usr/bin/env python

# Funkcije za izračun geometrijskih karakteristik

def Ax(n, x, y):
    v = 0.0
    for i in range(n):
        v = v + (x[i+1] + x[i]) * (y[i+1] - y[i])

    return 0.5 * v

def Sx(n, x, y):
    v = 0.0
    for i in range(n):
        v = v + (x[i+1] - x[i]) * (y[i+1]**2 + y[i] * y[i+1] + y[i]**2)

    return -1. / 6. * v

def Sy(n, x, y):
    v = 0.0
    for i in range(n):
        v = v + (y[i+1] - y[i]) * (x[i+1]**2 + x[i] * x[i+1] + x[i]**2)

    return 1. / 6. * v

def Ix(n, x, y):
    v = 0.0
    for i in range(n):
        v = v + (x[i+1] - x[i]) * (y[i+1]**3 +  y[i+1]**2 * y[i] + y[i+1] * y[i]**2 + y[i]**3)

    return -1. / 12. * v

def Iy(n, x, y):
    v = 0.0
    for i in range(n):
        v = v + (y[i+1] - y[i]) * (x[i+1]**3 +  x[i+1]**2 * x[i] + x[i+1] * x[i]**2 + x[i]**3)

    return 1. / 12. * v

def Ixy(n, x, y):
    v = 0.0
    for i in range(n):
        v = v + (y[i+1] - y[i]) * (y[i+1] * (3* x[i+1]**2 + 2 * x[i] * x[i+1] + x[i]**2) + y[i] * (3 * x[i]**2 + 2 * x[i] * x[i+1] + x[i+1]**2))

    return -1. / 24. * v

def xt(n, x, y):
    return Sy(n, x, y) / Ax(n, x, y)

def yt(n, x, y):
    return Sx(n, x, y) / Ax(n, x, y)

def Ixt(n, x, y):
    return Ix(n, x, y) - yt(n, x, y)**2 * Ax(n, x, y)

def Iyt(n, x, y):
    return Iy(n, x, y) - xt(n, x, y)**2 * Ax(n, x, y)

def Ixyt(n, x, y):
    return Ixy(n, x, y) + xt(n, x, y) * yt(n, x, y) * Ax(n, x, y)

geokar-txt2sql.py

#! /usr/bin/env python

import sys
import sqlite3

def vnos_podatkov():
    # Vnos podatkov
    dat = open(sys.argv[1], "r")

    n = int(dat.readline())

    x = []
    y = []
    for i in range(n):
        vrstica = dat.readline()
        besede = vrstica.split()
        x.append(float(besede[0]))
        y.append(float(besede[1]))

    x.append(x[0])
    y.append(y[0])

    dat.close()

    return n, x, y

def izpis_v_sqlite_datoteko(n, x, y):
    # Ustvari SQLite bazo in določi tabelo
    db = sqlite3.connect(sys.argv[2])
    cursor = db.cursor()
    sql_command = """
    CREATE TABLE IF NOT EXISTS tocke (
    id INTEGER PRIMARY KEY,
    x DOUBLE,
    y DOUBLE);"""
    cursor.execute(sql_command)
    db.commit()

    # Odstrani vse zapise
    sql_command = """ DELETE FROM tocke;"""
    cursor.execute(sql_command)
    db.commit()

    for i in range(n):
        # format_str = """INSERT INTO tocke (id, x, y) VALUES ("{id}", "{x_sql}", "{y_sql}");"""
        # sql_command = format_str.format(id=i+1, x_sql=x[i], y_sql=y[i])
        sql_command = """INSERT INTO tocke (id, x, y) VALUES ("{}", "{}", "{}");""".format(i+1, x[i], y[i])
        cursor.execute(sql_command)
        db.commit()

    db.close()


def main():
    # Vnos podatkov
    print("Vnos podatkov ...")
    n, x, y = vnos_podatkov()

    # Izpis podatkov v XML datoteko
    print("Izpis v SQLite ...")
    izpis_v_sqlite_datoteko(n, x, y)


if __name__ == "__main__": main()

Podatki in rezultati

primer-1.txt

4
0 0
1 0
1 1
0 1

primer-1-rez.txt

   Ax = 1.000
   Sx = 0.500
   Sy = 0.500
   Ix = 0.333
   Iy = 0.333
  Ixy = -0.250
   xt = 0.500
   yt = 0.500
  Ixt = 0.083
  Iyt = 0.083
 Ixyt = 0.000

Copyright © Matevž Dolenc. All rights reserved.