<jupyter_start><jupyter_text>Determinacion de puntos para toma de muestras<jupyter_code># python 3.7
#
# Parametros de entrada:
#    nombre del archivo formato csv
#    numero minimo de puntos a seleccionar o ... porcentaje minimo de puntos a seleccionar
#    distancia minima entre puntos <--- en mts
#
import random
import math
import sys, getopt
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import pandas as pd
# Print helpful messages
# set to False in Production
debug = False
plot  = True
# Lee el csv
filename = '../data/Puntos_Point.csv'

name = 'A<PASSWORD>'
#name = 'A<PASSWORD>'

# Filtra solo las filas con Name = ...
df = pd.read_csv(filename)
df = df[(df['Name'] == name )]

total_points =  len(df)

# Prop. of total point number
prop = 0.40
# Minimal Distance Allowed
min_distance_allowed = 141

print('Total Points in csv:',total_points)
print('% of Points to use:',prop*100, '| Number of points to use:',int(prop*len(df)) )
print('Min. distance allowed:',min_distance_allowed)
if debug: df.head(3)
# Transform wkt_geom to X, Y coord.
def regulariza(t):
    nc  = t.replace('(','')
    nc2 = nc.replace(')','')
    return nc2 

df[['sinuso','X','Y']] = df['wkt_geom'].str.split(" ",expand=True, )
df.drop(columns=['sinuso'],inplace=True)
df['X']=df['X'].apply(regulariza)
df['Y']=df['Y'].apply(regulariza)
if debug: df.head(3)
# To numeric
df['X'] = pd.to_numeric(df['X'], errors='raise', downcast='float')
df['Y'] = pd.to_numeric(df['Y'], errors='raise', downcast='float')
X = df['X'].values
Y = df['Y'].values
#df.info()

# Minimos
xmin = int(df['X'].min())
ymin = int(df['Y'].min())

# Maximos
xmax = int(df['X'].max())
ymax = int(df['Y'].max())

print('Min. coord.:',df['X'].min(), df['Y'].min(),' | Integer:',xmin,ymin)
print('Max. coord.:',df['X'].max(), df['Y'].max(),' | Integer:',xmax,ymax)<jupyter_output>Min. coord.: 11699435.0 1483940.4  | Integer: 11699435 1483940
Max. coord.: 11700935.0 1485740.4  | Integer: 11700935 1485740<jupyter_text>Puntos recibidos<jupyter_code>if plot:
    plt.title('Grid Points (loaded)')
    plt.scatter(X,Y, marker='.', s = 5);
# universe_points se obtiene del archivo csv
universe_points = []
for i in range(0,len(X)):
    x = X[i] #- xmin
    y = Y[i] #- ymin
    universe_points.append([x,y])
if debug: universe_points[0:5]
min_necesary_points = int(prop*len(universe_points))+1
print('Min. number of Points needed:',min_necesary_points)
#universe_points
# Crea un arreglo de points_number puntos  
# Esto debe ser obtenido desde el archivo csv
#total_points_number = 100
#for x in range(total_points_number):
#  universe_points.append( [random.randint(1,100),random.randint(1,100)] ) 
def distance(p,q):
    '''
    Euclidian distance
    '''
    return math.sqrt( (p[0]-q[0])*(p[0]-q[0]) + (p[1]-q[1])*(p[1]-q[1]) )  
def get_point():
    '''
    Get a new point from the points array
    '''
    i = random.randint(0, total_points-1)
    return universe_points[i]
def get_first_point():
    '''
    Get the first point from the points array
    The nearest point to the centroid of all points
    '''
    totx = 0
    toty = 0
    #xmin = 9999999999999999
    #xmax = -999999999999999
    #ymin = 9999999999999999
    #ymax = -999999999999999
    first_point = []
    n = len(universe_points)
    for i in range(0,n):
        x = universe_points[i][0]
        y = universe_points[i][1]
        #if x < xmin: xmin = x
        #if x > xmax: xmax = x
        #if y < ymin: ymin = y
        #if y > ymax: ymax = y
        totx = totx + x
        toty = toty + y
                
    #centroid = [ xmin + (xmax-xmin)/2 ,ymin + (ymax-ymin)/2 ]
    centroid = [ totx/n, toty/n ]

    if debug: print('*** Centroid:',centroid)
    
    d = 9999999999999999
    for i in range(0,len(universe_points)):
        if distance(universe_points[i], centroid) < d:
            d = distance(universe_points[i], centroid)
            first_point = universe_points[i]
    
    return first_point
