Why knn2 of spatialite finds 0 points? - python

So I add a set of points and try to find them like so:
import os
from datetime import datetime
import json
from pprint import pprint
from playhouse.sqlite_ext import *
from peewee import *
from playhouse.kv import *
from playhouse.shortcuts import model_to_dict
from playhouse.reflection import generate_models, print_model, print_table_sql
from IPython.display import SVG
import folium
db = SqliteExtDatabase('./my_database.db')
db.connect()
db.connection().enable_load_extension(True)
print(db.execute_sql("SELECT load_extension('mod_spatialite.so');").fetchall())
print(db.execute_sql("SELECT InitSpatialMetaData(); ").fetchall())
print(db.execute_sql("SELECT sqlite_version(), spatialite_version();").fetchall())
#print(db.execute_sql("CREATE VIRTUAL TABLE knn2 USING VirtualKNN2();").fetchall())
class BaseModel(Model):
class Meta:
database = db
class GeometryField(Field):
db_field = 'geometry'
def geometryFromWKT(wellknowntext):
return fn.ST_GeomFromText(wellknowntext)
class Tbl(BaseModel):
geometry = GeometryField()
models = generate_models(db)
Tbl.create_table()
Tbl.insert(geometry=geometryFromWKT('POINT(4 9)')).execute()
Tbl.insert(geometry=geometryFromWKT('POINT(9 9)')).execute()
Tbl.insert(geometry=geometryFromWKT('POINT(14 9)')).execute()
Tbl.insert(geometry=geometryFromWKT('POINT(4 19)')).execute()
Tbl.insert(geometry=geometryFromWKT('POINT(41 9)')).execute()
Tbl.insert(geometry=geometryFromWKT('POINT(14 19)')).execute()
#https://www.gaia-gis.it/gaia-sins/spatialite-sql-4.4.0.html
cmd = """SELECT AsGeoJSON(ST_Union(CastToXY(geometry)), 5, 3) from Tbl"""
data_all = db.execute_sql(cmd).fetchall()[0][0]
print(data_all)
cmd = """SELECT * FROM knn2
WHERE f_table_name = 'Tbl' AND ref_geometry = MakePoint(5, 5) AND f_geometry_column = 'geometry' AND radius = 1000.0;"""
data_knn = db.execute_sql(cmd).fetchall()
print(data_knn)
One can try it out here (simple with knn) or here (complicated with knn2).
The code outputs:
[(None,)]
[(1,)]
[('3.22.0', '5.1.0-devel')]
{"type":"MultiPoint","bbox":[4,9,41,19],"coordinates":[[4,9],[4,19],[9,9],[14,9],[14,19],[41,9]]}
[]
So there are points (one can see on the map) around Point(5, 5) yet none is found in fadius of 10 and 1000... Why and how to make knn2 work properly for points created from WKT in sqlite using spatialite?

Related

iterating intergers over a method from a range

Im trying to create a list of CommandLinks in Revit Dynamo using python with a forloop that checks the length of the inputs list and creates an array of Commandlinks from a range generated from that list. Is it possible to insert the integer into the CommandLink method using a forloop?
`
import clr
import sys
import System
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Clipboard
# import Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import (TaskDialog, TaskDialogCommonButtons,
TaskDialogCommandLinkId, TaskDialogResult)
title = IN[0]
buttonlists = IN[1]
resultslist = IN[2]
dialog = TaskDialog(title)
buttonNum = len(resultslist)
# Properties
dialog.MainInstruction = title
# dialog.ExpandedContent = expanded_content
# Settings and buttons
dialog.TitleAutoPrefix = False
dialog.AllowCancellation = True
dialog.CommonButtons = TaskDialogCommonButtons.Cancel
dialog.DefaultButton = TaskDialogResult.Cancel
# Add Command Link
for n in range(buttonNum+1):
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink+(n), buttontext+(n))
result = dialog.Show()
if result == TaskDialogResult.Cancel:
OUT = 'Dialog was Cancelled'
if result == TaskDialogResult.CommandLink(n):
OUT = result(n)
`
enter image description here
I need to iterate (n) into the commandlink numbers and results, so it creates commandlinks based on the length of the input list.

the DataFrameClinet class of python's package for influxdb uploads only the last line from the dataframe

