powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / HTML, JavaScript, VBScript, CSS [игнор отключен] [закрыт для гостей] / Проблемы с переводом с JS на язык c#!!!
3 сообщений из 3, страница 1 из 1
Проблемы с переводом с JS на язык c#!!!
    #38925791
adimmat
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Добрый день, друзья!
Пару дней назад задал вопрос на счет перевода одного скрипта с JS на C#! К большому сожалению, не получил никакого ответа и помощи, поэтому сам начал переводить! Процесс затрудняется тем, что в JS нет строгой типизации переменных и дело усугубляется еще тем, что я в JS совсем не разбираюсь! Я был бы Вам крайне признателен, если бы Вы мне помогли с решением моей проблемы!
Вот скрипт, который нужно перевести на C#:
Код: javascript
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
/* Geohash encoding/decoding and associated functions        (c) Chris Veness 2014 / MIT Licence  */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */

/* jshint node:true, bitwise:false *//* global define */
'use strict';


/**
 * Geohash encode, decode, bounds, neighbours.
 *
 * @namespace
 */
var Geohash = {};


/* (Geohash-specific) Base32 map */
Geohash.base32 = '0123456789bcdefghjkmnpqrstuvwxyz';


/**
 * Encodes latitude/longitude to geohash, either to specified precision or to automatically
 * evaluated precision.
 *
 * @param   {number} lat - Latitude in degrees.
 * @param   {number} lon - Longitude in degrees.
 * @param   {number} [precision] - Number of characters in resulting geohash.
 * @returns {string} Geohash of supplied latitude/longitude.
 * @throws  Invalid geohash.
 *
 * @example
 *     var geohash = Geohash.encode(52.205, 0.119, 7);
 */
Geohash.encode = function(lat, lon, precision) {
    // infer precision?
    if (typeof precision == 'undefined') {
        // refine geohash until it matches precision of supplied lat/lon
        for (var p=1; p<=12; p++) {
            var hash = Geohash.encode(lat, lon, p);
            var posn = Geohash.decode(hash);
            if (posn.lat==lat && posn.lon==lon) return hash;
        }
    }

    lat = Number(lat);
    lon = Number(lon);
    precision = Number(precision);

    if (isNaN(lat) || isNaN(lon) || isNaN(precision)) throw new Error('Invalid geohash');

    var idx = 0; // index into base32 map
    var bit = 0; // each char holds 5 bits
    var evenBit = true;
    var geohash = '';

    var latMin =  -90, latMax =  90;
    var lonMin = -180, lonMax = 180;

    while (geohash.length < precision) {
        if (evenBit) {
            // bisect E-W longitude
            var lonMid = (lonMin + lonMax) / 2;
            if (lon > lonMid) {
                idx = idx*2 + 1;
                lonMin = lonMid;
            } else {
                idx = idx*2;
                lonMax = lonMid;
            }
        } else {
            // bisect N-S latitude
            var latMid = (latMin + latMax) / 2;
            if (lat > latMid) {
                idx = idx*2 + 1;
                latMin = latMid;
            } else {
                idx = idx*2;
                latMax = latMid;
            }
        }
        evenBit = !evenBit;

        if (++bit == 5) {
            // 5 bits gives us a character: append it and start over
            geohash += Geohash.base32.charAt(idx);
            bit = 0;
            idx = 0;
        }
    }

    return geohash;
};


/**
 * Decode geohash to latitude/longitude (location is approximate centre of geohash cell,
 *     to reasonable precision).
 *
 * @param   {string} geohash - Geohash string to be converted to latitude/longitude.
 * @returns {{lat:number, lon:number}} (Center of) geohashed location.
 * @throws  Invalid geohash.
 *
 * @example
 *     var latlon = Geohash.decode('u120fxw');
 */
Geohash.decode = function(geohash) {

    var bounds = Geohash.bounds(geohash); // <-- the hard work
    // now just determine the centre of the cell...

    var latMin = bounds.sw.lat, lonMin = bounds.sw.lon;
    var latMax = bounds.ne.lat, lonMax = bounds.ne.lon;

    // cell centre
    var lat = (latMin + latMax)/2;
    var lon = (lonMin + lonMax)/2;

    // round to close to centre without excessive precision: &#8970;2-log10(&#916;°)&#8971; decimal places
    lat = lat.toFixed(Math.floor(2-Math.log(latMax-latMin)/Math.LN10));
    lon = lon.toFixed(Math.floor(2-Math.log(lonMax-lonMin)/Math.LN10));

    return { lat: Number(lat), lon: Number(lon)};
};


/**
 * Returns SW/NE latitude/longitude bounds of specified geohash.
 *
 * @param   {string} geohash - Cell that bounds are required of.
 * @returns {{sw: {lat: number, lon: number}, ne: {lat: number, lon: number}}}
 * @throws  Invalid geohash.
 */