def verify(point, selected_points):
    '''
    Verify that the new point is useful (based on a minimall distance)
    '''
    useful = False
    '''
    Compare the point with the rest of points selected
    '''
    i = 0
    while i < len(selected_points) :
        if debug: print('comparing ',point, selected_points[i], sep='')
        dist =  distance(point, selected_points[i])
        if  dist <= min_distance_allowed:
            if debug: print('not ok',int(dist), ' < ',  min_distance_allowed)
            useful = False
            return useful
        else:
            if debug: print('ok',int(dist), ' > ',  min_distance_allowed)
            useful = True
        i += 1
    return useful<jupyter_output><empty_output><jupyter_text>Ejecucion de la seleccion de puntos<jupyter_code>selected_points=[]
'''
First point added to selected_points
'''
p = get_first_point()
selected_points.append( p )
if debug: print('first point',selected_points)
if plot:
    plt.title('Centroid')
    plt.scatter(X,Y, c='b', marker='.', label='grid', s=10)
    plt.scatter(selected_points[0][0], selected_points[0][1], c='r', marker='X', label='centroid', s=40)
    plt.legend(loc='lower center')
    plt.show()
k = 0
max_iter = 1000
while ( len(selected_points) < min_necesary_points and k < max_iter ):
    p = get_point()
    if debug: print('checking if new point:',p, 'is useful ...')
    res = False
    res = verify(p, selected_points)
    if res:
        if debug: print('yes, is useful')
        selected_points.append( p )
    else:
        if debug: print('no, not useful')
    k += 1
    if debug: print('number of selected points',len(selected_points))
    #print('points selected', selected_points)
    

print('\n*** end of selection.\t',k, 'iterations')
print('*** number of selected points:',len(selected_points))
display = False
if display:
    print('selected points:')
    selected_points
XX=[]
YY=[]
for p in selected_points:
    XX.append(p[0]) 
    YY.append(p[1])<jupyter_output><empty_output><jupyter_text>Grafico con puntos seleccionados<jupyter_code>if plot:
    plt.title('Selected Points')
    plt.scatter(XX,YY, c='r', marker='+' );
if plot:
    plt.title('Selected Points in Grid')
    plt.scatter(X,Y, c='b', marker='.', label='grid', s=10)
    plt.scatter(XX, YY, c='r', marker='s', label='selected', s=10)
    plt.legend(loc='lower left')
    plt.show()<jupyter_output><empty_output><jupyter_text>Graba el csv con los puntos generados<jupyter_code>index = []
for i in range(0,len(XX)):
    index.append(i)

data = {
    'x': XX,
    'y': YY
}
dfsel = pd.DataFrame(data=data, columns=['x','y'], index=index)
filename_selected = '../data/sel_'+name+'.csv'
dfsel.to_csv(filename_selected)<jupyter_output><empty_output><jupyter_text>---<jupyter_code>def main(argv):
    filename = ''
    min_distance_allowed = 0
    percent = 0

    try:
        opts, args = getopt.getopt(argv,"hi:p:d",["filename=","percent="])
    except getopt.GetoptError:
        print ('sel_point.py -d <min_distance> -i <filename> -n <percent>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print ('sel_point.py -m <min_distance> -i <filename> -p <percent>')
            sys.exit()
        elif opt in ("-i", "--ifile"):
            filename = arg
        elif opt in ("-p", "--percent"):
            prop = arg
        elif opt in ("-m", "--min_distance"):
            min_distance_allowed = arg

    print ('CSV Filename :', filename )
    print ('Percent of. points :', prop )
    print ('Min. Distance :', min_distance_allowed )


if __name__ == "__main__":
   main(sys.argv[1:])<jupyter_output><empty_output><jupyter_text>---  Georeferenciado<jupyter_code>print('Centroid:',[selected_points[0][0], selected_points[0][1]] )

#p = [selected_points[0][0], selected_points[0][1]]
p = [-33.4726900, -70.6472400]

import folium
m = folium.Map(
    location= p,
    zoom_start=10,
    crs='EPSG3857',
#    tiles='Stamen',
    tiles='Stamen Terrain'
)

tooltip = 'Click'
folium.Marker(p, popup='<i>'+'punto_seleccionado'+'</i>', tooltip=tooltip).add_to(m)

    
#for i in range(0,3): #len(selected_points)):
#    folium.Marker([selected_points[i][0], selected_points[i][1]], popup='<i>'+'punto_seleccionado'+'</i>', tooltip=tooltip).add_to(m)

m
html_page = '../data/sel_'+name+'.html'
m.save(html_page)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1654658981748319, 'fraction_numerical': 0.030499519692603266, 'mean_word_length': 3.5613362541073386, 'pattern_counts': {'":': 1, '<': 49, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '580556', 'license': 'MIT', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3032, 'n_words': 710, 'path': 'sel_point.ipynb', 'repo_name': 'only4cc/sel_points', 'size': '8314'}