i am trying to use python's package for influxdb to upload dataframe into the database
i am using the write_points class to write point into the database as given in the documentation(https://influxdb-python.readthedocs.io/en/latest/api-documentation.html)
every time i try to use the class it only updates the last line of the dataframe instead of the complete dataframe.
is this a usual behavior or there is some problem here?
given below is my script:
from influxdb import InfluxDBClient, DataFrameClient
import pathlib
import numpy as np
import pandas as pd
import datetime
db_client = DataFrameClient('dbserver', port, 'username', 'password', 'database',
ssl=True, verify_ssl=True)
today = datetime.datetime.now().strftime('%Y%m%d')
path = pathlib.Path('/dir1/dir/2').glob(f'pattern_to_match*/{today}.filename.csv')
for file in path:
order_start = pd.read_csv(f'{file}')
if not order_start.empty:
order_start['data_line1'] = (order_start['col1'] - \
order_start['col2'])*1000
order_start['data_line2'] = (order_start['col3'] - \
order_start['col4'])*1000
d1 = round(order_start['data_line1'].quantile(np.arange(0,1.1,0.1)), 3)
d2 = round(order_start['data_line2'].quantile(np.arange(0,1.1,0.1)), 3)
out_file = pd.DataFrame()
out_file = out_file.append(d1)
out_file = out_file.append(d2)
out_file = out_file.T
out_file.index = out_file.index.set_names(['percentile'])
out_file = out_file.reset_index()
out_file['percentile'] = out_file.percentile.apply(lambda x: f'{100*x:.0f}%')
out_file['tag_col'] = str(file).split('/')[2]
out_file['time'] = pd.to_datetime('today').strftime('%Y%m%d')
out_file = out_file.set_index('time')
out_file.index = pd.to_datetime(out_file.index)
db_client.write_points(out_file, 'measurement', database='database',
retention_policy='rp')
can anyone please help?

gremlin id column extraction - GLUE

I am trying to extract Neptune database vertices into CSV file which is failing extracting at id column. Below is the script i am trying to run in AWS GLUE Console.
import boto3
import os
import sys
import site
import json
import pandas as pd
from setuptools.command import easy_install
from importlib import reload
s3 = boto3.client('s3')
dir_path = os.path.dirname(os.path.realpath(__file__))
#os.path.dirname(sys.modules['__main__'].__file__)
install_path = os.environ['GLUE_INSTALLATION']
easy_install.main( ["--install-dir", install_path, "gremlinpython"] )
reload(site)
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import T
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
graph = Graph()
remoteConn = DriverRemoteConnection('wss://neptune-test-new-reader-1.c3nqs7vjaggx.eu-west-1.neptune.amazonaws.com:8182/gremlin','g')
g = graph.traversal().withRemote(remoteConn)
vertices_columns = ['id', 'label', 'region','country']
vertices = g.V().hasLabel('airport').limit(2).project('id','label','region','country').by('T.id').by('T.label').by('region').by('country').select(values).fold()
for v in vertices:
print(v)
Error:
Name 'Values' is not defined
Tried below script with for loop
import boto3
import os
import sys
import site
import json
import pandas as pd
from setuptools.command import easy_install
from importlib import reload
s3 = boto3.client('s3')
dir_path = os.path.dirname(os.path.realpath(__file__))
#os.path.dirname(sys.modules['__main__'].__file__)
install_path = os.environ['GLUE_INSTALLATION']
easy_install.main( ["--install-dir", install_path, "gremlinpython"] )
reload(site)
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import T
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
graph = Graph()
remoteConn = DriverRemoteConnection('wss://neptune-test-new-reader-1.c3nqs7vjaggx.eu-west-1.neptune.amazonaws.com:8182/gremlin','g')
g = graph.traversal().withRemote(remoteConn)
vertices_columns = ['id', 'label', 'region','country']
"""
vertices = g.V().hasLabel('airport').limit(2).project('id','label','region','country').by('T.id').by('T.label').by('region').by('country').select(values).fold()
for v in vertices:
print(v)
"""
#vertices = []
vertices = g.V().limit(1).valueMap(True).toList()
for v in vertices:
print(v)
for col in vertices_columns:
print(v[col])
#print(vertices)
Error:
Output of the print(v)
{T.id:1:'1',T.label:1:'airport','country':'US','region':'US-AL'}
Key Error: id
The values keyword used in select(values) is a reference to an enum defined as part of Column. In your code you can use select(Column.values) and you can include the definition using :
from gremlin_python.process.traversal import Column
Here is a Python example:
>>> g.V('3').project('id','label','code').by(T.id).by(T.label).by('code').toList()
[{'id': '3', 'label': 'airport', 'code': 'AUS'}]
>>> g.V('3').project('id','label','code').by(T.id).by(T.label).by('code').select(Column.values).toList()
[['3', 'airport', 'AUS']]
Note that T.id and T.label are not inside quotes.

how to connect input and function module in main module and can we return list from a function?

I have three modules: GetInput, Main and Converter. In the GetInput file there are all the inputs values and excel data in the form of list. In the Converter file I am using those input values from Getinput file and in the main file I am connecting both these files here. I am doing this so that my code can look more organized.
GetInput.py:
import pandas as pd
import numpy as np
import time
def getInputs():
df = pd.read_excel('input.xlsx')
actual = df['actual'].values.tolist()
schedule = df['schedule'].values.tolist()
freq = df['frequency'].values.tolist()
ACP = df['acp'].values.tolist()
modelInput = {
'actual': actual, 'schedule': schedule, 'freq': freq, 'ACP': ACP,'df' : df
}
return modelInput
Converter.py
import pandas as pd
def fun(modelInput):
underdraw = []
overdraw = []
for i,j, in zip(schedule, actual):
dev = j - i
if dev < 0:
underdraw.append(dev)
else:
underdraw.append(0)
if dev > 0:
overdraw.append(dev)
else:
overdraw.append(0)
df['underdraw'] = pd.Series(underdraw)
df['overdraw'] = pd.Series(overdraw)
df.to_excel('mainfile.xlsx')
Main.py
import pandas as pd
import numpy as np
from convert import *
from GetInputs import *
def fun1():
inpu = getInputs()
con = fun(inpu)
fun1()
This whole program works when I run it in a single module but it throw errors when I try divide my code into separate modules. Basically it throw error in GetInput.py and in Converter.py (df is not defined) file. I know its a very basic thing but I don't know how to make it work. There is no desired output for this program, I am already getting an output when I run it in a single file. I just want to divide my code in this format as I mentioned above: GetIput File, Converter File and Main File.
Keep all the files in same directory or else mention the file paths at the top of main code using os module.
You have misspelled the following in the main code:
from convert import *
from GetInputs import *
It should be:
from Converter import *
from GetInput import *
I have tested this using the following:
MainModule.py
from Converter import *
from GetInputs import *
def fun1():
inpu = getInputs()
con = fun(inpu)
fun1()
Converter.py
import pandas as pd
def fun(modelInput):
print("HIE" + modelInput)
GetInputs.py
def getInputs():
return "modelInput"

Why is "...like '%Wert'" not working with ogr.ExecuteSQL and dialect = SQLITE?

I am trying to count features that have the text "Fehler" in a certain field. This only works, when using the OGRSQL-dialect, but returns None when using SQLITE. I want to use the SQLITE-dialect, because I am looping over several SQL-Strings, and some of them contain SUM(LENGTH)/1000, which only works in SQLITE, and not with OGRSQL.
I am using Python 2.7.5 an gdal 2.1.3
Here is the code that I am using:
import os
try:
from osgeo import ogr
from osgeo import gdal
except:
sys.exit("Failed to import OGR-module. Please check the GDAL-Installation!")
srcFile = r"D:\Didi\Tmp\OGR_Select_Bug\Export.shp"
driver = ogr.GetDriverByName('ESRI Shapefile')
destFileDS = driver.Open(srcFile, 0)
sql = u"SELECT COUNT(*) as Anzahl from %s WHERE rstatus like '%%Fehler'" % (os.path.basename(srcFile)[:-4])
#Works
actLayer = destFileDS.ExecuteSQL(statement=sql.encode('utf-8'), dialect='OGRSQL')
actFeat = actLayer.GetNextFeature()
destFileDS.ReleaseResultSet(actLayer)
#Returns none
actLayer = destFileDS.ExecuteSQL(statement=sql.encode('utf-8'), dialect='SQLITE')
actFeat = actLayer.GetNextFeature()
destFileDS.ReleaseResultSet(actLayer)

Categories

Resources