Geohash.bounds = function(geohash) {
    if (geohash.length == 0) throw new Error('Invalid geohash');

    geohash = geohash.toLowerCase();

    var evenBit = true;
    var latMin =  -90, latMax =  90;
    var lonMin = -180, lonMax = 180;

    for (var i=0; i<geohash.length; i++) {
        var chr = geohash.charAt(i);
        var idx = Geohash.base32.indexOf(chr);
        if (idx == -1) throw new Error('Invalid geohash');

        for (var n=4; n>=0; n--) {
            var bitN = idx >> n & 1;
            if (evenBit) {
                // longitude
                var lonMid = (lonMin+lonMax) / 2;
                if (bitN == 1) {
                    lonMin = lonMid;
                } else {
                    lonMax = lonMid;
                }
            } else {
                // latitude
                var latMid = (latMin+latMax) / 2;
                if (bitN == 1) {
                    latMin = latMid;
                } else {
                    latMax = latMid;
                }
            }
            evenBit = !evenBit;
        }
    }

    var bounds = {
        sw: { lat: latMin, lon: lonMin },
        ne: { lat: latMax, lon: lonMax }
    };

    return bounds;
};


/**
 * Determines adjacent cell in given direction.
 *
 * @param   geohash - Cell to which adjacent cell is required.
 * @param   dirn - Direction (N/S/E/W).
 * @returns {string} Geocode of adjacent cell.
 * @throws  Invalid geohash.
 */
Geohash.adjacent = function(geohash, dirn) {
    // based on github.com/davetroy/geohash-js

    geohash = geohash.toLowerCase();
    dirn = dirn.toLowerCase();

    if (geohash.length == 0) throw new Error('Invalid geohash');
    if ('nsew'.indexOf(dirn) == -1) throw new Error('Invalid direction');

    var neighbour = {
        n: [ 'p0r21436x8zb9dcf5h7kjnmqesgutwvy', 'bc01fg45238967deuvhjyznpkmstqrwx' ],
        s: [ '14365h7k9dcfesgujnmqp0r2twvyx8zb', '238967debc01fg45kmstqrwxuvhjyznp' ],
        e: [ 'bc01fg45238967deuvhjyznpkmstqrwx', 'p0r21436x8zb9dcf5h7kjnmqesgutwvy' ],
        w: [ '238967debc01fg45kmstqrwxuvhjyznp', '14365h7k9dcfesgujnmqp0r2twvyx8zb' ]
    };
    var border = {
        n: [ 'prxz',     'bcfguvyz' ],
        s: [ '028b',     '0145hjnp' ],
        e: [ 'bcfguvyz', 'prxz'     ],
        w: [ '0145hjnp', '028b'     ]
    };

    var lastCh = geohash.slice(-1);    // last character of hash
    var parent = geohash.slice(0, -1); // hash without last character

    var type = geohash.length % 2;

    // check for edge-cases which don't share common prefix
    if (border[dirn][type].indexOf(lastCh) != -1 && parent != '') {
        parent = Geohash.adjacent(parent, dirn);
    }

    // append letter for dirn to parent
    return parent + Geohash.base32.charAt(neighbour[dirn][type].indexOf(lastCh));
};


/**
 * Returns all 8 adjacent cells to specified geohash.
 *
 * @param   {string} geohash - Geohash neighbours are required of.
 * @returns {{n,ne,e,se,s,sw,w,nw: string}}
 * @throws  Invalid geohash.
 */
Geohash.neighbours = function(geohash) {
    return {
        'n':  Geohash.adjacent(geohash, 'n'),
        'ne': Geohash.adjacent(Geohash.adjacent(geohash, 'n'), 'e'),
        'e':  Geohash.adjacent(geohash, 'e'),
        'se': Geohash.adjacent(Geohash.adjacent(geohash, 's'), 'e'),
        's':  Geohash.adjacent(geohash, 's'),
        'sw': Geohash.adjacent(Geohash.adjacent(geohash, 's'), 'w'),
        'w':  Geohash.adjacent(geohash, 'w'),
        'nw': Geohash.adjacent(Geohash.adjacent(geohash, 'n'), 'w')
    };
};


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
if (typeof module != 'undefined' && module.exports) module.exports = Geohash; // CommonJS
if (typeof define == 'function' && define.amd) define([], function() { return Geohash; }); // AMD



