I am currently linear referencing a set of roads. Using IMSegmatation2.SetMsAsDistance2 works fine for single-part polylines but for multi-part polylines I want to set the M values as the length along the polyline to the point, not the shortest distance between the point and the start point of the polyline. The SetMsAsDistance2 function sets the Measures on parallel lines as equal. I want them to be different.
Apart from setting an M value for each polyline vertex does anyone know of a method that sets the M as the length along the polyline?
The solution is to use the IMSegmentation3.SetAndInterpolateMsBetween function.
Note that the solution assumes that the geometries that make up the feature are in the right order. It assigns measures geometry-by-geometry.
The code for this is:
public static void measuresAdd_NonDuplicating(ref IFeature pFeat, bool bHasZ, bool bIgnoreGaps, out string sError)
{
// Add non-duplicating measures to the feature
sError = "";
IMSegmentation3 pSeg;
IGeometryCollection pGeomColl;
//IGeometryCollection pNewGeomColl; // Use if geometries are to be re-ordered.
IGeometry pGeom;
IPolyline4 pPoly;
IQueryFilter pQ;
IMAware pMAware;
IZAware pZAware;
double dStartMeasure;
double dToMeasure;
double dMeasure;
double dLen;
try
{
if (pFeat.ShapeCopy.IsEmpty == false)
{
pGeomColl = (IGeometryCollection)new PolylineClass();
pGeomColl = (IGeometryCollection)pFeat.ShapeCopy;
if (pGeomColl.GeometryCount == 1)
{
// Single line geometry. Duplication not an issue.
pMAware = (IMAware)pFeat.ShapeCopy;
pMAware.MAware = true;
pSeg = (IMSegmentation3)pMAware;
pPoly = geometryToPolyline((IGeometry)pFeat.ShapeCopy, out sError);
if (sError.Length > 0)
{
sError = "measuresAdd_NonDuplicating\r\n" + sError;
return;
}
pSeg.SetMsAsDistance2(pPoly.FromPoint, 1, 0, bIgnoreGaps);
pFeat.Shape = (IGeometry)pSeg;
pFeat.Store();
}
else
{
// For re-ordering geometries. Not currently used.
//pNewGeomColl = (IGeometryCollection)new Polyline();
//IZAware pZawareNew = (IZAware)pNewGeomColl;
//pZawareNew.ZAware = bHasZ;
//IMAware pMAwareNew = (IMAware)pNewGeomColl;
//pMAwareNew.MAware = true;
dStartMeasure = 0;
dMeasure = 0;
// MultiGeometry. Place them in order and set the measures on each part increasing.
// Currently assumes the existing order is correct.
for (int i = 0; i < pGeomColl.GeometryCount; i++)
{
pGeom = pGeomColl.Geometry[i];
pPoly = geometryToPolyline(pGeom, out sError);
if (sError.Length > 0)
{
sError = "measuresAdd_NonDuplicating\r\n" + sError;
return;
}
// Measure Values
dStartMeasure = dMeasure;
if (i > 0) dStartMeasure += 0.01;
dLen = pPoly.Length;
dToMeasure = dMeasure + dLen;
// Set Measures
pMAware = (IMAware)pPoly;
pMAware.MAware = true;
pZAware = (IZAware)pPoly;
pZAware.ZAware = bHasZ;
pSeg = (IMSegmentation3)pPoly;
pSeg.SetAndInterpolateMsBetween(dStartMeasure, dToMeasure);
// If geometries are re-ordered into a connecting network
//IGeometryCollection pXGeomColl = new PolylineClass();
//pMAware = (IMAware)pXGeomColl;
//pMAware.MAware = true;
//pZAware = (IZAware)pXGeomColl;
//pZAware.ZAware = bHasZ;
//pXGeomColl = (IGeometryCollection)pPoly;
//for (int j = 0; j < pXGeomColl.GeometryCount; j++)
// pNewGeomColl.AddGeometry(pXGeomColl.Geometry[j]);
dMeasure += dLen;
}
pFeat.Shape = (IGeometry)pGeomColl;
}
}
}
catch (Exception ex)
{
System.Diagnostics.StackTrace pStack = new System.Diagnostics.StackTrace(ex, true);
System.Diagnostics.StackFrame pFrame = pStack.GetFrame(pStack.FrameCount - 1);
int iLineNo = pFrame.GetFileLineNumber();
sError = "ERROR: measuresAdd_NonDuplicating; Line: " + iLineNo + "\n" + ex.ToString();
}
}
Related
I am trying to make a multiple date axis graph in AmCharts 4
I have two inputs from two temperature sensors. The dateAxis is cutted at some value and the values are not properly set.
Any ideas please on what's wrong ?
Here is the graph from first temperature sensor
chart from sensor 1
2.The graph from second temperature sensor
chart from sensor 2
3.The resulting image of both sensors:
chart from both sensors
4.Here is the code:
`
chart.data = chartData;
if (chart.data[0]) {
seriesArray = Object.keys(chart.data[0]);
// TODO: only for occurDate
seriesArray.splice(0, 1);
}
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 50;
dateAxis.baseInterval = {
"timeUnit": "day",
"count": 1
};
dateAxis.skipEmptyPeriods = true;
// Create value axis
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
for(let i=0;i<seriesArray.length;i++){
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = seriesArray[i];
series.dataFields.dateX = "date";
series.strokeWidth = 2;
series.tensionX = 0.90;
series.name = seriesArray[i];
var bullet = series.bullets.push(new am4charts.Bullet());
bullet.tooltipText = seriesArray[i] + ": {valueY}" + unit;
bullet.adapter.add("fill", function(fill, target){
if(target.dataItem.valueY < 21){
return am4core.color("#D66D6D");
}
return fill;
})
var range = valueAxis.createSeriesRange(series);
range.value = goodRange.min;
range.endValue = 0;
range.contents.stroke = am4core.color("#D66D6D");
range.contents.fill = range.contents.stroke;
range.axisFill.fill = am4core.color("#cccccc");
range.axisFill.fillOpacity = 0.2;
range.grid.strokeOpacity = 0;
var range2 = valueAxis.createSeriesRange(series);
range2.value = 1000000;
range2.endValue = goodRange.max;
range2.contents.stroke = am4core.color("#D66D6D");
range2.contents.fill = range.contents.stroke;
range2.axisFill.fill = am4core.color("#cccccc");
range2.axisFill.fillOpacity = 0.2;
range2.grid.strokeOpacity = 0;
}
// Add scrollbar
var scrollbarX = new am4charts.XYChartScrollbar();
scrollbarX.series.push(series);
chart.scrollbarX = scrollbarX;
chart.cursor = new am4charts.XYCursor();
chart.legend = new am4charts.Legend();
chart.legend.position = "bottom";
chart.legend.scrollable = true;
}`
The default audio sample rate is 48000. Is it possible to change it to other values like 44100?
I log the value of AudioSettings.outputSampleRate and it shows 48000. But it doesn't seem possible to change that value.
Here is the code to change sample rate of Unity's AudioClip:
Most simple, but very rough
Averaging approach, but channels are get mixed
Averaging approach for each channel (best quality)
public static AudioClip SetSampleRateSimple(AudioClip clip, int frequency)
{
if (clip.frequency == frequency) return clip;
var samples = new float[clip.samples * clip.channels];
clip.GetData(samples, 0);
var samplesLength = (int)(frequency * clip.length) * clip.channels;
var samplesNew = new float[samplesLength];
var clipNew = AudioClip.Create(clip.name + "_" + frequency, samplesLength, clip.channels, frequency, false);
for (var i = 0; i < samplesLength; i++)
{
var index = (int) ((float) i * samples.Length / samplesLength);
samplesNew[i] = samples[index];
}
clipNew.SetData(samplesNew, 0);
return clipNew;
}
public static AudioClip SetSampleRateAverage(AudioClip clip, int frequency)
{
if (clip.frequency == frequency) return clip;
var samples = new float[clip.samples * clip.channels];
clip.GetData(samples, 0);
var samplesNewLength = (int) (frequency * clip.length) * clip.channels;
var samplesNew = new float[samplesNewLength];
var clipNew = AudioClip.Create(clip.name + "_" + frequency, samplesNewLength, clip.channels, frequency, false);
var index = 0;
var sum = 0f;
var count = 0;
for (var i = 0; i < samples.Length; i++)
{
var index_ = (int)((float)i / samples.Length * samplesNewLength);
if (index_ == index)
{
sum += samples[i];
count++;
}
else
{
samplesNew[index] = sum / count;
index = index_;
sum = samples[i];
count = 1;
}
}
clipNew.SetData(samplesNew, 0);
return clipNew;
}
public static AudioClip SetSampleRate(AudioClip clip, int frequency)
{
if (clip.frequency == frequency) return clip;
if (clip.channels != 1 && clip.channels != 2) return clip;
var samples = new float[clip.samples * clip.channels];
clip.GetData(samples, 0);
var samplesNewLength = (int) (frequency * clip.length) * clip.channels;
var clipNew = AudioClip.Create(clip.name + "_" + frequency, samplesNewLength, clip.channels, frequency, false);
var channelsOriginal = new List<float[]>();
var channelsNew = new List<float[]>();
if (clip.channels == 1)
{
channelsOriginal.Add(samples);
channelsNew.Add(new float[(int) (frequency * clip.length)]);
}
else
{
channelsOriginal.Add(new float[clip.samples]);
channelsOriginal.Add(new float[clip.samples]);
channelsNew.Add(new float[(int) (frequency * clip.length)]);
channelsNew.Add(new float[(int) (frequency * clip.length)]);
for (var i = 0; i < samples.Length; i++)
{
channelsOriginal[i % 2][i / 2] = samples[i];
}
}
for (var c = 0; c < clip.channels; c++)
{
var index = 0;
var sum = 0f;
var count = 0;
var channelSamples = channelsOriginal[c];
for (var i = 0; i < channelSamples.Length; i++)
{
var index_ = (int) ((float) i / channelSamples.Length * channelsNew[c].Length);
if (index_ == index)
{
sum += channelSamples[i];
count++;
}
else
{
channelsNew[c][index] = sum / count;
index = index_;
sum = channelSamples[i];
count = 1;
}
}
}
float[] samplesNew;
if (clip.channels == 1)
{
samplesNew = channelsNew[0];
}
else
{
samplesNew = new float[channelsNew[0].Length + channelsNew[1].Length];
for (var i = 0; i < samplesNew.Length; i++)
{
samplesNew[i] = channelsNew[i % 2][i / 2];
}
}
clipNew.SetData(samplesNew, 0);
return clipNew;
}
I'm trying to get outline working with a custom language in VScode. I have the below code but I feel like it is slow because of the way I find a range in class. Are there better ways to find the range and assign children. I've thought about just keeping track of the depth of the brackets and assigning all functions/methods/classes in higher depths into the last item of previous depth.
It was based off of this answer.
class JSLDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
public provideDocumentSymbols(document: vscode.TextDocument,
token: vscode.CancellationToken): Thenable<vscode.DocumentSymbol[]> {
return new Promise((resolve, reject) => {
var symbols: vscode.DocumentSymbol[] = [];
var depth = 0;
for (var i = 0; i < document.lineCount; i++) {
var line = document.lineAt(i);
var txt = line.text;
var ltxt = txt.toLowerCase();
let open_brackets = ltxt.match(/\(/g) || [];
let close_brackets = ltxt.match(/\)/g) || [];
// console.log(ltxt)
// console.log(open_brackets, close_brackets)
//console.log(i, open_brackets.length, close_brackets.length)
depth += open_brackets.length - close_brackets.length;
//console.log(depth);
if (ltxt.includes("define class(")) {
let sname = txt.trim().substr(14, txt.trim().length - 16); //this is hard coded right now but it's kind of working
let detail = "ARGS:x, y returns z";
let start_pos = new vscode.Position(i, 0);
let n_bracket = 1;
let i_char = 0;
//let children: vscode.DocumentSymbol[] = []
let ds = new vscode.DocumentSymbol(sname, detail, vscode.SymbolKind.Class, line.range, line.range);
for(var i_line = i; n_bracket > 0; i_line++){
let class_line = document.lineAt(i_line);
let mtxt = class_line.text;
let ic;
if(i == i_line) ic = 16;
else ic = 0;
for(i_char = ic; i_char < mtxt.length; i_char++){
if(mtxt[i_char] === "(") n_bracket++;
else if(mtxt[i_char] === ")") n_bracket--;
if(n_bracket === 0) break
}
if (/(\w[\w\d\s]*)=\s*method\({((?:\s*(?:\w[\w\d\s]*)(?:=[^,]*)?,?\s*)*)},/i.test(mtxt)) {
let result = mtxt.match(/(\w[\w\d\s]*)=\s*method\({((?:\s*(?:\w[\w\d\s]*)(?:=[^,]*)?,?\s*)*)},/i)!;
let mname = result[1].trim();
let m_details = ""
if(result.length == 3){
m_details = result[2].trim();
}
ds.children.push(new vscode.DocumentSymbol(mname, m_details, vscode.SymbolKind.Method, class_line.range, class_line.range));
}
if(n_bracket === 0) break
}
let end_pos = new vscode.Position(i_line, i_char);
let rng = new vscode.Range(start_pos, end_pos);
ds.range = rng;
//ds.children = children;
symbols.push(ds);
}
else if (/(\w[\w\d\s]*)=\s*function\({((?:\s*(?:\w[\w\d\s]*)(?:=[^,]*)?,?\s*)*)},/.test(ltxt)) {
let result = txt.match(/(\w[\w\d\s]*)=\s*function\({((?:\s*(?:\w[\w\d\s]*)(?:=[^,]*)?,?\s*)*)},/i)!;
let sname = result[1].trim();
let detail = "";
if(result.length == 3){
detail = "(" + result[2].trim() + ")";
}
symbols.push(new vscode.DocumentSymbol(sname, detail, vscode.SymbolKind.Function, line.range, line.range));
}
}
resolve(symbols);
});
}
}
I have a group of Multipart Polyline Geometries which have measures. I am trying to re-order the geometries to better follow the traffic flow.
I am using a GeometryBridge to add the re-orderd segments to an ISegmentCollection.
The problem is that my segments have their M values reset so they must be IMSegmentation4 segments. When I try to add these to an ISegmentCollection VS the program won't compile.
I am using VS2010, ArcGis 10.2 and Windows 7.
pExGeomColl = (IGeometryCollection)new Polyline();
pExGeomColl = (IGeometryCollection)pBaseFeat.ShapeCopy;
for (int i = 0; i < hGeomToFrom.Count; i++)
{
iTo = i;
iFrom = (int)hGeomToFrom[i];
pGeom = pExGeomColl.Geometry[iFrom];
pGeom.SpatialReference = pSpRef;
pMAware = (IMAware)pGeom;
pMAware.MAware = true;
pPolyline = (IPolyline6)new PolylineClass();
if (pGeom.GeometryType != esriGeometryType.esriGeometryPolyline)
{
pPolyline = geometryToPolyline(pGeom, false, true, ref sError);
if (sError.Length > 0)
{
sError = "cmdReset\r\n" + sError;
clsMain.write_log(sError, clsMain.m_eLogType.FATAL);
MessageBox.Show(sError);
return;
}
}
else
{
pPolyline = (IPolyline)pGeom;
}
dLen = pPolyline.Length;
dFrom = dMeasure;
dTo = dFrom + dLen;
pSeg = (IMSegmentation4)pPolyline;
pSeg.SetAndInterpolateMsBetween(dFrom, dTo);
dMeasure = dTo;
pSegArray[i] = pSeg;
ProgressBar1.Value = iCount;
iCount++;
}
// Add the segment array to a segment collection
pNewSegColl = (ISegmentCollection)new PolylineClass();
pMAware = (IMAware)pNewSegColl;
pMAware.MAware = true;
pGeomBridge = new GeometryEnvironmentClass();
pGeomBridge.AddSegments(pNewSegColl, pSegArray); // This doesn't work
pGeom = (IGeometry)pNewSegColl;
pGeom.SpatialReference = pSpRef;
pBaseFeat.Shape = pGeom;
pBaseFeat.Store();
Can anyone tell me how to accumulate a set of measure segments?
I found that using a Segment Collection is the wrong approach. A segment is the line segment between vertices of a polyline.
The trick is to put the polyline into a geometry collection and then add the parts of that geometry collection into the new (re-ordered) collection.
// Existing Geometry Collection
pExGeomColl = (IGeometryCollection)new PolylineClass();
pExGeomColl = (IGeometryCollection)pBaseFeat.ShapeCopy;
// New Geometry collection
pNewGeomColl = (IGeometryCollection)new PolylineClass();
pMAware = (IMAware)pNewGeomColl;
pMAware.MAware = true;
pZAware = (IZAware)pNewGeomColl;
pZAware.ZAware = bHasZ;
for (int i = 0; i < hGeomToFrom.Count; i++)
{
iTo = i;
iFrom = (int)hGeomToFrom[i];
pGeom = pExGeomColl.Geometry[iFrom];
pGeom.SpatialReference = pSpRef;
pMAware = (IMAware)pGeom;
pMAware.MAware = true;
pZAware = (IZAware)pGeom;
pZAware.ZAware = bHasZ;
// Convert the geometry to a polyline
pPolyline = (IPolyline6)new PolylineClass();
if (pGeom.GeometryType != esriGeometryType.esriGeometryPolyline)
{
pPolyline = geometryToPolyline(pGeom, bHasZ, true, ref sError);
if (sError.Length > 0)
{
sError = "cmdReset\r\n" + sError;
clsMain.write_log(sError, clsMain.m_eLogType.FATAL);
MessageBox.Show(sError);
return;
}
}
else
{
pPolyline = (IPolyline)pGeom;
}
pMAware = (IMAware)pPolyline;
pMAware.MAware = true;
pZAware = (IZAware)pPolyline;
pZAware.ZAware = bHasZ;
dLen = pPolyline.Length;
dFrom = dMeasure;
dTo = dFrom + dLen;
pSeg = (IMSegmentation4)pPolyline;
pSeg.SetAndInterpolateMsBetween(dFrom, dTo);
IGeometryCollection pXGeomColl = new PolylineClass();
pMAware = (IMAware)pXGeomColl;
pMAware.MAware = true;
pZAware = (IZAware)pXGeomColl;
pZAware.ZAware = bHasZ;
pXGeomColl = (IGeometryCollection)pPolyline;
for (int j = 0; j < pXGeomColl.GeometryCount; j++)
pNewGeomColl.AddGeometry(pXGeomColl.Geometry[j]);
dMeasure = dTo;
ProgressBar1.Value = iCount;
iCount++;
}
pGeom = (IGeometry)pNewGeomColl;
pGeom.SpatialReference = pSpRef;
pMAware = (IMAware)pGeom;
pMAware.MAware = true;
pZAware = (IZAware)pGeom;
pZAware.ZAware = bHasZ;
pBaseFeat.Shape = pGeom;
pBaseFeat.Store();
I have an file of points which are to be converted to a set of polylines. Some of the lines are multipart and I want to convert them to singlepart.
The points are processed and the polylines added to an Interim featureclass (for backup purposes). If a line is multipart it is added to this interim featureclass as multipart.
The interim featureclass is then read and it's features copied to a base featureclass. This works fine for single part features but I keep getting the 'No support for this geometry type' error when converting the multiparts to singleparts. The problem is that to create the multipart feature from a set of points I have to use a segment collection as a Path. I've tried setting this to a polyline but then adding line segments to it causes an error (wrong geometry type).
When I add the multipart features to the interim featureclass the geometries are Polylines. When I retrieve them later (by putting thte shape into a new GeometryCollection) the geometry collection is a Polyline but the individual geometries are Paths (?).
The code is:
1. Add points to interim featureclass by putting them in a pointcollection.
pPtColl = (IPointCollection4)new Polyline();
pGeomColl = (IGeometryCollection)new Polyline();
// Fill point collection
.......
// Create a path made up of segments from the point collection
ISegment pSegment;
ISegmentCollection pSegColl = (ISegmentCollection)new ESRI.ArcGIS.Geometry.Path(); // Fails if changed to new Polyline()
// M and Z aware
if (bHasZ == true)
{
pZAware = (IZAware)pSegColl;
pZAware.ZAware = true;
}
if (bHasM == true)
{
pMAware = (IMAware)pSegColl;
pMAware.MAware = true;
}
for (int n = 1; n < pPtColl.PointCount; n++)
{
pSegment = (ISegment)new Line();
pSegment.SpatialReference = pSpRef;
pSegment.FromPoint = pPtColl.Point[n - 1];
pSegment.ToPoint = pPtColl.Point[n];
pSegColl.AddSegment(pSegment, oMissing, oMissing);
}
pGeomColl.AddGeometry(pSegColl as IGeometry, oMissing, oMissing);
pGeom = (IGeometry)pGeomColl; // pGeom has geometry type = Polyline
pGeom.SpatialReference = pSpRef;
pFeat.Shape = pGeom;
This part of the code all works fine.
When processing these features from the interim featureclass to add them to the base featureclass I get an error because the geometry type from the geometry collection is 'Path' not 'Polyline'.
// Read the geometry from the interim feature into a geometry collection
pGeomColl = (IGeometryCollection)new Polyline();
pGeomColl = (IGeometryCollection)pFromFeature.ShapeCopy;
for (int j = 0; j < pGeomColl.GeometryCount; j++)
{
// Create a new (Polyline) feature pToFeat and populate its attributes
pToFeat = pToFC.CreateFeature();
....
// pGeomColl has geometry type = Polyline
pGeom = pGeomColl.Geometry[j]; // pGeom has geometry type = Path
pToFeat.Shape = pGeom; // Fails. pToFeat is a Polyline.
}
How can I ensure that the geometry collection contains geometries with Polylines rather than Paths?
Thanks,
JM
I have found what I condsider to be a workaround for this problem but it's not very pretty.
The solution is to cast the Path geometry into a polyline and then cast it back into a geometry when assigning it to the .Shape attribute.
if (pToFeat .ShapeCopy.GeometryType == esriGeometryType.esriGeometryPolyline && pGeom.GeometryType == esriGeometryType.esriGeometryPath)
{
IPolyline pPoly = (IPolyline)new Polyline();
pPoly = geometryToPolyline(pGeom, bHasZ, bHasM, ref sError);
if (sError.Length > 0)
{
sError = "processAdds; ID = " + sID + " OID = " + iOID.ToString() + sNL + sError;
clsMain.write_log(sError, clsMain.m_eLogType.FATAL);
iErrorCount++;
}
else
{
pToFeat.Shape = (IGeometry)pPoly;
}
}
private static IPolyline geometryToPolyline(IGeometry pInputGeom, bool bHasZ, bool bHasM, ref string sError)
{
IPolyline pPoly = null;
IGeometryCollection pPolyColl = null;
IZAware pZAware;
IMAware pMAware;
double dZ;
ISpatialReference pSpRef;
bool bIsMulti;
esriGeometryType pType;
try
{
sError = "";
pSpRef = pInputGeom.SpatialReference;
// Create a new polyline
pPoly = (IPolyline)new Polyline();
if (bHasZ == true)
{
pZAware = (IZAware)pPoly;
pZAware.ZAware = true;
}
if (bHasM == true)
{
pMAware = (IMAware)pPoly;
pMAware.MAware = true;
}
// Create the geometry collection
pPolyColl = (IGeometryCollection)new Polyline();
if (bHasZ == true)
{
pZAware = (IZAware)pPolyColl;
pZAware.ZAware = true;
}
if (bHasM == true)
{
pMAware = (IMAware)pPolyColl;
pMAware.MAware = true;
}
// Set the polyline as the geometry collection
pPoly = (IPolyline)pPolyColl;
pPoly.SpatialReference = pSpRef;
pPolyColl.AddGeometry(pInputGeom);
return pPoly;
}
catch (Exception ex)
{
System.Diagnostics.StackTrace pStack = new System.Diagnostics.StackTrace(ex, true);
System.Diagnostics.StackFrame pFrame = pStack.GetFrame(pStack.FrameCount - 1);
int iLineNo = pFrame.GetFileLineNumber();
sError = "ERROR: geometryToPolyline; Line: " + iLineNo + "\n" + ex.ToString();
return pPoly;
}
}