А вот это мой перевод, что я смог понять и что я смог сделать сам:
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Console_Geo_Test
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }
    public class Geohash
    {
        public string base32 = "0123456789bcdefghjkmnpqrstuvwxyz";

        public static string Encode (double lat, double lon, int precision)
        {
            if (precision == null)
            {
                for (int p = 1; p <= 12; p++)
                {
                    string hash = Encode(lat, lon, p);
                    var posn = Decode(hash);
                    if (posn.lat == lat && posn.lon == lon) return hash;
                }
            }

            //lat = Convert.ToDecimal(lat);
            //lon = Convert.ToDecimal(lon);
            lat=(int)lat;
            lon = (int)lon;
            precision = Convert.ToInt32(precision);

            if (double.IsNaN(lat) || double.IsNaN(lon) || float.IsNaN(precision)) Console.WriteLine("Error! Invalid Geohash!");

            int idx = 0;
            int bit = 0;
            bool evenBit = true;
            string geohash = "";

            int latMin = -90;
            int latMax = 90;
            int lonMin = -180;
            int lonMax = 180;

            while (geohash.Length < precision)
            {
                if (evenBit)
                {
                    int lonMid = (lonMin + lonMax) / 2;
                    if (lon > lonMid)
                    {
                        idx = idx * 2 + 1;
                        lonMin = lonMid;
                    }
                    else
                    {
                        idx = idx * 2;
                        lonMax = lonMid;
                    }
                }
                else
                {
                    int latMid = (latMin + latMax) / 2;
                    if (lat > latMid)
                    {
                        idx = idx * 2 + 1;
                        latMin = latMid;
                    }
                    else
                    {
                        idx = idx * 2;
                        latMax = latMid;
                    }
                }
                evenBit = !evenBit;

                if (++bit == 5)
                {
                     //geohash+=
                     bit = 0;
                     idx = 0;
                }
            }
        
             return geohash;   
        } // Метод Encode

        public static decimal Decode(string geohash)
        {
            //var bounds = Geohash.bounds(geohash);

            double latMin = bounds.sw.lat;
            double lonMin = bounds.sw.lon;
            double latMax = bounds.ne.lat;
            double lonMax = bounds.ne.lon;

            lat = lat.ToString(Math.Floor(2 - Math.Log((double)(latMax - latMin)) / Math.Log(10, Math.E))); // Разобраться с decimal
            lon = lon.ToString(Math.Floor(2 - Math.Log((double)(lonMax - lonMin)) / Math.Log(10, Math.E))); // Разобраться с decimal

            //return {lat:int(lat), lon:int(lon)};
            return 0; 
        
        } // Метод Decode

        public static bounds (string geohash)
        {
            if (geohash.Length == 0) Console.WriteLine("Error, Invalid geohash");

            geohash = geohash.ToLower();

            bool evenBit = true;
            int latMin = -90;
            int latMax = 90;
            int lonMin = -180;
            int lonMax = 180;

            for (int i = 0; i<geohash.Length; i++)
            {
                //string chr = geohash[i];
                //var idx = Geohash.base32.indeof(chr);
                if (idx == -1) Console.WriteLine("Error, Invalid geohash.");

                for (int n = 4; n>=0; n--)
                {
                    int bitN = idx >> n & 1;
                    if (evenBit)
                    {
                        int lonMid = (lonMin + lonMax) / 2;
                        if (bitN == 1)
                        {
                            lonMin = lonMid;
                        }
                        else
                        {
                            lonMax = lonMid;
                        }
                    }
                    else
                    {
                        int latMid = (latMin + latMax) / 2;
                        if (bitN == 1)
                        {
                            latMin = latMid;
                        }
                        else
                        {
                            latMax = latMid;
                        }
                    }
                    evenBit = !evenBit; 
                }
            }
            var bounds = {
                            sw: {lat: latMin, lon: lonMin},
                            ne: {lat: latMax, lon:lonMax}
                         };
            
            return bounds;
        
        } //Метод bounds

    } // Класс Geohash

} //Konec
...
Рейтинг: 0 / 0
Проблемы с переводом с JS на язык c#!!!
    #38925809
adimmat
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Ребята, если трудно, был бы вам признателен, если бы вы помогли мне хотя бы с одной из функций, а именно,с функцией bounds!
...
Рейтинг: 0 / 0
Проблемы с переводом с JS на язык c#!!!
    #38926107
petalvik
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
adimmat,

нужно создать дополнительные классы (или структуры):
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
class Coordinate
{
    public int lat;
    public int lon;
};
class Bounds
{
    public Coordinate sw;
    public Coordinate ne;
};


И возвращать из метода bounds экземпляр Bounds:
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
public static Bounds bounds(string geohash)
{
    ...

    var bounds = new Bounds
    {
        sw = new Coordinate { lat = latMin, lon = lonMin },
        ne = new Coordinate { lat = latMax, lon = lonMax }
    };
    return bounds;
}



Вариант 2 - работать со словарями, вместо создания классов.
Вариант 3 - использовать dynamic (внутри тот же словарь).
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / HTML, JavaScript, VBScript, CSS [игнор отключен] [закрыт для гостей] / Проблемы с переводом с JS на язык c#!!!
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]